Problem 45 : Triangular, pentagonal, and hexagonal
Problem Statement
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle: Tn=n(n+1)/2 1, 3, 6, 10, 15, ā¦
Pentagonal: Pn=n(3nā1)/2 1, 5, 12, 22, 35, ā¦
Hexagonal: Hn=n(2nā1) 1, 6, 15, 28, 45, ā¦
It can be verified that T285 = P165 = H143 = 40755.
Find the next triangle number that is also pentagonal and hexagonal.
Solution
def pentagonal(n):
if (1+(24*n+1)**0.5) % 6 == 0:
return True
return False
def hexagonal(n):
if (1+(8*n+1)**0.5)%4 == 0:
return True
return False
flag =True
while flag:
for i in range(1,10**6):
a=int(i*(i+1)/2)
if hexagonal(a) and pentagonal(a):
if a > 40755:
print(a)
flag=False
break
Output
1533776805