The Euler’s formula involves
The Euler’s Identity is the special case when
The
#!/usr/bin/env python
from random import random, seed
from math import e
maxn = 100
seed()
s = 0
for i in range(maxn):
ss = 0
j = 0
while ss < 1:
j += 1
ss += random()
s += j
ee = s * 1.0 / maxn
print "%.6f" % ee
print e, abs(e - ee)
The 100 iterations give us an approximate value of
2.750000 2.71828182846 0.031718171541
If we increase the iterations to 1000000,
Surprisingly, the output is the following.
2.718216 2.71828182846 6.58284590451e-05
We can see that if we keep increasing the iterations, the absolute error to
See also: Simple and Efficient C Program to Compute the Mathematic Constant E (Euler’s number)
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Codeforces: A. Almost Prime
Next Post: One Simple Equation to Compute e, base of the natural logarithm