Welcome, guest | Sign In | My Account | Store | Cart

Calculate the continous fraction from a fraction or decimal number

Python, 33 lines
 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
def fr2fc(a,b,t):
    "Return continuos fraction fc0 [a1,a2,a3,...at] from a fraction a/b"
    a=float(a);b=float(b);t0=0
    fc0=[];c=(a/b);pe=int(c);pf=c-pe
    while (t0 < t):
        fc0.append(pe);t0+=1
        c=1.0/pf;pe=int(c);pf=c-pe 
    return fc0
  
def fc2dec(fc0):
    "Return a number from a continuos fraction fc0 [a1,a2,a3,...]"
    ff=list(fc0); ff.reverse()
    return reduce(lambda x,y: y+(1.0/x),ff)      
  
def fc2fr(fc0):
    "Return a fraction from a continuos fraction fc0 [a1,a2,a3,...]"
    fc3=list(fc0)
    a=fc3.pop()
    b=1
    fc3.reverse()
    for k in fc3:
        a,b=k*a+b,a
    return [a,b]

if __name__ == "__main__":
 
    f1= fr2fc(3.141516,1,10)
    print f1
    
    for k in range(1,len(f1)+1):
        f2= fc2fr(f1[0:k]);print f1[0:k],f2 ,float(f2[0])/f2[1]
    
    print fc2dec(f1)

2 comments

Robin Becker 7 years, 2 months ago  # | flag

Slight error in the test the for loop should probably read

for k in range(1,len(f1)+1):

I also think fc2fr could benefit (in speed) from being written like this

def fc2fr(fc0):
    "Return a fraction from a continuos fraction fc0 [a1,a2,a3,...]"
    fc3=list(fc0)
    a=fc3.pop()
    b=1
    fc3.reverse()
    for k in fc3:
        a,b=k*a+b,a
    return a,b
juan (author) 7 years, 2 months ago  # | flag

Robin thank you. Smart python vision. your comments are welcome.

Created by juan on Fri, 19 Sep 2014 (MIT)
Python recipes (4591)
juan's recipes (6)

Required Modules

Other Information and Tasks