Sunday, 11 August 2013

How can I sum up the different values within this while loop?

How can I sum up the different values within this while loop?

This seems so simple yet I am having a hard time getting it to work. I was
trying to create a new variable inside the "while loop" to collect the
values of x at each loop, like
k2 += x
but it does not work. So how could I sum up the different values within
this while loop? Thank you much.
# pi approximation by using Ramanujan Formula
import math
def estimate_pi(k):
x = (2 * math.sqrt(2)/9801 * math.factorial(4*k) *(1103 +
26390*k))/(math.factorial(k**4)*396**(4*k))
while x >= 1e-15:
k += 1
print '{:>5.15f} {:>5} {:>1}'.format(x, 'for k =', k)
return estimate_pi(k)
estimate_pi(0)

No comments:

Post a Comment