Monday, January 21, 2008

Proof that the sum of the squares of the first n whole numbers is n^3/3 + n^2/2 + n/6

A recent thread on Hacker News that I started with a flippant comment turned into a little mathematical puzzle.

What's the sum of the square of the first n whole numbers?

It's well known that the sum of the first n whole numbers is n(n+1)/2. But what's the value of sum(i=1..n) n^2? (I'll call this number S for the remainder of this post).

It turns out that it's easy to prove that S = n^3/3 + n^2/2 + n/6 by induction. But how is the formula derived? To help with reasoning here's a little picture of the first 4 squares stacked up one on top of the other: If we fill in the blank squares to make a rectangle we have the basis of a derivation of the formula: Looking at the formerly blank squares (that I've numbered to assist with the thinking) we can see that the columns have 1 then 1+2 then 1+2+3 and finally 1+2+3+4 squares. Thus the columns are sums of consecutive whole numbers (for which we already have the n(n+1)/2 formula.

Now the total rectangle is n+1 squares wide (in this case 5) and its height is the final sum of whole numbers up to n or n(n+1)/2 (in the image it's 4 x 5 / 2 = 10. So the total number of squares in the rectangle is (n+1)n(n+1)/2 (in the example that's 5 x 10 = 50).

So we can calculate S as the total rectangle minus the formerly blank squares which gives:

S = (n+1)n(n+1)/2 - sum(i=1..n)sum(j=1..i) j
= (n(n+1)^2)/2 - sum(i=1..n) i(i+1)/2
2S = n(n+1)^2 - sum(i=1..n) i(i+1)
= n(n+1)^2 - sum(i=1..n) i^2 - sum(i=1..n) i
= n(n+1)^2 - S - n(n+1)/2
3S = n(n+1)^2 - n(n+1)/2
= n(n+1)( n+1 - 1/2 )
= n(n+1)(n+1/2)
= (n^2+n)(n+1/2)
= n^3 + n^2/2 + n^2 + n/2
= n^3 + 3n^2/2 + n/2
S = n^3/3 + n^2/2 + n/6

sanjay said...

Brilliant John

sanjay said...

Brilliant John

Ervin Peretz said...

Thanks! I love these geometrically-formulated proofs; they're much easier to understand, appreciate, and admire.

warrior said...

great work

I am not a mathematician. I love the simplicity of this proof but I cannot understand the formula for the blank squares, could you possibly help with that?

therealjohn said...

Matthew,

I struggled to follow the algebra in this post due to the formatting as well. As we know, the height of the nth column of the numbered squares, is the sum of the first n integers, ∑k = (1/2)(n^2+n). So the fourth column of the numbered squares, from left to right, will have a height of ten.

=> The total area of the blank square shape will be equal to the sum of all the columns. So we have a sum of sums. I think this is what is represented by the following:
sum(i=1..n)sum(j=1..i) j

Although, I find that formatting/notation quite confusing. I prefer the following for the sum of sums:
∑(∑k) from k=0 to k = n

I hadn't worked with sums of sums before, but they obey the distributive and associative law, so:

a∑k = ∑a(k)

and

∑(a + b) = ∑a + ∑b

So, for the algebraic portion of this post, I would re-write is as follows.
Given S = ∑n^2, which is what we want to find, we know that:

S = (n+1)n(n+1)/2 - ∑(∑n)
=> S = (n+1)n(n+1)/2 - ∑((1/2)(n^2+n)) [ ∑k from 0 to n is (1/2)(n^2+n)]

=> S = (1/2)(n+1)n(n+1) - (1/2)∑(n^2+n) [By distributive law, can take out 1/2 from the sum term]

=> 2S = (n+1)n(n+1) - ∑(n^2+n)

=> 2S = (n+1)n(n+1) - (∑n^2 + ∑n) [By associative law of summands]
=> 2S = (n+1)n(n+1) - (S + (1/2)(n^2+n)) [ Since S = ∑n^2 and ∑n = (1/2)(n^2+n)]
=> 3S = (n+1)n(n+1) - (1/2)(n^2+n)

From here, the blog post is quite clear and in agreement with my derivation, i.e.

3S = n(n+1)^2 - n(n+1)/2

Hope this has made sense.

John