The sum of the squares of the first ten natural numbers is,
The square of the sum of the first ten natural numbers is,
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.
답은 25,164,150
코드가 지저분하지만 이렇게 풀었다. BigInteger 클래스를 이용하는 것이 키 포인트...
import java.math.BigInteger;
class sumSquareDiff{
public static void main(String[] args){
BigInteger bisum = new BigInteger("0");
BigInteger bi = new BigInteger("1");
BigInteger i = new BigInteger("1");
BigInteger sum2 = new BigInteger("0");
for(int j=1; j<=100; j++){
bi = i.multiply(i);
bisum = bisum.add(bi);
i = i.add(BigInteger.ONE);
}
i = BigInteger.ONE;
for(int j=1; j<=100; j++){
sum2 = sum2.add(i);
i = i.add(BigInteger.ONE);
}
sum2 = sum2.multiply(sum2);
// System.out.println(bisum);
// System.out.println(sum2);
i = sum2.subtract(bisum);
System.out.println(i);
}
}