#!/usr/bin/env python3 # 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 T_285 = P_165 = H_143 = 40755. # # Find the next triangle number that is also pentagonal and hexagonal. from projecteuler import is_pentagonal, timing @timing def p045() -> None: found = 0 i = 143 while not found: i = i + 1 # Hexagonal numbers are also triangle numbers, so it's sufficient # to generate hexagonal numbers (starting from H_144) and check if # they're also pentagonal. n = i * (2 * i - 1) if is_pentagonal(n): found = 1 print('Project Euler, Problem 45') print(f'Answer: {n}') if __name__ == '__main__': p045()