Triangular Squares

In a recent video Matt Parker showed a triangular number that also is a square number, 6, and asked if there were more.

A triangular number has the form \frac{n^2+n}{2} — shown by Euler — and a square number has the form m^2.
Triangular squares are those numbers for which \frac{n^2+n}{2} = m^2 with n,m \in \mathbb{N}.
Examples are \{0, 1, 6, 35, 204, 1189, 6930, \dots\} (sequence A001109 in OEIS).

To check if triangular numbers are square numbers is easy (code listed below), but a mathematical function would be nicer.
The first thing I tried was to define the triangular number’s square root as a whole number, \sqrt{\frac{n^2+n}{2}} = \lfloor \sqrt{\frac{n^2+n}{2}} \rfloor. This function does not return the square numbers that are triangular but the triangular numbers that are square.
The resulting sequence is \{0, 1, 8, 49, 288, 1681, 9800, \dots\} (sequence A001108 in OEIS).

# Python 2.7.7 Code
# Jonathan Frech 13th of July, 2016
#         edited 15th of July, 2016

# Triangular square numbers
# a*(1+a)/2 == b**2

# Mathematica Code
# n=0;p=1;While[True,n+=p;p++;If[Mod[Sqrt[n],1]==0,Print[n]]]


# def a(n):
# 	if n in [1, 0]:
# 		return n
# 	return 6*a(n-1)-a(n-2)

# Mathematica Code
# a[0]=0;a[1]=1;a[n_]:=6*a[n-1]-a[n-2]

# import math module
import math

# variables to calculate triangular numbers
n = 0
p = 1

# while loop
while True:
	# test if triangular number is a square number
	if math.sqrt(n) % 1 == 0:
		print int(math.sqrt(n))

	# calculate triangular number
	n += p
	p += 1

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s