As with all my posts here I’ll try to give a more verbose version of what the book covers; specifically how to get the sum of squares closed form using “Method 4: Replace sums by integrals”. All the book explains is finding the error in the approximation and the approximation itself.

This post assumes you have the book to hand although hopefully it’s readable on it’s own. Any mistakes in this text are my own.

In Concrete Mathematics (Knuth, Graham, Patashnik) they use integrals to as a stepping stone to find the closed form for the sum of squares. I am not as eloquent as they are so I’ll just quote them here

In calculus, an integral can be regarded as the area under a curve, and we can approximate this area by adding up the areas of long, skinny rectangles that touch the curve. We can also go the other way if a collection of long, skinny rectangles is given: Since n is the sum of the areas of rectangles whose sizes are 1×1, 1×4, . . . , 1×n2, it is approximately equal to the area under the curve f(x)=x2 between 0 and n.

I am not even going to try and replicate the graph in the book so here is one made with mathisfun.com integral approximation calculator. The area under this graph is approximately n

approximation x^2

This post covers the first “sub-method” in Method 4: Replace sums by integrals

Method 1 - Examine Error in Approximation n3/3Permalink

What confused me is this is not exactly the same as counting the error wedges on the beautiful graph in the book (that comes in the next method). This is literally the error in our first pass closed form approximation using the integration rules where the area under x2 is x2dx=n3/3. Thus we know the sum of squares is approximately 13n3.

Thus the error in approximation is En=n13n3 and therefore En1=n1(n1)33. Rearranging we get n1=En1+(n1)33

We also know what n=n1+n2. The book then very elegantly puts this all together to find a simple recurrence for En as follows

En=n13n3=n1+n213n3=En1+(n1)33+n213n3=En1+n13

Method 2 - Summing Wedge Shaped Error TermsPermalink

This means summing the error shaped wedges above the curve

error wedges

What I needed some pondering to understand is that a given rectangle is x2 and the area under the curve for that section is is k1kx2dx. What we are doing is subtracting the latter from the former to find the error wedges. Thus in total the full error would be

En=n0nx2dx

Now let’s step-wise sum of each x2 rectangle and that sections portion under the curve using the same colours as in the graph

k=1n(k2k1kx2dx)

Then, by using the rules of integration to find the anti-derivative of something squared, we will find the area under graph of just that section k1k

k=1n(k2k33(k1)33)

And finally we land at our error in the approximation recurrence again

k=1nk13

Stepping Stone: Finding En Recurrence Closed FormPermalink

Therefore if we could just find the closed form of En we can find the closed form of n because we can do n=En+13n3. Let’s break the recurrence En down to it’s component bits

  • En1+n - Here we are just summing in which we can do with Gauss’ formula: n(n1)/2
  • 13 - This is just a constant so to find the sum we simply multiply by how many times we’re summing so n13

THUS our En closed form is

En=n(n+1)2n3

Tying it Together: Using En to Find n Closed FormPermalink

We can prove En closed form works via some induction I’ll add at the end to avoid distracting from the main topic. So, finally, our n ends up being

n=n(n+1)2n3+13n3     for n0

Which is equivalent to what the book provides in equation 2.39

n=n(n+12)(n+1)3     for n0

TODO: Convert from the form I have above to the book’s closed form 2.39.

Appendix: Induction Proof of EnPermalink

For the recurrence En=En1+n13 we find that the closed form is En=n(n+1)2n3. This checks out via the following induction proof (following the same process as in my super simple induction post)

n(n+1)2n3=(n1)n2n13+n133n(n+1)2n6=3n(n1)2(n1)+6n263n2+3n2n6=3n23n2n+2+6n263n2+n6=3n2+n6

And RHS is the same as LHS so closed form is OK.