# 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.

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


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


# all hexagonal numbers are triangular numbers, so we can ignore calculating tri nums
# T(2n - 1) == H(n)
hexa = set()

count = 144  # if we started lower we would find the given 40755

# pentagonal numbers increase slower than hexagonal,
# so we just check if the pentagonal number is in the list of hexagonal numbers
while nth_pentagonal(count) not in hexa:
    hexa.add(nth_hexagonal(count))
    count += 1

print(nth_pentagonal(count))  # 1533776805
