Computing tetrahedral numbers

I left off the discussion of tetrahedral numbers in my last post by posing the question of what the 144th tetrahedral number is, and promising to explain a quick way to figure it out without having to actually add up the first 144 triangular numbers. I still intend to do that — but first, I’ll demonstrate how we can use a computer to calculate it for us. Although it would be incredibly tedious and error-prone for a human to calculate and add up the first 144 triangular numbers, any modern computer can do it in a fraction of a second (mine can do it in about three milliseconds).

I’ll use the programming language J, which is completely free and is great for playing around with math. If you’ve never programmed a computer before, don’t worry — J isn’t much like other programming languages; although it does have a lot of advanced features, it’s pretty easy and intuitive to get started. It mostly just acts like a really sophisticated calculator. If you want to try downloading it to play around with yourself (go ahead, it’s fun!), the website is; here’s a link to the download page. Some books and reference materials can be found here (I haven’t read most of them, so I can’t vouch for the difficulty level/quality.)

At its core, J acts just like a calculator: you type in expressions, and it evaluates them. In all of the following examples, anything indented three spaces is what you type, and anything not indented is the answer printed by J.

   8 % 5

Obviously, J can do addition, subtraction, multiplication, and division. It can also do such things as factorial (!). (Of course, it can do tons of other things too — like exponentiation, sine and cosine, and derivatives, to name just a few. Here’s a complete list, just to boggle your brain.) A few things to note about the above examples: first, J uses an underscore to represent negative numbers (e.g. _7), and a percent sign to represent division — a bit different than your calculator, but easy to get used to. Notice also that using an ‘x’ after the 20 in the above example made J calculate 20 factorial exactly, instead of using scientific notation.

Okay, so how are we going to use J to calculate the 144th tetrahedral number? I’ll start by illustrating the process of calculating the 12th tetrahedral number, and we can easily extend it. We’ll start here:

   i. 12
0 1 2 3 4 5 6 7 8 9 10 11

The i. function creates a list of integers from zero to one less than the number you give it. So typing i. 12 prints all the numbers from zero to 11. Okay, but what we really want is all the numbers from 1 to 12, not 0 to 11. No problem: we’ll use the >: function, which adds one to whatever number you give it. As with most functions that operate on numbers, if you give it a list of numbers, it will simply operate on each number in the list separately.

   >: i. 12
1 2 3 4 5 6 7 8 9 10 11 12

Now we have the first twelve whole numbers. But what we really want is the first twelve triangular numbers, so we can add them up. Recall that triangular numbers are formed by adding up consecutive whole numbers (1, 1+2, 1+2+3, and so on).

Here’s a start:

   +/ >: i.12

What happened? You may recall that 78 is the twelfth triangular number. The +/ added up our list of numbers from 1 to 12! In general, putting a frontslash after a function causes that function to be applied throughout an entire list. For example, +/ 1 2 3 4 5 is the same as 1 + 2 + 3 + 4 + 5. As another example, we could do something like this:

   */ >: i.12

Apparently, 479001600 is the product of all the numbers from 1 through 12 — as we can confirm by finding the factorial of 12 directly. Anyway, back to triangular numbers. We used the list of numbers from 1 to 12 to find the 12th triangular number; but what we really want is all the triangular numbers from the first to the 12th! Well, there’s an easy way to do that too. If you put a backslash after a function, it causes the function to be applied to all the prefixes of a list. That is, it will be applied to the first thing in the list; then to the first two things in the list; then to the first three things… and so on. For example:

   +/\ >: i. 12
1 3 6 10 15 21 28 36 45 55 66 78

Got that? The >: i. 12 part, of course, generates the list of numbers from 1 to 12. We know that +/ means to add up all the numbers in a list. So writing +/\ means add up the first number in the list; then add up the first two numbers in the list; then add up the first three numbers in the list; and so on. So we get 1, 1+2, 1+2+3, … the first twelve triangular numbers!

We’re almost there… the nth tetrahedral number, of course, is just the sum of the first n triangular numbers. So now that we know how to create a list of the first n triangular numbers, all we have to do is add them up (using +/ again, of course):

   +/ +/\ >: i.12
   +/ +/\ >: i.144

Obviously we can just replace the 12 with 144 to find the 144th triangular number — 508,080!

Fun, huh? Even this method has its limits, though — using J to compute the nth tetrahedral number actually causes your computer to store a list of the first n whole numbers in its memory, which is fine for values of n like 12 or 144. But for values like one million, it takes a while, and for something like one trillion, it would be rather hopeless. But never fear — with a bit of slick mathematics (isn’t it all?), you’ll soon know how to compute the one millionth tetrahedral number for yourself using pencil and paper*!

*(Why you’d ever need to do this, I have no idea. But shush, that’s not the point.)

About Brent

Associate Professor of Computer Science at Hendrix College. Functional programmer, mathematician, teacher, pianist, follower of Jesus.
This entry was posted in computation, famous numbers, geometry, sequences. Bookmark the permalink.

3 Responses to Computing tetrahedral numbers

  1. Congratulations, you are rekindling my interest in this sort of thing.

  2. Jonathan says:

    Brent, just wanted to let you know I read here every so often, and I love what you’re doing.

  3. Pingback: Triangular number formula (Challenge #8) | The Math Less Traveled

Comments are closed.