# Triangular, pentagonal, and hexagonal (Euler Problem 45)¶

Back to overview.

https://projecteuler.net/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 T285 = P165 = H143 = 40755.

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

In [1]:

    def T(n):
        return n * (n + 1) // 2

    assert(T(3) == 6)

    def P(n):
        return n * (3 * n - 1) // 2

    assert(P(3) == 12)

    def H(n):
        return n * (2 * n - 1)

    assert(H(3) == 15)

In [2]:

    n_max = 100000
    s = 0

    T_list = [T(n) for n in range(1, n_max + 1)]
    P_set = {P(n) for n in range(1, n_max + 1)}
    H_set = {H(n) for n in range(1, n_max + 1)}

    for n in range(1, n_max + 1):
        if T_list[n - 1] in P_set and T_list[n - 1] in H_set:
            t = T_list[n - 1]
            if t > 40755:
                s = t

In [3]:

    print(s)
    assert(s == 1533776805)

    1533776805

In [ ]: