-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path065.py
71 lines (69 loc) · 1.46 KB
/
065.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
63
64
65
66
67
68
69
70
71
#create the sequence with the terms
#of the continued fraction
terms=100
seq=[2]
i=0; k=1
while True:
if i==terms-1: break
seq.append(1)
i+=1
if i==terms-1: break
seq.append(2*k)
i+=1; k+=1
if i==terms-1: break
seq.append(1)
i+=1
if i==terms-1: break
#and now calculate the fraction
denominator=seq.pop()
numerator=1
while True:
try:
curr=seq.pop()
numerator=denominator*curr+numerator
#now inverse the fraction
tmp=numerator
numerator=denominator
denominator=tmp
except:
tmp=numerator
numerator=denominator
denominator=tmp
break
print "The %dth term is %d/%d." % (terms, numerator, denominator)
#sum of the digits in the numerator
strnum=str(numerator)
soma=0
for c in strnum: soma+= int(c)
print "The sum of the digits in the numerator is %d." % soma
#output
#The 100th term is
#6963524437876961749120273824619538346438023188214475670667
#/
#2561737478789858711161539537921323010415623148113041714756.
#The sum of the digits in the numerator is 272.
"""
#just a test
sqrt2=2.5
for i in range(20):sqrt2=2+1/sqrt2
sqrt2=1+1/sqrt2
print 2**.5-sqrt2
#just another test
sqrt23=1.0
for i in range(5):
sqrt23=8+1/sqrt23
sqrt23=1+1/sqrt23
sqrt23=3+1/sqrt23
sqrt23=1+1/sqrt23
sqrt23=4+1/sqrt23
print 23**.5-sqrt23
#and another
from math import exp
e=1.0
for i in range(10, 0, -1):
e=1+1/e
e=2*i+1/e
e=1+1/e
e=2+1/e
print exp(1.) - e
"""