Continued fraction of the constant e

1.3k views Asked by At

I am very new to python. I have just recently started doing computer science. I am however stuck on this topic as to how do I get the value of the continued fraction of e constant after I've input a number. Please do not solve it for me but teach me how to go about it. I am not sure if I should use while loops or for loops and even if I do, I am not sure how to go about it.

This is the full phrasing,

This is the question.

2

There are 2 answers

0
dan_fulea On

One can use the following code in python2.

# exp(1) from the generalized continued fraction
def expOneFromContinuedFraction( n=30 ):
    """Compute an approximative value of exp(1) from
    e ~ 2 + 1/( 1+1/ (2+2/ (3+3/ ( ... n+n/(n+1) ) ) ) )
    """

    a = 1. + n
    for k in range(n, 0, -1):
        a = k + k/a
    return 2+1/a

for n in range(1,11):
    print "n = %2s :: approximation is %.24f" % ( n, expOneFromContinuedFraction(n) )

A copy+paste into the ipython interpreter gave me:

n =  1 :: approximation is 2.666666666666666518636930
n =  2 :: approximation is 2.727272727272727514957751
n =  3 :: approximation is 2.716981132075471538911415
n =  4 :: approximation is 2.718446601941747697850360
n =  5 :: approximation is 2.718263331760264023273521
n =  6 :: approximation is 2.718283693893449814993346
n =  7 :: approximation is 2.718281657666403727802162
n =  8 :: approximation is 2.718281842777827250756673
n =  9 :: approximation is 2.718281827351874291309741
n = 10 :: approximation is 2.718281828538485989099627

I hope it is clear where python looses precision.

Note that one can also perform exact computations, e.g. by using a fraction supporting package. In such situations i use sage instead of python. Same language, but with more "batteries". The similar version of the code, where we do not start with the float a = 1. + n, but with the sage integer a = 1+n gives the fractions. Here is the exact computation, with the a posteriori taken numerical value.

def sageExpOneFromContinuedFraction( n=30 ):
    a = n+1
    for k in range(n, 0, -1):
        a = k + k/a
    return 2 + 1/a

for n in range(1,11):
    a = sageExpOneFromContinuedFraction(n)
    print "n = %2s :: exp(1) ~ %s ~ %s" % ( n, a, a.n(digits=50) )

Results, that reflect better the periodicity of the decimal representation of rational numbers...

n =  1 :: exp(1) ~ 8/3 ~ 2.6666666666666666666666666666666666666666666666667
n =  2 :: exp(1) ~ 30/11 ~ 2.7272727272727272727272727272727272727272727272727
n =  3 :: exp(1) ~ 144/53 ~ 2.7169811320754716981132075471698113207547169811321
n =  4 :: exp(1) ~ 280/103 ~ 2.7184466019417475728155339805825242718446601941748
n =  5 :: exp(1) ~ 5760/2119 ~ 2.7182633317602642756016989145823501651722510618216
n =  6 :: exp(1) ~ 45360/16687 ~ 2.7182836938934499910109666207227182836938934499910
n =  7 :: exp(1) ~ 44800/16481 ~ 2.7182816576664037376372792913051392512590255445665
n =  8 :: exp(1) ~ 3991680/1468457 ~ 2.7182818427778273384920361985403726496587915070036
n =  9 :: exp(1) ~ 43545600/16019531 ~ 2.7182818273518744088075986743931517096224602330742
n = 10 :: exp(1) ~ 172972800/63633137 ~ 2.7182818285384861664135778815996451660083959085657

Note: Please show next time the own efforts to compute

0
JimmyCarlos On

When tackling these mathematical questions, the best strategy I use is to try to break the problem down into tiny steps. This is what the function for n = 1,2,3 looks like.

n = 1
c = 1/2
c = 1/(1+c)
print(2+c)

n = 2
c = 2/3
c = 1/(2+c)
c = 1/(1+c)
print(2+c)

n = 3
c = 3/4
c = 2/(3+c)
c = 1/(2+c)
c = 1/(1+c)
print(2+c)

I would then try to work out by hand what the function for n=4 and n=5 would look like. Once I have got the hang of it, generalise it in your own words. Then, convert that to your programming language of choice.