-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
44 lines (36 loc) · 1.27 KB
/
Copy pathsolution.py
File metadata and controls
44 lines (36 loc) · 1.27 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
import json
import time
from itertools import count
from typing import List
from queue import PriorityQueue
_PENTAGONAL_NUMS = [int(i*(i*3 -1)/2) for i in range(0, 10000000)]
_PN_LOOKUP = set(_PENTAGONAL_NUMS)
def pentagonal_dif(n, k):
return abs(_PENTAGONAL_NUMS[n] - _PENTAGONAL_NUMS[n+k])
def pentagonal_add(n, k):
return abs(_PENTAGONAL_NUMS[n] + _PENTAGONAL_NUMS[n+k])
def pentagonal_difs():
_PN_DIFS = PriorityQueue()
_PN_DIFS.put((4,1,1))
highest_n = 2
highest_n_dif = (3*(highest_n) + 1, highest_n, 1)
_PN_DIFS.put(highest_n_dif)
while True:
min_dif = _PN_DIFS.get()
_, n, k = min_dif
_PN_DIFS.put((pentagonal_dif(n, k+1), n, k+1))
if n == highest_n:
highest_n += 1
highest_n_dif = (3*(highest_n) + 1, highest_n, 1)
_PN_DIFS.put(highest_n_dif)
yield min_dif
def solution() -> int:
for dif, n, k in pentagonal_difs():
add = pentagonal_add(n, k)
if add in _PN_LOOKUP and dif in _PN_LOOKUP:
return dif
cpu_s, wall_s = time.process_time(), time.time()
result = solution()
cpu_e, wall_e = time.process_time(), time.time()
cpu_time, wall_time = cpu_e - cpu_s, wall_e - wall_s
print(json.dumps({"solution": result, "cpu": cpu_time, "wall": wall_time}))