-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_44.py
32 lines (24 loc) · 1.01 KB
/
problem_44.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
"""
Challenge 44 of project euler - Pentagon Numbers
@author Ori Dabush
"""
from utils.pentagons import get_pentagon_number, get_pentagon_numbers, is_pentagon_number
def solve():
"""
By iterating on the difference we make sure that the first option we find is the one with the minimal difference.
We can stop iterating on p_j once p_k is smaller than p_(j+1), because the differences keeps getting bigger
so all the next pentagon numbers will also be less than p_(j+1).
Also, we can optimize number of iterations by making sure p_j <= d because if p_j > d it will be found when
d will be equal to p_j.
"""
for d in get_pentagon_numbers():
for j, p_j in get_pentagon_numbers(indexed=True):
p_k = p_j + d
if p_j > d or p_k < get_pentagon_number(j + 1):
break
if is_pentagon_number(p_k) and is_pentagon_number(p_j + p_k):
return d
def main():
print(f'The answer is {solve()}')
if __name__ == '__main__':
main()