Triangular numbers between square numbers

James Tanton asked on Twitter:

As there are “more” triang nmbrs than sq nmbrs let f(N) = nmbr triangs >= N^2 but < (N+1)^2. Curious:What graph like?

The kth triangular number is about k^2/2 (more precisely, it’s (k^2+k)/2.) So there are about \sqrt{2} n triangular numbers less than n^2. Therefore, “on average”, in each interval [N^2, (N+1)^2) there are \sqrt{2} triangular numbers.

For example, in the interval [9, 16) there are two triangular numbers, namely 10 and 15; this is f(3). In the interval [16, 25) there is one triangular number, namely 21; this is f(4).

Let’s write down an explicit formula for f(n). Let g(x) be the number of triangular numbers less than x. To figure this out, I’ll introduce a function t(x), which takes as input x and outputs the index of x in the triangular-number sequence. For example, t(10) = 4, t(15) = 5. But we also want to be able to compute, say, t(12). But that’s fine! t(n) is just the inverse of the function which takes n to the nth triangular number, the function n \to (n^2+n)/2; in particular, solving the quadratic,

t(n) = {\sqrt{8n+1}-1 \over 2}.

So t(10) = (\sqrt{81}-1)/2 = (9-1)/2 = 4; t(12) = (\sqrt{97}-1)/2 \approx 4.42.

Next we write g(x) in terms of t(x). It’s tempting to say that g(x) = \lfloor t(x) \rfloor, but it’s not. t(10) = 4, for example, but we want g(10) = 3. We’ll say that g(x) = \lfloor t(x-1/8) \rfloor — we’ll only need this formula to work when x is an integer. So, for example, g(10) = \lfloor t(9.875) \rfloor, and the index of 9.875 in the triangular number sequence, whatever that means, is between 3 and 4. But g(11) = \lfloor t(10.875) \rfloor = 4.

Why the constant 1/8? Because

t(x-1/8) = {\sqrt{8x}-1 \over 2} = {\sqrt{2x}} - {1/2}

which makes the formula marginally easier to write.

Finally f(n) = g((n+1)^2) - g(n^2). Take the number of triangular numbers less than (n+1)^2, and subtract the number less than n^2, and you get the number in the interval in between. For example g(5^2) = \lfloor \sqrt{50} - 1/2 \rfloor = 6; there are 6 triangular numbers less than 25, namely 1, 3, 6, 10, 15, and 21. And g(4^2) = \lfloor \sqrt{32} - 1/2 \rfloor = 5. Thus f(4) = g(5^2) - g(4^2) = 6-5 = 1, indicating the triangular number 21. So at long last we have the formula

f(n) = \lfloor (n+1) \sqrt{2} - {1 \over 2} \rfloor - \lfloor n \sqrt{2} - {1 \over 2} \rfloor.

In particular the arguments of these two floor functions differ by \sqrt{2}, which is between 1 or 2, so f(n) is always either 1 or 2. The graph that Tanton asked about is below.

You can see some hints of periodicity in the function; for example, from a quick glance at the graph it might look like f(x) has period 12, each period containing five 2s and seven 1s. But this can’t hold, not unless \sqrt{2} = 17/12. In fact f(x) can’t be periodic, because \sqrt{2} is irrational.

One thought on “Triangular numbers between square numbers

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s