-
Notifications
You must be signed in to change notification settings - Fork 0
/
57.py
30 lines (26 loc) · 783 Bytes
/
57.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
from fractions import gcd
def continued_fraction_2(N):
n, d = (3, 2)
for _ in xrange(N-1):
n, d = 2*d + n, d + n
k = gcd(n, d)
n, d = n/k, d/k
return n, d
assert continued_fraction_2(1) == (3, 2)
assert continued_fraction_2(2) == (7, 5)
assert continued_fraction_2(3) == (17, 12)
assert continued_fraction_2(4) == (41, 29)
assert continued_fraction_2(5) == (99, 70)
assert continued_fraction_2(6) == (239, 169)
assert continued_fraction_2(7) == (577, 408)
assert continued_fraction_2(8) == (1393, 985)
def problem57(N=1000):
n, d = (3, 2)
count = 0
for _ in xrange(N-1):
n, d = 2*d + n, d + n
k = gcd(n, d)
n, d = n/k, d/k
if len(str(n)) > len(str(d)): count += 1
return count
print problem57()