The Difference Between Sum of Squares and Square of the Sum

  • 时间:2020-09-10 12:55:33
  • 分类:网络文摘
  • 阅读:130 次

The sum of the squares of the first ten natural numbers is,
1^2+2^2+3^2+…10^2=385

The square of the sum of the first ten natural numbers is,
(1+2+…+10)^2=55^2=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.

Bruteforce Algorithm

Given the input is 100, we can bruteforce.

1
2
3
4
5
6
7
let sum = 0;
let square_sum = 0;
for (let i = 1; i <= 100; ++ i) {
   sum += i;
   square_sum += i * i;
}
console.log(square_sum - sum * sum);
let sum = 0;
let square_sum = 0;
for (let i = 1; i <= 100; ++ i) {
   sum += i;
   square_sum += i * i;
}
console.log(square_sum - sum * sum);

The answer is 25164150

Math Formula to compute the Difference Between Sum of Squares and Square of the Sum

We know the Sum of first N Natural numbers is: tex_10eb31a3ccf5cc4326aec18a51c0adf8 The Difference Between Sum of Squares and Square of the Sum javascript math project euler . It would be great if we can find out the formula for the sum of squares.

Let’s assume is it (the sum of squares) tex_334c00e322f2992abde11bd833ce6483 The Difference Between Sum of Squares and Square of the Sum javascript math project euler . The first few values:

tex_8872d0fdf9d4e6025b8c193699b40888 The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_087e1be5a25d075a3968fb7ae7122887 The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_fd4f2c30c5752fb2c1311295839a3d21 The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_0bc416269f87247026f2d6d99aac0910 The Difference Between Sum of Squares and Square of the Sum javascript math project euler

With these four equations, we can obtain the following values by solving the system of equations:

tex_540ab656445d06ff498b5e81a46c4c66 The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_afed91675f9be83d934b5d668c67aaec The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_66ad763c6278eb699609cb34e571af3c The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_c657fe8402142d8c7ac96eceade581f2 The Difference Between Sum of Squares and Square of the Sum javascript math project euler

Thus, the f() function can be guessed as:

tex_af7fea100321e2b1b7cd6a70e7c286c9 The Difference Between Sum of Squares and Square of the Sum javascript math project euler

We just have to prove this by induction:

tex_9556cbae338f2d9eef4356b58e7688b9 The Difference Between Sum of Squares and Square of the Sum javascript math project euler
tex_85df87d1552d574442b3695535d29fb1 The Difference Between Sum of Squares and Square of the Sum javascript math project euler

Both sides are equals, thus the correct formula has been obtained.

Therefore, it can be computed pretty quickly in O(1) constant time by any input value:

1
2
3
4
const limit = 100;
const sum = limit * (limit + 1) / 2;
const sum_eq = (2*limit + 1) * (limit + 1) * limit / 6;
console.log(sum*sum-sum_eq);
const limit = 100;
const sum = limit * (limit + 1) / 2;
const sum_eq = (2*limit + 1) * (limit + 1) * limit / 6;
console.log(sum*sum-sum_eq);

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
豆腐搭配鸡蛋做出香酥可口的丸子,营养也很丰富  这道小学奥数题难倒多数学生,解题关键是比例  分享茄子的家常做法,吃起来不油腻,营养美味又下饭  中华人民共和国慈善法(主席令第四十三号)  中华人民共和国深海海底区域资源勘探开发法(主席令第四十二号)  全国人民代表大会常务委员会关于修改《中华人民共和国人口与计划生育法》的决定(主席令第四十一号)  全国人大常委会关于修改《中华人民共和国高等教育法》的决定(主席令第四十号)  中华人民共和国反家庭暴力法(主席令第三十七号)  全国人大常委会关于修改《中华人民共和国教育法》的决定(主席令第三十九号)  中华人民共和国国家勋章和国家荣誉称号法(主席令第三十八号) 
评论列表
添加评论