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.


In [1]:
import math

def is_pentagonal(x):
    # See test for pentagonal numbers
    # https://en.wikipedia.org/wiki/Pentagonal_number
    n = math.sqrt(24 * x + 1) + 1
    return n/6 == n//6

def is_hexagonal(x):
    # See test for pentagonal numbers
    # https://en.wikipedia.org/wiki/Hexagonal_number
    n = math.sqrt(8 * x + 1) + 1
    return n/4 == n//4

def search(start_at=285):
    n = start_at
    while True:
        T_n = n * (n+1) // 2
        if is_pentagonal(T_n) and is_hexagonal(T_n):
            return (T_n, n)
        n += 1

In [2]:
search(start_at=286)


Out[2]:
(1533776805, 55385)

In [ ]: