﻿'''
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.
'''

import math

def trianglenumber(i): #start at 285
  t = i * (i + 1) /2
  return t

def checkpent(n):
  pent = ((1/2) + math.sqrt((1/4) + 6 * n)) / 6
  if pent == int(pent):
    return True
  return False

def checkhex(n):
  hex = (1 + math.sqrt(1 + 4 * n)) / 4
  if hex == int(hex):
    return True
  return False

searching = True
i = 285
while searching:
  i += 1
  triangle = trianglenumber(i)
  if checkhex(triangle) and checkpent(triangle):
    searching = False

print triangle