-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
31 lines (26 loc) · 843 Bytes
/
Copy pathsolution.py
File metadata and controls
31 lines (26 loc) · 843 Bytes
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
import json
import time
from itertools import count, islice
from typing import Iterator, Tuple
def get_e_cont_frac() -> Iterator[int]:
"""Get continued fraction for e
"""
for i in count(1):
yield 1
yield 2*i
yield 1
def get_nth_convergent(nth: int) -> Tuple[int, int]:
"""Get numerator and denominator for nth convergent
"""
n, d = 0, 1
for i in reversed(list(islice(get_e_cont_frac(), nth-1))):
n, d = d, d*i + n
return n+2*d, d
def solution() -> int:
n, d = get_nth_convergent(100)
return sum(int(i) for i in str(n))
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}))