lunes, 24 de abril de 2017

Estimate e


Estimate e:

In this assignment you will estimate the mathematical constant e. You should create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.
You will want to use the infinite series to calculate the value, stopping when the accuracy is reached (previous and current calculation are the same at the specified accuracy).


Here´s my code:
def factorial(x):
    if x==0:
        return 1
    else:
        return x * factorial(x - 1)

def estimate_e(estimation):
    guess= 1
    dif= 10
    e= 2
    x= 2
    while dif > precision:
        e =  e + 1/factorial(x)
        dif= e - guess
        guess= e
        x  =  x + 1
    return e

precision = float(input("Give the precision: "))
print (estimate_e(precision)) 

No hay comentarios:

Publicar un comentario