Calculating e using Continued Fraction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Calculating e using Continued Fraction
# http://en.wikipedia.org/wiki/Continued_fraction
import math
n = 18 # number of iterations
x = 0.0
for i in range(n, 0, -1):
if i % 3 == 1:
j = int(i / 3) * 2
else:
j = 1
x = 1.0 / (x + j)
print x + 1, math.e
|
Tags: math, mathematics
Great! Here is another way of doing this. Also works for e, pi, sqrt(n) [n can be any integer not just 2 or 3]. Let me know if you want the code or explanation
It looks like it is calculating digits of e one by one. Definitely more impressive than my examples :-)
You could just print the result this way though:
print result[0] + "." + result[1:]