DEV Community

Discussion on: Project Euler #6 - Sum Square Difference

Collapse
 
natonathan profile image
Nathan Tamez • Edited

Here is my nodejs solution

function sumSquareDifference(min, max) {
    let sumOfSquares = 0;
    let squareOfSums = 0;
    for (i = min; i < max + 1; i++) {
        sumOfSquares += i ** 2;
        squareOfSums += i;
    }

    return (squareOfSums ** 2) - sumOfSquares;
}
const startTime = Date.now();
console.log(`The difference between the sum of the squares of the first one
 hundred natural numbers and the square of the sum,
is ${sumSquareDifference(1, 100)}`);
console.log(`Time Taken: ${Math.round(Date.now() - startTime)}ms`);

output

The difference between the sum of the squares of the first one hundred natural numbers and the square of the sum,is 25164150
Time Taken: 2ms
Collapse
 
nans profile image
Nans Dumortier • Edited

Yours performs faster than the one I submitted, and still looks nice, well done !

EDIT: some of the lets could be replaced with const though 🙈

Collapse
 
natonathan profile image
Nathan Tamez

true