-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e848416
commit 542b46b
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from fractions import Fraction | ||
from itertools import count | ||
|
||
def terms_for_e(): | ||
for k in count(2, 2): | ||
yield 1 | ||
yield k | ||
yield 1 | ||
|
||
def drop(sequence, n): | ||
for _, value in zip(range(n), sequence): | ||
pass | ||
yield from sequence | ||
|
||
def take(sequence, n): | ||
for _, value in zip(range(n), sequence): | ||
yield value | ||
|
||
def continued_fractions(a0, an): | ||
frac = Fraction(0) | ||
for ai in reversed(an): | ||
frac = Fraction(1, ai + frac) | ||
return a0 + frac | ||
|
||
terms = [*take(terms_for_e(), 99)] | ||
e_fraction = continued_fractions(2, terms) | ||
digits = str(e_fraction.numerator) | ||
print(sum(int(n) for n in digits)) |