#45 - Triangular, pentagonal, and hexagonal

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

TypeFormulaFirst terms
Triangle$T_n=\frac{1}{2}n(n+1)$1, 3, 6, 10, 15, …
Pentagonal$P_n=\frac{1}{2}n(3n-1)$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.


Just like how hexagons are composed of triangles, hexagonal numbers are themselves triangle numbers:

\(H_n=n(2n-1)=\frac{1}{2}(2n)(2n-1)=\frac{1}{2}(2n-1)((2n-1)+1)=T_{2n-1}\) One equality pair is already done, so now we only have to find the next hexagonal number which is also a pentagonal number. Using the same function as #44 - Pentagonal numbers, we can set up a single loop starting from $H_{143}$ going on up.

# file: "problem045.py"
n = 144  # H_143 is pentagonal
hexNum = n * (2 * n - 1)
while not isPentagonal(hexNum):
    n += 1
    hexNum = n * (2 * n - 1)

print(n, hexNum)

Running this quick loop results in,

27693 1533776805
0.03727003511300319 seconds.

Therefore, $H_{27693} = \mathbf{1533776805}$ is the next number which is triangular, pentagonal, and hexagonal.