Skip to main content
2 of 5
added 141 characters in body
MT0
  • 1k
  • 5
  • 8

You can find the solution without any loops:

The xth triangle number is given by:

$$\frac{x(x+1)}{2}$$

So the sum n of the square of two consecutive triangular numbers is given by:

$$\begin{align} n & = \left(\frac{x(x+1)}{2}\right)^2 + \left(\frac{(x+1)(x+2)}{2}\right)^2 \\ & = \frac{x^2(x+1)^2 + (x+1)^2(x+2)^2}{4}\\ & = \frac{(x+1)^2(2x^2+4x+4)}{4} \\ & = \frac{(x+1)^2(x^2+2x+1 + 1)}{2} \\ & = \frac{(x+1)^2((x+1)^2 + 1)}{2} \\ \text{let } i = (x+1)^2 \implies n & = \frac{i(i+1)}{2} \\ \Leftrightarrow 0 & = i^2 + i - 2n \end{align}$$

Then using Pythagoras theorem:

$$\begin{align}i=\frac{-1 + \sqrt{1+8n}}{2}\\ \implies x = \sqrt{\frac{-1 + \sqrt{1+8n}}{2}} - 1 \end{align}$$

If n = 45 then

$$ x = \sqrt{\frac{-1 + \sqrt{1+8 \cdot{} 45 }}{2}}-1 = 2$$

Which is an integer so n=45 is a valid sum of the square of the 2nd (since x=2) and 3rd triangular numbers.

If n = 6 then

$$ x = \sqrt{\frac{-1 + \sqrt{1+8 \cdot{} 6 }}{2}}-1 = \sqrt{3}$$

Which is not an integer so n=6 is not a valid sum of the squares of two consecutive triangular numbers.

MT0
  • 1k
  • 5
  • 8