-
Notifications
You must be signed in to change notification settings - Fork 0
/
006.js
35 lines (31 loc) · 1.04 KB
/
006.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* The sum of the squares of the first ten natural numbers is,
* Math.pow(1, 2)+ Math.pow(2, 2) +....+ Math.pow(10, 2) = 385
* The square of the sum of the first ten natural numbers is,
* Math.pow(1 + 2 +....+ 10, 2) = Math.pow(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 - 285 = 2640
* Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum
*/
function sumOfSquares(limit) {
let sum = 0;
for (let i = 1; i <= limit; i++) {
sum += Math.pow(i, 2);
}
return sum;
}
function squareOfSums(limit) {
let sum = 0;
for (let i = 1; i <= limit; i++) {
sum += i;
}
sum = Math.pow(sum, 2);
return sum;
}
//inefficient!!!!!! apperently there are formula's for this
function findDifferenceSquarednumbers(limit) {
let sumOSquares = sumOfSquares(limit);
let squareOSums = squareOfSums(limit);
return squareOSums - sumOSquares;
}
console.log(findDifferenceSquarednumbers(100));