Euler #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.

https://projecteuler.net/problem=6

I feel like this problem is gently poking fun at a classic math mistake. I know I’ve made this mistake before! Much to the chagrin of math teachers worldwide,

(a+b)^2 \neq a^2+b^2

unless at least one of the numbers is zero.

Anyway, this problem doesn’t require any programming. Just some applications of summations and their properties, like how I solved Problem 1 and Problem 2.

The square of the sum of the first n natural numbers is (\frac{n(n+1)}{2})^2. The sum of the squares of the first n natural numbers is a bit trickier. It is \frac{n(n+1)(2n+1)}{6}. Some proofs are provided on ProofWiki. We now can set up the difference:

\Bigg(\frac{n(n+1)}{2}\Bigg)^2 - \frac{n(n+1)(2n+1)}{6}

After a lot of FOILing, we arrive at the simplified polynomial:

\frac{1}{12}\big(3n^4+2n^3-3n^2-2n\big)

Plugging and chugging with n=100 yields the answer: 25164150.