-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem-057.py
40 lines (31 loc) · 944 Bytes
/
problem-057.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
38
39
40
"""
Problem 57 - Square Root Convergents
It is possible to show that the square root of two can be expressed as an
infinite continued fraction.
In the first one-thousand expansions, how many fractions contain a numerator
with more digits than the denominator?
"""
def num_fracs(n: int) -> int:
"""
Calculate number of fraction with more digits in numerator
Parameters
n (int): number of terms in series
Returns
(int): number of terms with more digits in the numerator
"""
num = 3
den = 2
num_fracs = 0
for _ in range(2, n + 1):
num_temp = num + 2 * den
den_temp = num + den
if len(str(num_temp)) > len(str(den_temp)):
num_fracs += 1
num = num_temp
den = den_temp
return num_fracs
if __name__ == "__main__":
print(
"The number of expansions with more digits in the numerator:"
+ str(num_fracs(1000))
)