-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler57.py
62 lines (38 loc) · 985 Bytes
/
euler57.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# def count(limit):
# 1+1/2
# 1 + 1/(2+ 1/2)=
# 1 + 1/(2 + 1/(2 + 1/2)=
# 1 + 1/(2 + 1/(2 + 1/(2 + 1/2)=
# (2 + 1/2) = 5/2
# (2 + 1/(5/2)) = (2 + 2/5) = 12/5
# (2 + 1/(12/5)) = (2 + 5/12) = 29/12
def makestring(limit):
limit=limit-1
beg="1 + 1/"
term="(2 + 1/"
end="2"
string= beg+(term*limit)+end
string=string.replace('(2 + 1/2','(5/2')
return string
def eval_last(string):
a=string.rfind("(")
b=string.rfind("/")
den=int(string[b+1:])
num=int(string[a+1:b])
if a==6:
numf=num+den
if len(str(numf))>len(str(num)):
return True
else:
return False
else:
numf=2*num+den
term=str(numf)+'/'+str(num)
return eval_last(string[:a-6]+term)
def count():
n=0
for i in range(2,1001):
s=makestring(i)
if eval_last(s):
n+=1
return n