-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path044.py
More file actions
45 lines (33 loc) · 1.12 KB
/
044.py
File metadata and controls
45 lines (33 loc) · 1.12 KB
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
41
42
43
44
45
"""
Problem 44
==========
Pentagonal numbers are generated by the formula, P[n]=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 P[4] + P[7] = 22 + 70 = 92 = P[8]. However, their
difference, 70 − 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, P[j] and P[k], for which their sum
and difference are pentagonal and D = |P[k] − P[j]| is minimised; what is
the value of D?
Answer: 2c2556cb85621309ca647465ffa62370
"""
from common import check
from itertools import count, takewhile
PROBLEM_NUMBER = 44
ANSWER_HASH = "2c2556cb85621309ca647465ffa62370"
pentagonals = set()
def get_pentagonal(n):
t = int(n * (3 * n - 1) * 0.5)
return t
D = None
for d in count(1):
p_d = get_pentagonal(d)
pentagonals.add(p_d)
for c in range(d - 1, 0, -1):
p_c = get_pentagonal(c)
p_b = p_d - p_c
p_a = p_c - p_b
if p_b not in pentagonals or p_a not in pentagonals:
continue
check(p_a, PROBLEM_NUMBER, ANSWER_HASH)
exit()