/
problem044.py
40 lines (30 loc) · 1.17 KB
/
problem044.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Pentagonal numbers are generated by the formula Pn = n*(3*n-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 common_funcs import pentagon, pentagonal, answer
import math
# This is not a rigorous solution to the problem as far as I
# can see. I am not minimizing D explicitly, only finding the first
# instance where j-k = j+k counting up from j. Since Pn increases at an
# increasing rate, I am guessing this will be the solution, because if Pj grows
# large enough then differences between Pj and Pk will become even
# larger. Should revisit this later.
def solve():
i = 1
not_found = True
while True:
i += 1
j = pentagon(i)
for m in range(i-1,0,-1):
k = pentagon(m)
if pentagonal(j-k) and pentagonal(j+k):
return abs(j-k)
break
answer(solve)