首页 > 解决方案 > 在 Python 中使用 Chudnovsky 算法计算 Pi 时我变得不准确

问题描述

我是 Python 的初学者(和一般的编码),我正在尝试将 Pi (π) 计算到小数点后 1000 位,以提高我的学习能力。我使用的是 Chudnovsky 算法,因为它非常简单,并且比大多数其他算法收敛的迭代次数更少。更多信息在这里:http ://en.wikipedia.org/wiki/Chudnovsky_algorithm

这是我正在使用的公式。

但是,我设法只获得了前 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()

Pi 的前 100,000 个小数位可以在这里找到: http ://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html

标签: pythonpi

解决方案


问题是您在计算 M 时缺少括号:

M = Decimal(factorial(6*k))/(factorial(k)**3)*factorial(3*k)

应该 :

M = Decimal(factorial(6*k))/((factorial(k)**3)*factorial(3*k))

推荐阅读