在Python中使用Chudnovsky算法计算Pi时,我的精确度越来越高
我是Python的初学者(一般来说是编码),我正在尝试将Pi(π)计算到小数点后1000位,以便更好地学习。我使用的是Chudnovsky算法,因为它非常简单,收敛的迭代次数比其他大多数算法都少。 更多信息请点击此处: 但是,我只获得了前10位的准确度:(.我还设法从一个网站上获得了一个现有的解决方案,效果非常好!我尝试在不丢失可读性的情况下调整代码,并使用相同的参数执行两个代码块,以消除可能导致问题的其他因素。但问题仍然存在 有人能指出(为了我的学习)错误发生的地方和原因吗 这是我的代码,供您自己运行并查看结果:在Python中使用Chudnovsky算法计算Pi时,我的精确度越来越高,python,pi,Python,Pi,我是Python的初学者(一般来说是编码),我正在尝试将Pi(π)计算到小数点后1000位,以便更好地学习。我使用的是Chudnovsky算法,因为它非常简单,收敛的迭代次数比其他大多数算法都少。 更多信息请点击此处: 但是,我只获得了前10位的准确度:(.我还设法从一个网站上获得了一个现有的解决方案,效果非常好!我尝试在不丢失可读性的情况下调整代码,并使用相同的参数执行两个代码块,以消除可能导致问题的其他因素。但问题仍然存在 有人能指出(为了我的学习)错误发生的地方和原因吗 这是我的代码,供
Python 3.7.6
from decimal import Decimal, getcontext
def factorial(x):
'''
Function to perform a factorial operation on a given argument. Returns factorial value.
'''
if x < 1:
return 1
else:
return x * factorial(x-1)
def chudnovsky01(iterations):
'''
http://en.wikipedia.org/wiki/Chudnovsky_algorithm
Computes approximation of pi as per chudivsky algorithm.
This is the code block that is giving innacuracy.
'''
sum_term = Decimal(0) #setting sumterm as zero
C = Decimal(10005).sqrt() / 4270934400 #defining the Constant term
for k in range(0,iterations): #defining the summation loop
M = Decimal(factorial(6*k))/(factorial(k)**3)*factorial(3*k) #defining the Multinomial term
L = 13591409+(545140134*k) #defining the Linear term
X = Decimal(640320**(k*3)) #defining the Exponential term
sum_term += Decimal((-1)**k)*M*L/X #Calculating the sum-term
pi = sum_term*C #Multiplying the Constant & Sum-term
pi = pi**(-1) #Taking reciprocal
return pi
def chudnovsky02(n):
'''
The same algorithm picked up from a website. Same formula; harder to read but Works perfectly!
'''
pi = Decimal(0)
k = 0
while k < n:
pi += (Decimal(-1)**k)*(Decimal(factorial(6*k))/((factorial(k)**3)*(factorial(3*k)))* (13591409+545140134*k)/(640320**(3*k)))
k += 1
pi = pi * Decimal(10005).sqrt()/4270934400
pi = pi**(-1)
pi
return pi
def main():
print('******** WELCOME TO PI CALCULATOR ********\n')
precision = int(input('Enter the number of decimal places 1-1000\t'))
getcontext().prec = precision+1
iterations = 20 # Arbitrary value.Should be sufficient enough to
# get atleast 100 digits of Pi with accuracy
pi = chudnovsky01(iterations)
pi2 = chudnovsky02(iterations)
print(f'Completed! with {iterations} iterations.')
print(f'Pi: First func result:\t {pi}')
print(f'Pi: Second func result:\t {pi2}')
if __name__ == '__main__':
main()
Python 3.7.6
从十进制导入十进制,getcontext
def阶乘(x):
'''
函数对给定参数执行阶乘运算。返回阶乘值。
'''
如果x<1:
返回1
其他:
返回x*阶乘(x-1)
def chudnovsky01(迭代次数):
'''
http://en.wikipedia.org/wiki/Chudnovsky_algorithm
根据chudivsky算法计算pi的近似值。
这是给出不精确性的代码块。
'''
求和项=十进制(0)#将求和项设置为零
C=十进制(10005).sqrt()/4270934400#定义常量项
对于范围内的k(0,迭代):#定义求和循环
M=十进制(阶乘(6*k))/(阶乘(k)**3)*阶乘(3*k)#定义多项式项
L=13591409+(545140134*k)#定义线性项
X=十进制(640320**(k*3))#定义指数项
求和项+=十进制(-1)**k)*M*L/X#计算求和项
pi=求和项*C#乘以常数和求和项
pi=pi**(-1)#取倒数
返回pi
def chudnovsky02(n):
'''
同样的算法从一个网站上获得。同样的公式;更难阅读,但工作完美!
'''
pi=十进制(0)
k=0
当k
Pi的前100000位小数可在此处找到:
问题在于,在计算M时缺少括号:
M = Decimal(factorial(6*k))/(factorial(k)**3)*factorial(3*k)
M = Decimal(factorial(6*k))/((factorial(k)**3)*factorial(3*k))
应该是: