-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
38 lines (31 loc) · 1.03 KB
/
Copy pathsolution.py
File metadata and controls
38 lines (31 loc) · 1.03 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
import json
import time
from functools import reduce
def highest_common_denom(a, b):
a, b = sorted((a,b))
while a != b:
a, b = sorted((b-a,a))
return a
def simplify(n, d):
"""simplify fraction given by numerator n and denominator d
"""
common_d = highest_common_denom(n, d)
return(n//common_d, d//common_d)
def curious_fractions():
for i in range(1, 10):
for j in range(1, i):
for k in range(1, 10):
n = 10*j + i
d = 10*i + k
if simplify(n,d) == simplify(j, k):
yield simplify(n, d)
def solution() -> int:
fractions = list(curious_fractions())
product = reduce(lambda x, y: (x[0]*y[0], x[1]*y[1]), fractions)
num, denom = simplify(*product)
return denom
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}))