Euler_Problem6

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 = 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:一组数和的平方与平方的和的差是这组数两两相乘的积的和的二倍;

分析2:写出上面和的形式

1x2 1x3 1x4 ...   =   1x(2+3+4+...)

      2x3 2x4 ...   =   2x(3+4+...)

            3x4 ...   =   3x(4+...)

...

    99x100    ...   =  99x(100)

这样的形式可以看出,采用倒序求和的方式,将只使用一个循环就可以求出和。

但有没有更好的方式呢?

你可能感兴趣的:(算法,Project Euler,和的平方与平方和之差)