-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp057.py
29 lines (27 loc) · 1013 Bytes
/
p057.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
def compute():
"""
Generalize this by letting a = 2.
S_{1} = 1 + \frac{1}{a}
S_{2} = 1 + \frac{1}{(a - 1) + 1 + \frac{1}{a}} = 1 + \frac{1}{S_{1} + (a-1)}
S_{3} = 1 + \frac{1}{S_{2} + (a-1)}
...
S_{n} = 1 + \frac{1}{S_{n-1} + (a-1)}
Given that S_{n-1} = \frac{numerator}{denominator}
S_{n} = 1 + \frac{1}{\frac{numerator}{denominator} + \frac{(a-1) * denominator}{denominator}}
= 1 + \frac{denominator}{numerator + (a-1) * denominator}
= \frac{numerator + (a-1) * denominator + denominator}{numerator + (a-1) * denominator}
= \frac{numerator + a * denominator}{numerator + (a-1) * denominator}
"""
a = 2 # number being repeated
count = 0
iterations = 0
num = a + 1
denom = a
while iterations < 1000:
if len(str(num)) > len(str(denom)):
count += 1
num, denom = (num + a * denom, num + (a - 1) * denom)
iterations += 1
return count
if __name__ == "__main__":
print(compute())