Using Python To Find Square-Triangular Numbers

A number is triangular if it satisfies the formula \frac{n(n+1)}{2} for some positive integer n. The square-triangular numbers are numbers that are both triangular and the square of a positive integer.

Letting the n-th triangular number equal

\frac{{n\left( {n + 1} \right)}}{2}

…and the square of the m-th positive integer equal m^2… it follows that a positive integer is both triangular and a square if

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

for some value of n and some value of m. Finding pairs of values of m and n that satisfy this equation is not easy by hand. The following manipulations of the equations above allow for the use of a simple python algorithm to find square-triangular numbers.

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

\frac{1}{2}{\left( {n + \frac{1}{2}} \right)^2} = \frac{1}{2}\left( {{n^2} + n + \frac{1}{4}} \right) = \frac{1}{2}\left( {{n^2} + n} \right) + \frac{1}{8}

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

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

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

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

{\left( {2n + 1} \right)^2} - 8{m^2} = 1

{\left( {2n + 1} \right)^2} - 2 \cdot {\left( {2m} \right)^2} = 1

{\left( {2n + 1} \right)^2} - 2 \cdot {\left( {2m} \right)^2} - 1 = 0

The following two definitions will help simplify the equation above:

w \equiv 2n + 1

z \equiv 2m

With the definitions of w and z above, {\left( {2n + 1} \right)^2} - 2 \cdot {\left( {2m} \right)^2} = 1 can be re-written as

{w^2} - 2{z^2} - 1 = 0

Finding values of w and z that satisfy the equation above is not simple, but solutions can be found easily in Python. The following short algorithm finds the first 6 square-triangular numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *