-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp057.py
More file actions
62 lines (27 loc) · 959 Bytes
/
p057.py
File metadata and controls
62 lines (27 loc) · 959 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
It is possible to show that the square root of two can be expressed as an infinite continued fraction.
By expanding this for the first four iterations, we get:
The next three expansions are
,
, and
, but the eighth expansion,
, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?
'''
from fractions import Fraction
import sys
def calc(n):
if n == 1:
return Fraction(1,2)
return Fraction(1, 2 + calc(n-1))
def is_final_fraction_top_heavy(n):
to_convert = Fraction(1,1) + calc(n)
return len(str(to_convert.numerator)) > len(str(to_convert.denominator))
def compute():
result = 0
for i in range(1,1001):
if is_final_fraction_top_heavy(i):
result += 1
return result
sys.setrecursionlimit(2000)
print(compute())