Sum of cubes via difference tables

Yesterday I watched the most recent Numberphile video, in which Ben Sparks explains a few finite series and the equations that simplify the calculation of their sums. He derives the formulas graphically, and it’s all very cleverly done, especially the one for the sum of cubes.

The idea is to get a simple polynomial expression in n for

N=m=1nm3

As I said, Ben’s graphical method is really clever and easy to understand, and it leads to this expression:

N=14n2(n+1)2

I wanted to see if I could get that same result using a nongraphical and less clever method. The method that came to mind was to generate a handful of values, put them in a table, and start taking differences.

Here are some values of n, N, and the first four differences:

n N Δ Δ² Δ³ Δ⁴
1 1 8 19 18 6
2 9 27 37 24 6
3 36 64 61 30
4 100 125 91
5 225 216
6 441

The differences are calculated by looking in the preceding column and subtracting the value in the same row from the value in the following row. This sort of thing is fairly easy to do by hand but is really easy to do in a spreadsheet. Here’s a screenshot of the table in Numbers, where I’m displaying the formula for the first difference, Δ:

Difference table in Numbers

That formula can be filled into all the difference cells, which is why it’s so easy. (I included only the first six rows in the table above because I figured you’d trust me that all the values in the fourth difference column, Δ⁴, are the same.)

The constant values in the fourth difference column mean N is a fourth-degree polynomial in n:

N=a0+a1n+a2n2+a3n3+a4n4

Because the constant fourth difference is 6, we know that

a4=64!=624=14

This comes from the fact that differences are analogous to derivatives, and

d4Ndn4=4321a4= 24a4

Once we know the degree of the polynomial and have a4, we can figure out the other coefficients by solving a set of four simultaneous equations for four different values of n and N. For example:

a0+a11+a212+a313+1414=1 a0+a12+a222+a323+1424=9 a0+a13+a232+a333+1434=36 a0+a14+a242+a343+1444=100

We could use any four of the six Ns we’ve calculated, but the first four are convenient. Let’s solve them in Python using NumPy and the solve function from the linalg submodule. We can do this interactively:

python:
from numpy import np

m = np.array([[1, 1, 1, 1], [1, 2, 4, 8], [1, 3, 9, 27], [1, 4, 16, 64]])
b = np.array([1 - 1**4/4, 9 - 2**4/4, 36 - 3**4/4, 100 - 4**4/4])
np.linalg.solve(m, b)

The last line returns

python:
array([ 1.77635684e-15, -3.99680289e-15,  2.50000000e-01,  5.00000000e-01])

Those first two elements of the solution array are at the lower limit of what a floating point number can be. So we can say

a0=0a1=0a2=14a3=12

Therefore,

N=14n4+12n3+14n2

or, after collecting terms,

N=14n2(n+1)2

which is the formula Ben got in the video.

Doing basically the same thing in Mathematica is

m = {{1, 1, 1, 1}, {1, 2, 4, 8}, {1, 3, 9, 27}, {1, 4, 16, 64}};
b = {1 - 1^4/4, 9 - 2^4/4, 36 - 3^4/4, 100 - 4^4/4};
LinearSolve[m, b]

which returns

{0, 0, 1/4, 1/2}

This is the same as the Python answer but with no need to think about floating point precision.

A third way to solve the simultaneous equations is to do it in a spreadsheet. Here’s how that looks in Numbers:

Simultaneous equations solution in Numbers

where

The spreadsheet uses a combination of MINVERSE and MMULT, functions that are also in Excel and Google Sheets. As with the Python solution, there are floating point artifacts here. They’re mostly hidden by my choice to show only four decimal places, but that -0.0000 for a1 is a clue that these are not exact answers.

You might well ask why I’d bother using Python or Mathematica to solve the simultaneous equations if I already had a spreadsheet open to make the difference table. The answer is that I generally prefer working in an environment where my formulas and expressions are always visible. It makes things easier to debug, and I’m always debugging.


There are other ways to determine the coefficients. My favorite is a step-by-step procedure that simplifies the problem one polynomial degree at a time.

Given that we know a4=1/4 from the difference table above, we can make a new table for

P=N14n4

and its differences. By subtracting the fourth-degree term from N, we expect P to be a third-degree polynomial. Here are the first five rows of the difference table for P:

n P Δ Δ² Δ³
1 0.75 4.25 6.50 3.00
2 5.00 10.75 9.50 3.00
3 15.75 20.25 12.50
4 36.00 32.75
5 68.75

As expected, they become constant at the third difference. Using the same technique we used to get a4, we can say

a3=33!=36=12

Moving on, we make a table for

Q=P12n3

and its differences. Here are the first four rows of that table:

n Q Δ Δ²
1 0.25 0.75 0.50
2 1.00 1.25 0.50
3 2.25 1.75
4 4.00

These become constant at the second difference, and we can say

a2=1/22!=14

Finally, we make a table for

R=Q14n2

which is this:

n R
1 0.00
2 0.00
3 0.00
4 0.00

Since all the R values are zero, the rest of the coefficients, a1 and a0, must be zero, and we’re done.

If it’s not obvious why a1=a0=0, consider this:

R = Na4n4a3n3a2n2 = a0+a1n+a2n2+a3n3+a4n4a4n4a3n3a2n2 = a0+a1n

The only way for R to be zero for all values of n is if a1=a0=0.

Building these tables is fairly easy, but it’s not as fast as building and solving the simultaneous equations. Still, there’s some satisfaction in marching to the answer this way. It’s basically a recursive solution, where we’re simplifying the problem with each step.


Using difference tables to work out the formula for the sum of cubes isn’t as slick as the graphical method shown in the video, but it does have the advantage of not requiring any ingenuity. I’m not opposed to ingenuity, but sometimes you want to just set the problem up, turn the mathematical crank, and get the answer.


Permanent Daylight Saving Time

A couple of days ago, Casey Liss took a break from arguing about temperature scales to tweak me about the recent passage of the Sunshine Protection Act by the House. The Act would make Daylight Saving Time permanent, something Casey knows I disapprove of. A similar bill passed the Senate a few years ago, and Donald Trump has said he will sign this one, so there’s a decent chance it’ll become law. Let’s see what will happen if it does.

First, of course, there will be a lot of cheering from the people who moan about changing their clocks twice a year. Well, some of the moaners will cheer—the ones who wanted to eliminate DST and stay on Standard Time all year will grumble, but they’ll probably still be pleased to be released from that terrible burden.

I’m more interested in the consequences of permanent DST. You may recall my sunrise/sunset plots. Here’s one for Chicago in 2026:

Sunrise-sunset in Chicago

The dirty yellow zones cover the DST period, which currently runs from the second Sunday in March to the first Sunday in November. A small change to the sunplot code extends that zone to the entire year:

Sunrise-sunset in Chicago with permanent DST

I left the Standard Time lines in place for comparison, even though there won’t be any Standard Time if the Act becomes law.

As you can see, there will be a long stretch—more than two months—for which sunrise will be after 8:00.1 I’m sure this won’t bother many of you who don’t do anything before 8:00, but there are lots of people it will bother. And I bet we’ll hear from them, even though a good chunk of them will be from the current cohort of clock-change moaners.

People living near the western edge of a time zone will have even more morning darkness. You may recall my visit to a Louis Sullivan bank in West Lafayette, Indiana, a couple of weeks ago. My photos showed the sun shining on the north side of the bank, which happened because the sun rises late in West Lafayette. (By “late” I mean in local clock time. You could make an argument that the Sun, like Gandalf, is never late. Nor is it early. It rises precisely when it means to.)

Let’s see the sunrise/sunset times in West Lafayette under permanent DST:

Sunrise-sunset in West Lafayette with permanent DST

Basically five months for which the sun never rises before 8:00. And about seven weeks for which it doesn’t rise before 9:00. No one deserves that—not even Boilermakers.


  1. Yes, the graph is just for 2026, but sunrise times don’t change all that much from year to year. 


Floating Saturn calculations

You’ve probably seen somewhere that the density of Saturn is less than that of water. If there were a bathtub big enough to hold it, Saturn would float. I think I first read this in one of Isaac Asimov’s collections of science essays. If you do an image search, you can easily find many illustrations of Saturn floating in water. Most of these show no more than half of Saturn under the water. Could that be right?

Because there were several things I should have been doing this afternoon, I decided to work out how much of Saturn should be underwater in these images. Even better, I’d do the more general problem: how much of any sphere would be submerged in a liquid if the density of the sphere is less than that of the liquid?

Here’s a cross-section of the problem:

Floating sphere

We’ll say the sphere has a radius r, a diameter d=2r, and a uniform density of ρs. The density of the liquid is ρ. Because the sphere floats, ρs<ρ. The distance b is how far the bottom of the sphere is below the liquid surface.

The mechanics of the system is simple: the mass of liquid displaced by the submerged portion of the sphere is equal to the entire mass of the sphere. That is,

ρVb=ρsVs

where

Vb=πb23(3rb)

is the volume of the submerged portion of the sphere and

Vs=43πr3

is the volume of the entire sphere. These expressions are usually given in terms of r, as I’ve shown here, but eventually I want to work out the value of b as a fraction of d.

In fact, since I want to make a plot, which requires pure numbers, let’s nondimensionalize our variables by saying

β=brandρ=ρsρ

Putting all this together and doing a little algebra, we get

rρ(β33β2+4ρ)=0

Since neither r nor ρ is zero, the expression in the parentheses must be. In other words,

ρ=14(3β2β3)

Since 0<β2, the right-hand side of the equation must be greater than zero, so we don’t have to worry about negative densities.

Typically, we’d want to calculate β for a given value of ρ, so this equation isn’t in the most useful form. But it’s easy to plot values using this equation, even if we do want ρ to be plotted on the horizontal axis.

I mentioned earlier that I prefer to show the depth b as a fraction of the diameter, not the radius, i.e.,

bd=b2r=β2

Here’s that plot, which comes out in a sigmoidal shape:

Floating sphere plot

To figure out how much of Saturn would be under the water, we need Saturn’s density, which we can find on NASA’s old Saturn Fact Sheet, as archived on the Wayback Machine. It’s 687kg/m3, which means our density ratio is 0.687. Looking that up in the plot, we see that the submerged portion of Saturn is between 60% and 65% of its diameter. A quick numerical solution gives us 62.7% of the diameter. Only 37.3% would be high and dry.

So those many illustrations showing more than half of Saturn’s diameter sticking out above the water are all wet.1 Of course, just seeing that Saturn’s specific gravity was over 0.5 is enough to know that most of it is underwater, but now we can put a number on it.


  1. Yes, I went there. 


Plotting of and by students

I saw this article at Inside Higher Ed this morning, guided by a Mastodon post from Techmeme. The title of the article is “Brown Professor Suspects Majority of His Class Used AI to Cheat,” so if you’re sick to death of reading about AI—pro, con, or caveated—don’t feel obligated to follow the link. I’m interested in a plot included in the article more than the article itself.

An economics professor gave his class a take-home midterm, and the grades on it were much higher than usual. He suspected the high marks came from the students using LLMs to answer the questions, so the final exam was done in class and the marks were generally much lower. Here’s the plot given in the article:

Test grades by student from IHE

Image from Inside Higher Ed.

Let me start by saying I have no criticisms of the plot, just some comments about things that struck me.

First, the upper portion of the chart made me think the students, S1 through S59, were ordered according to their score on the final exam (the gray dots and figures). But as you go down the list, you soon see that that isn’t the case. After reading past the chart, I saw that the professor decided to throw out the results of the midterm and use the final exam as 80% of the course grade. Presumably, the students were sorted by their course grade.

More important, though, was the chart’s layout. When plotting a pair of scores for every student, the usual convention would be to have the students (the categories) laid out along the horizontal axis and their scores (the values) plotted on the vertical axis. This does it the other way around. There’s nothing wrong with doing it that way; it’s just unusual. Sort of like seeing a time series chart in which time is on the vertical axis. There can be good reasons to do it, but usually people don’t.

I first read the article on my phone, so I wondered if the layout was driven by the aspect ratio of most phones in portrait mode. In fact, since the chart is not actually an image but some sort of JavaScript thingy from Datawrapper. At least I think that’s what it is—I couldn’t select the chart as an image, and when I looked at the page’s HTML, I saw it was in an <iframe> element.

This made me wonder if the chart would flip to a more conventional layout if the aspect ratio of the browser were different. Turning my phone to landscape mode didn’t flip the axes, nor did opening the page on my MacBook Pro with a wide Safari window. Clearly the author of the article, Emma Whitford, thought it was best to have the students running down the vertical axis.

I decided to see what a more conventional layout would look like. I used the link on the page to download the plot’s data as a CSV file—a very thoughtful addition to the article and something I wish more authors did—and whipped out a quick plot in Matplotlib. Here it is:

Midterm and final exam results

Even in a wide browser window, it’s pretty tightly constrained, mainly because I have a width limit on the content portion of ANIAT (that’s to keep lines of text of reasonable length). If you click on the chart, it’ll open to the full width of your browser window, which will make it easier to peruse.

Here’s the code that produced the chart:

python:
 1:  #!/usr/bin/env python3
 2:  
 3:  import pandas as pd
 4:  import numpy as np
 5:  import matplotlib.pyplot as plt
 6:  from matplotlib.ticker import MultipleLocator, AutoMinorLocator
 7:  
 8:  # Read in the exam scores
 9:  df = pd.read_csv('scores.csv')
10:  
11:  # Create the plot with a given size in inches
12:  fig, ax = plt.subplots(figsize=(12, 6))
13:  
14:  # Bar colors are based on whether midterm was higher than final
15:  colors = ['#0571b0']*59
16:  for i in range(59):
17:    if df.Final[i] > df.Midterm[i]:
18:      colors[i] = '#ca0020'
19:  
20:  # Plot the scores as columns between the final and midterm scores
21:  ax.bar(df.Student, df.Midterm-df.Final, bottom=df.Final, width=.5, color=colors, zorder=10)
22:  
23:  # Set the limits
24:  plt.xlim(xmin=0, xmax=60)
25:  plt.ylim(ymin=0, ymax=100)
26:  
27:  # Set the major and minor ticks and add a grid
28:  ax.xaxis.set_major_locator(MultipleLocator(5))
29:  ax.xaxis.set_minor_locator(AutoMinorLocator(5))
30:  ax.yaxis.set_major_locator(MultipleLocator(20))
31:  ax.yaxis.set_minor_locator(AutoMinorLocator(2))
32:  ax.grid(linewidth=.5, axis='x', which='both', color='#dddddd', linestyle='-', zorder=0)
33:  ax.grid(linewidth=.5, axis='y', which='both', color='#dddddd', linestyle='-', zorder=0)
34:  
35:  # Title and axis labels
36:  plt.title('Final to midterm exam result ranges')
37:  plt.xlabel('Student ID')
38:  plt.ylabel('Score')
39:  
40:  # Make the border and tick marks 0.5 points wide
41:  [ i.set_linewidth(0.5) for i in ax.spines.values() ]
42:  ax.tick_params(which='both', width=.5)
43:  
44:  # Add a note
45:  ax.text(5, 25, 'Midterms were higher than finals except for Student 22', va='center')
46:  
47:  # Save as PDF
48:  plt.savefig('20260709-Midterm and final exam results.png', format='png', bbox_inches='tight', dpi=150)

A few comments on this:

Overall, I think my chart works, and I had fun thinking about how to make it. But it’s not better than the original.

Update 10 Jul 2026 5:07 AM
Sometimes I just want to be done with a post, and I publish it before I should. That’s what happened last night. While it’s true that categories are usually laid out horizontally, I shouldn’t have left the impression that it’s tremendously rare for them to be laid out vertically. There are plenty of good vertical examples.

A clear reason for a vertical layout is a large number of categories, and while 59 categories isn’t especially large, it’s definitely heading in that direction. I made my chart mainly to see if a horizontal layout can work with 59 categories, and I think it can—at least if you can give your plot enough horizontal space.

(Another good reason for a vertical layout is that it works better typographically. Sometimes the categories have long names, and they fit better in a column than in a row. That isn’t the case here, but it happens.)

Thanks to Janne Moren for making me realize that this post was too blunt as originally written.