Project Euler: Problem 6

Dec 6, 2025

🚨

Spoiler alert: This post contains spoilers for Project Euler.

Problem 6

projecteuler p6 (light) Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

JS Solution 1 (brute force)

This one is easy enough to brute force (though that's not the only, or "best" way to solve)

JS Solution 2 (System of equations)

There's a neat solution in the overview attached the problem.

This isn't something I would have thought to come up with on my own. The overview pdf provides pseudo code, so I've translated that to JS.

Aside from the obvious lack of a need for any looping, the cool thing about this solution is that limit can be as high as your system/language can support.

Conclusion

It's pretty neat that the difference can be directly calculated with the 2nd solution. For fun, change the limit of both solutions to 10,000,000,00010,000,000,000

Copy/pasteable: 10000000000

The first code block will most likely timeout, while the second one will complete instantly.

Final answer

2516415025164150

Ryan McIntire