/**
* 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.
*/
const main = (limit) => {
// 1 = 1 - 3
// 2 = 4 - 5
// 3 = 9 - 7
// 4 = 16 - 9
// 5 = 25 - 11
// 6 = 36 - 13
// 7 = 49 - 15
// 8 = 64 - 17
// 9 = 81 - 19
// 10 = 100 - 21
// 11 = 121 - 23
// 12 = 144 - 25
// 13 = 169 - 27
// 14 = 196 - 29
// d = (a^2 - b^2)
let sumSquares = 0;
let squareOfSum = 0;
let difference;
for (let n = 1; n <= limit; n++) {
sumSquares += Math.pow(n, 2);
squareOfSum += n;
}
//
squareOfSum = Math.pow(squareOfSum, 2);
return sumSquares - squareOfSum;
}
console.log('difference', main(100));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.