Sum square difference

Problem 6

The sum of the squares of the first ten natural numbers is,

1 2 + 2 2 + ... + 10 2 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10) 2 = 55 2 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

平方和之差

问题 6

前十个正自然数的平方的和如下,

1 2 + 2 2 + ... + 10 2 = 385

前十个正自然数的和的平方如下,

(1 + 2 + ... + 10) 2 = 55 2 = 3025

因此前10个正自然数的和的平方与前10个正自然数的平方的和的差是 3025 385 = 2640.

求前100个正自然数的和的平方与前100个正自然数的平方的和的差是多少.

public class Euler6
{
    public static void main(String[] args)
    {
        int sumSquare=0,t=0,squareSum=0;
        for(int i=1;i<=100;i++)
        {
            squareSum+=i*i;
        }
        t=100*(100+1)/2;
        sumSquare=t*t;
        System.out.print("平方和之差=");
        System.out.println(sumSquare-squareSum);
    }
}