Skip to content

Instantly share code, notes, and snippets.

@md2perpe
Last active September 23, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md2perpe/6a3819798b2da731449dd945deeecb2c to your computer and use it in GitHub Desktop.
Save md2perpe/6a3819798b2da731449dd945deeecb2c to your computer and use it in GitHub Desktop.
Calculate Eulers number (e)
# https://www.facebook.com/groups/progfml/permalink/344454616128233/
from math import factorial
e=0
n=0
step_length=1
e_right=2.71828
margin_of_error=0.001
Klart=False
while Klart != True:
for k in range (0,n+1):
a=factorial(k)
e=e+1/a
print("a=%d, 1/a=%f, e=%f" % (a, 1/a, e))
difference= e_right - e
if difference <= margin_of_error:
Klart=True
if difference > margin_of_error:
n=n+1
print (n,e,difference)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment