-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_44.py
More file actions
66 lines (47 loc) · 1.36 KB
/
problem_44.py
File metadata and controls
66 lines (47 loc) · 1.36 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# P(n) = n * (3n - 1)/2
# P(n+1) - P(n) = 3n + 1
import math
def calc_pentagonal(n):
return n * (3*n - 1)//2
def is_pentagonal(n):
determinant = 1 + 24*n
root = math.isqrt(determinant)
if root**2 != determinant:
return False
return (1 + root) % 6 == 0
def check_options(queue, count):
nodes = queue.pop(count)
for node in nodes:
x1 = calc_pentagonal(node[0])
x2 = calc_pentagonal(node[1])
difference = x2 - x1
if is_pentagonal(difference):
total = x2 + x1
if is_pentagonal(total):
print(node)
print(difference)
return True
next_node = (node[0]+1, node[1]+1)
increment = node[1] - node[0]
key = count + increment
if count + increment in queue:
queue[key].append(next_node)
else:
queue[key] = [next_node]
return False
queue = {0: [(1, 2)]}
count = 0
next_add = (1, 3)
next_index = 2
count_added = 1
found_solution = False
while not found_solution:
if count == next_index:
queue[count].append(next_add)
next_index += next_add[1]
next_add = (next_add[0], next_add[1]+1)
count_added += 1
if count_added % 3 == 0:
next_index += 1
found_solution = check_options(queue, count)
count += 1