The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 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.
译文:
自然数1到10的平方之和为385,他们的和的平方为3025,现将自然数1到10的和的平方减去平方之和等于2640,求出自然数从1到100的和的平方减去平方之和。
================================
第一次code:
1 public class Main 2 { 3 public static void main(String[] args) 4 { 5 System.out.println(start(100)-run(100)); 6 } 7 /** 8 * 求前N项平方之和 9 * @param n 10 * @return 11 */ 12 public static int run(int n) 13 { 14 int m=0; 15 for(int i=0;i<n+1;i++) 16 { 17 m += i*i; 18 } 19 return m; 20 } 21 /** 22 * 求前N项和的平方 23 * @param n 24 * @return 25 */ 26 public static int start(int n) 27 { 28 int m=0,o=0; 29 for(int i=0;i<n+1;i++) 30 { 31 m += i; 32 } 33 o = m*m; 34 return o; 35 } 36 }