problem 45 https projecteuler netproblem45 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 3 n 1 2 1 5 12 22 35 hexagonal hn n 2 n 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 all triangle numbers are hexagonal numbers t2n1 n 2 n 1 hn so we shall check only for hexagonal numbers which are also pentagonal returns nth hexagonal number hexagonalnum143 40755 hexagonalnum21 861 hexagonalnum10 190 returns true if n is pentagonal false otherwise ispentagonal330 true ispentagonal7683 false ispentagonal2380 true returns the next number which is triangular pentagonal and hexagonal solution144 1533776805 returns nth hexagonal number hexagonal_num 143 40755 hexagonal_num 21 861 hexagonal_num 10 190 returns true if n is pentagonal false otherwise is_pentagonal 330 true is_pentagonal 7683 false is_pentagonal 2380 true returns the next number which is triangular pentagonal and hexagonal solution 144 1533776805
def hexagonal_num(n: int) -> int:
    
    return n * (2 * n - 1)


def is_pentagonal(n: int) -> bool:
    
    root = (1 + 24 * n) ** 0.5
    return ((1 + root) / 6) % 1 == 0


def solution(start: int = 144) -> int:
    
    n = start
    num = hexagonal_num(n)
    while not is_pentagonal(num):
        n += 1
        num = hexagonal_num(n)
    return num


if __name__ == "__main__":
    print(f"{solution()} = ")