This article will walk you through how to solve Project Euler problem 6. If you haven’t already, I recommend that you read my Project Euler problem 6 overview article before you come back to this one so you can take a crack at the problem yourself. But, if you’ve already tried the problem and are stumped, you’re in the right place.

## Project Euler Problem 6 – Sum square difference

The sum of the squares of the first ten natural numbers is, $1^2 + 2^2 + ... + 10^2 = 385$

The square of the sum of the first ten natural numbers is, $(1+2+ ... +10)^2=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−385=2640

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

If you know the right formulas, this problem doesn’t require programming at all. In fact, it is just a math problem disguised as a programming problem.

In my overview article, I provided two formulas. One to find the sum of a number of natural numbers, and one to find the squares of natural numbers. Those formulas are listed below:

Sum of n natural numbers: $S_{n}=\frac{n(n+1)}{2}$

Sum of squares of n natural numbers: $S^2_{n}=\frac{n(n+1)(2n+1)}{6}$

Now, the problem asks us to find the difference between the sum of the squares of the first one hundred natural numbers (which we would use our second formula for), and the squares of the sum (the result of the second equation squared). Using our formulas above, we can write out the following equation: $S^2_{100} - (S_{100})^2 =|(\frac{100(100+1)(2(100)+1)}{6}) - (\frac{100(100+1)}{2})^2|$

And if you plug that into a calculator, you do in fact get the correct answer.

## Where’s the code?

The way I solved this problem only requires a basic calculator to execute. Of course, there are other more programming oriented ways of solving this problem, but why go the hard route of solving a problem when you can just have math do it for you? Also, going an iterative or recursive (I don’t know why you would go recursive in this case but ok) way would scale the program algorithmically as the input size got bigger. The way of doing it with mathematical formulas results in the program running in constant time (O(1)) every time.

However, I did write out the code to do the problem iteratively. As I said, coding isn’t the best way to solve this problem, but for those of you who love reading code on the internet, here you go.

There you go. Hopefully, your desire for machine-readable code has now been fulfilled.

Hey! Before you click away to a different website or back to your coding project, you should consider subscribing to my newsletter. It helps me grow my audience and develop my presence on the internet, and it helps you stay on top of my articles and become a better programmer, or at least an entertained one. So why don’t you subscribe to my newsletter today? After all, there’s no harm. You can always unsubscribe.