"""
Project Euler Problem 45
========================

Triangle, pentagonal, and hexagonal numbers are generated by the following
formulae:

Triangle     T[n]=n(n+1)/2   1, 3, 6, 10, 15, ...
Pentagonal   P[n]=n(3n-1)/2  1, 5, 12, 22, 35, ...
Hexagonal    H[n]=n(2n-1)    1, 6, 15, 28, 45, ...

It can be verified that T[285] = P[165] = H[143] = 40755.

Find the next triangle number that is also pentagonal and hexagonal.
"""

def get_tri(n):
    return n*(n+1)/2
def is_pent(n):
	return not ((24*n+1)**0.5 + 1) / 6 % 1
def is_hex(n):
    return not ((8*n+1)**0.5 + 1) / 4 % 1 

t = 286 
target = get_tri(t)

while not(is_pent(target) and is_hex(target)):
    t += 1
    target = get_tri(t)
print target