'''
Triangular, pentagonal, and hexagonal
Problem 45 
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.
'''

from math import sqrt


def is_triangle(num):
    d = sqrt((1-4*(-num*2)))
    x = (-1 + d) / 2
    if int(x) == x:
        return True


def is_pentagonal(num):
    d = sqrt((1+24*num))
    x = (1 + d) / 6
    if int(x) == x:
        return True


def is_hexagonal(num):
    d = sqrt((1 + 8 * num))
    x = (1 + d) / 4
    if int(x) == x:
        return True

start = 143

while True:
    start += 1
    next_hexagonal = start * (2 * start - 1)

    if is_triangle(next_hexagonal) and is_pentagonal(next_hexagonal) and is_hexagonal(next_hexagonal):
        print("Found:", next_hexagonal)
        break

