-
Notifications
You must be signed in to change notification settings - Fork 0
/
p65.py
37 lines (31 loc) · 1 KB
/
p65.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
33
34
35
36
37
#!/usr/bin/env python3
import itertools
import math
from fractions import Fraction
from typing import List
def get_orbit(frac: Fraction, length: int = 100) -> List[int]:
next_frac: Fraction = frac
orbit = []
while len(orbit) < length:
addend = math.floor(next_frac)
orbit.append(addend)
next_frac = 1 / Fraction(next_frac - addend)
return orbit
def calculate_e() -> Fraction:
curr_euler = Fraction(1)
factorial = Fraction(1)
for n in itertools.count(1):
factorial *= n
if factorial > Fraction(10 ** 300):
break
delta = Fraction(1) / factorial
curr_euler += delta
return curr_euler
if _name_ == "_main_":
euler = calculate_e()
orbit = get_orbit(euler)
running_frac = Fraction(0)
for term in reversed(orbit):
running_frac = Fraction(1, term + running_frac)
numerator = Fraction(1, running_frac).numerator
print(sum(int(digit) for digit in str(numerator)))