Skip to main content

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


Sanju said…
Brilliant John
Sanju 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
Unknown said…
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?
Unknown said…

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)


∑(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.


Popular posts from this blog

Your last name contains invalid characters

My last name is "Graham-Cumming". But here's a typical form response when I enter it:

Does the web site have any idea how rude it is to claim that my last name contains invalid characters? Clearly not. What they actually meant is: our web site will not accept that hyphen in your last name. But do they say that? No, of course not. They decide to shove in my face the claim that there's something wrong with my name.

There's nothing wrong with my name, just as there's nothing wrong with someone whose first name is Jean-Marie, or someone whose last name is O'Reilly.

What is wrong is that way this is being handled. If the system can't cope with non-letters and spaces it needs to say that. How about the following error message:

Our system is unable to process last names that contain non-letters, please replace them with spaces.

Don't blame me for having a last name that your system doesn't like, whose fault is that? Saying "Your last name …

All the symmetrical watch faces (and code to generate them)

If you ever look at pictures of clocks and watches in advertising they are set to roughly 10:10 which is meant to be the most attractive (smiling!) position for the hands. They are actually set to 10:09.14 if the hands are truly symmetrical. CC BY 2.0image by Shinji
I wanted to know what all the possible symmetrical watch faces are and so I wrote some code using Processing. Here's the output (there's one watch face missing, 00:00 or 12:00, because it's very boring):

The key to writing this is to figure out the relationship between the hour and minute hands when the watch face is symmetrical. In an hour the minute hand moves through 360° and the hour hand moves through 30° (12 hours are shown on the watch face and 360/12 = 30).
The core loop inside the program is this:   for (int h = 0; h <= 12; h++) {
    float m = (360-30*float(h))*2/13;
    int s = round(60*(m-floor(m)));
    int col = h%6;
    int row = floor(h/6);
    draw_clock((r+f)*(2*col+1), (r+f)*(row*2+1), r, h, floor(m…

The Elevator Button Problem

User interface design is hard. It's hard because people perceive apparently simple things very differently. For example, take a look at this interface to an elevator:

From flickr

Now imagine the following situation. You are on the third floor of this building and you wish to go to the tenth. The elevator is on the fifth floor and there's an indicator that tells you where it is. Which button do you press?

Most people probably say: "press up" since they want to go up. Not long ago I watched someone do the opposite and questioned them about their behavior. They said: "well the elevator is on the fifth floor and I am on the third, so I want it to come down to me".

Much can be learnt about the design of user interfaces by considering this, apparently, simple interface. If you think about the elevator button problem you'll find that something so simple has hidden depths. How do people learn about elevator calling? What's the right amount of informati…