Problem 6 Sum square difference

来源:互联网 发布:影集软件 编辑:程序博客网 时间:2024/05/22 13:45

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.

12+22+32+...+n2=n(n+1)(2n+1)6 平方和公式
1+2+3+...+n=n(n+1)2 等差数列

import mathdef run(n):        square_sum = n*(n+1)*(2*n+1)/6        sum_square = math.pow(n*(n+1)/2,2)        return sum_square - square_sum
原创粉丝点击