* fix: remove repl.it links english * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add extra line Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
1.0 KiB
1.0 KiB
title
title |
---|
Sum square difference |
Problem 6: Sum square difference
Method:
-
Sum of first n natural numbers can be calculated by using this formula:
-
Sum of squares of n natural numbers can be calculated by using this formula:
-
We can calculate the values using the above formula and subtract them to get the result.
Solution:
function sumSquareDifference(n) {
const sumOfN = (n*(n+1))/2;
const sumOfNSquare = (n*(n+1)*(2*n+1))/6;
//** is exponentaial operator added in ES7, it's equivalent to Math.pow(num, 2)`
return (sumOfN ** 2) - sumOfNSquare;
}