Had it run 10 times too many integers woops, but still easy to solve
This commit is contained in:
59
57/main.py
Normal file
59
57/main.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
'''
|
||||||
|
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?
|
||||||
|
'''
|
||||||
|
|
||||||
|
import math
|
||||||
|
import time
|
||||||
|
|
||||||
|
def t(n, dDict):
|
||||||
|
assert n >= 0
|
||||||
|
|
||||||
|
if n == 0:
|
||||||
|
return 1, 1
|
||||||
|
|
||||||
|
num, denom = d(n-1, dDict)
|
||||||
|
return (num + denom, denom)
|
||||||
|
|
||||||
|
def d(n, dDict):
|
||||||
|
assert n >= 0
|
||||||
|
|
||||||
|
if n == 0:
|
||||||
|
return 1, 2
|
||||||
|
|
||||||
|
if n in dDict.keys():
|
||||||
|
return dDict[n]
|
||||||
|
|
||||||
|
num, denom = d(n-1, dDict)
|
||||||
|
r = (denom, 2 * denom + num)
|
||||||
|
dDict[n] = r
|
||||||
|
return r
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Hello, this is Patrick")
|
||||||
|
t0 = time.time()
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
dDict = {}
|
||||||
|
for n in range(1001):
|
||||||
|
num, denom = t(n, dDict)
|
||||||
|
if len(str(num)) > len(str(denom)):
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
print(counter, time.time() - t0)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user