baru003のブログ

baruの雑記兼備忘録

PE 0006

・問題リンクWhat is the difference between the sum of the squares and the square of the sums?

・コメント
100個の自然数の和の二乗と二乗の和の差を求める。

・ソース

import java.util.*;

public class P0006 {

	public static void main(String[] args) {
		long ans1 = 0;
		long ans2 = 0;
		for (long i = 1; i <= 100; i++) {
			ans1 += (i * i);
			ans2 += i;
		}
		ans2 *= ans2;
		long ans = Math.abs(ans2 - ans1);
		System.out.println(ans);
	}

}