, the base of the natural logarithm (e.g. ) can be expressed as the following equation:
The Euler’s formula involves , which is:
The Euler’s Identity is the special case when ,
The constant coincides in many interesting problems. For example, the average number to add random numbers () in order to obtain a sum larger than one can be obtained by the following Python code by brute-force over a few iterations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/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) |
#!/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 , which is 2.75 (with error = 0.0317).
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 will be improved. And if maxn reaches , the will be equal to . Anybody can explain this in mathematics?
See also: Simple and Efficient C Program to Compute the Mathematic Constant E (Euler’s number)
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Codeforces: A. Almost Prime
Next Post: One Simple Equation to Compute e, base of the natural logarithm