Problem 6
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.
求:求前100个自然数的平方和和的差
思路
- 求100个自然数和的平方 (1 + 2 + … + 100)^2
- 求100个自然数平方的和 1^2 + 2^2 + … + 100^2
- 相减
代码
%% 6 平方和-(和)的平方
suma=0;
sumb=0;
for i=1:1:100;
i=i*i;
suma=suma+i;
i=i+1;
end
i=1:1:100;
m=sum(i);
sumb=m*m;
Dif=sumb-suma
答案
25164150