Python Implementation of Problem 44

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 44

Problem:

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?

python.src.p0044.main() int
 1"""
 2Project Euler Problem 44
 3
 4Problem:
 5
 6Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are:
 7
 81, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
 9
10It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.
11
12Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = ``|Pk − Pj|``
13is minimised; what is the value of D?
14"""
15from itertools import islice
16
17from .lib.math import is_pentagonal, pentagonal
18
19
20def main() -> int:
21    D = 1_000_000_000_000
22    pentagonals = [pentagonal(x) for x in range(1, 2_500)]
23    for idx, k in enumerate(pentagonals):
24        for j in islice(pentagonals, idx):
25            if is_pentagonal(j + k) and is_pentagonal(k - j):
26                D = min((D, abs(k - j)))
27    return D

Tags: figurate-number