-3

"The first two numbers that are both squares and triangles are 1 and 36. Find the next one and, if possible, the one after that. Can you figure out an efficient way to find triangular–square numbers? Do you think that there are infinitely many?"

Thanks.

Ross Millikan
  • 383,099

1 Answers1

1

$${t_n} = \sum\limits_{k = 1}^n k = 1 + 2 + 3 + \cdots + n = \frac{{n\left( {n + 1} \right)}}{2} = n-th{\text{ triangular number}}$$ $${s_m} = {\text{m-th square number}} = {m^2}$$ $${s_m} = {t_n} \Rightarrow \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$$ $$\boxed{w \equiv 2n + 1}$$ $$\boxed{z \equiv 2m}$$ $$\boxed{{w^2} - 2{z^2} = 1}$$

Finding numbers that satisfy the last equation above isn't all that simple... so I found the first numbers that are both triangular and square using python

import pandas as pd
import numpy as np
triangular_and_square = []
for n in np.arange(1,10000):
    w = 2*n + 1
    for m in np.arange(1,10000):
        z = 2*m
        if w*w - 2*z*z - 1 == 0:
            triangular_and_square.append([m*m,m,n])
output_dataframe = pd.DataFrame(triangular_and_square,
                                 columns = ["Square (m-squared) + Triangular","m","n"], 
                                 index = [1,2,3,4,5,6])

print(output_dataframe)

Code output

$$\begin{array}{*{20}{c}} & {{\rm{Square (}}{m^2}){\rm{ and Triangular}}}&& m&& n& \\ \hline & 1&& 1&& 1& \\ & {36}&& 6&& 8& \\ & {1225}&& {35}&& {49}& \\ & {41616}&& {204}&& {288}& \\ & {1413721}&& {1189}&& {1681}& \\ & {48024900}&& {6930}&& {9800}& \end{array}$$

  • Your code has both triangular_and_square and triangluar_and_square. Please could you check it for typos and make sure it is what you intend? – Rosie F Oct 15 '17 at 18:19
  • Oh wow. Didn't even notice I did that. triangluar_and_square = [] <---- a list triangluar_and_square.append([m*m,m,n]) <--- adding values to the list triangular_and_square <---- a dataframe triangular_and_square = pd.DataFrame(triangluar_and_square.... <---- I used the list _triangluar_and_square to create the dataframe triangular_and_square.

    I should've used different names for the dataframe and the list. The old version worked fine., but I changed the name of the dataframe to avoid confusion. Sorry about my sloppy original answer.

    – Dave Rosenman Oct 16 '17 at 19:13