这看起来很简单,但我很难让它工作。我试图在“while循环”中创建一个新变量来收集每个循环中x的值,比如
k2 += x
但它不起作用。那么我如何总结这个while循环中的不同值呢?非常感谢。
# 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)