Check whether or not a triangular number is triangular is the square-sum of two other consecutive triangular numbers

I'm trying to write a program that would tell me whether or not a triangular number, a number of the form $\frac{(n)(n+1)}{2}$ is the sum of the squares of two other consecutive triangular numbers. It is guaranteed that the given $n$ is triangular. On oeis.org it gives a formula two calculate the nth number which satisfies the above, but no where can I find how to check whether or not a number satisfies the above.

I know this may not be the right place to post this, but I wanted a more mathematical answer to this.

• @YvesDaoust No it is not, but if you give it an input of a triangular number, I need to check whether or not there exist two consecutive triangular numbers whose sum of squares is that $n$ – user330602 Apr 11 '16 at 20:34
• I have updated my answer to be in line with your question – sanketalekar Apr 11 '16 at 20:37

$$8\frac{n(n+1)}2+1=\left(2n+1\right)^2.$$ Check if $8m+1$ is a perfect square. (By taking its square root.)

Update:

The question is about numbers that are the sum of two consecutive triangular numbers, i.e. which are of the form

$$m=\left(\frac{(n-1)n}2\right)^2+\left(\frac{n(n+1)}2\right)^2=\frac{n^2(n^2+1)}2.$$

By the above criterion, $$8m+1=(2n^2+1)^2$$ must be a perfect square and its square root $r$ must be such that $\dfrac{r-1}2$ is a perfect square.

In other words,

$$\sqrt{\frac{\sqrt{8m+1}-1}2}\in\mathbb N.$$

• Interesting approach, I'm guessing it's a version of my approach below. How did you know to express it in terms of $(2n + 1)^2$ ? – sanketalekar Apr 11 '16 at 20:22
• @sanketalekar: completing the square, then turning the expression to integer. – Yves Daoust Apr 11 '16 at 20:23
• How does this work for $n=6$. It isn't supposed to work. – user330602 Apr 11 '16 at 20:23
• @user330602: as far as I know, $49$ is a perfect square. – Yves Daoust Apr 11 '16 at 20:24
• Yes, but you cannot find two triangular numbers whose sum of squares is 6 – user330602 Apr 11 '16 at 20:25

First Let's take this step by step to figure out the conditions for an number being triangular:

1. For a number to be triangular, it needs to be of the form $k = \frac{n(n+1)}{2}$

2. If we simplify this, we get the equation $2k = n^2 + n$ which becomes $n^2 + n - 2k = 0$

3. By the Quadratic Formula, you get $n = \frac{-1 \pm \sqrt{1 + 8k}}{2}$ . Let's ignore the "-" of the $\pm$ as n is non-negative here so we get $n = \frac{-1 + \sqrt{1 + 8k}}{2}$

4. We know n has to be a positive integer, so 1 + 8k has to be a perfect square, and the $\sqrt{1+8k} + 1$ as to be even (as it's divided by 2) so $\sqrt{1+8k}$ has to be odd. So $1 + 8k$ has to be odd, which is true regardless of k.

5. So for a number k to be triangular, $1+8k$ has to be a perfect square, and k has to be greater than 0 (or 0 if you consider 0 triangular).

Now let's look at the conditions for a number being the square-sum of two consecutive triangular numbers. Let's consider two numbers $\frac{n(n-1)}{2}$ and $\frac{n(n+1)}{2}$

$(\frac{n(n-1)}{2})^2 +(\frac{n(n+1)}{2})^2 = \frac{n^4 -2n^3 + n^2 + n^4 + 2n^3 + n^2 }{4} = \frac{2*(n^4 + n^2)}{4} = \frac{n^2(n^2+1)}{4}$

The sum of squares of any two consecutive triangular numbers formed by n,n-1 and n+1,n is also a triangular number, with the "n" being n^2.

So for k to be triangular so that it's "n" is a perfect square

$\frac{-1 + \sqrt{1 + 8k}}{2}$ also has to be a perfect square.

So we have our conditions for k:

1. $1+8k$ has to be a perfect square, and k has to be greater than or equal to 0.

2. $\frac{-1 + \sqrt{1 + 8k}}{2}$ also has to be a perfect square.

We want to know if $N$ is a triangular number, in other words we want to know if there is $m$ so that $m(m+1)=2N$.

Notice that if $m=\sqrt{2n}$ we get $m(m+1)=2n+\sqrt{2n}>n$.

Notice if $m=\sqrt{2n}-1$ we get $m(m+1)=2n-\sqrt{2n}<n$

So we only have to try with numbers in the range $(\sqrt{2n}-1,\sqrt{2n})$, so we need only check if $\lfloor\sqrt{2n}\rfloor(\lfloor\sqrt{2n}\rfloor+1)=2n$