#!/usr/bin/env python3 # Pentagonal numbers are generated by the formula, Pn=n(3nāˆ’1)/2. The first ten pentagonal numbers are: # # 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... # # It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 āˆ’ 22 = 48, is not pentagonal. # # Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk āˆ’ Pj| is minimised; # what is the value of D? from projecteuler import is_pentagonal, timing @timing def p044() -> None: found = 0 n = 2 # Check all couples of pentagonal numbers until the right one # is found. Use the function implemented in projecteuler.py to # check if the sum and difference ot the two numbers is pentagonal. while not found: pn = n * (3 * n - 1) // 2 for m in range(1, n): pm = m * (3 * m - 1) // 2 if is_pentagonal(pn+pm) and is_pentagonal(pn-pm): found = 1 break n = n + 1 print('Project Euler, Problem 44') print(f'Answer: {pn - pm}') if __name__ == '__main__': p044()