I am a beginner at Python(and coding in general) and I'm trying to compute Pi (π) to 1000 decimal places to better my learning. I am using the Chudnovsky algorithm as it is quite straightforward and converges with fewer iterations than most other algorithms out there. More info here : http://en.wikipedia.org/wiki/Chudnovsky_algorithm
This is the formula that I am using.
However, I managed to only get accuracy to the first 10 digits :( . I also managed to get my hands on an existing solution from a website and this works perfectly! I tried to adapt my code without losing readablilty and executed both code blocks with the same arguments to eliminate other factors that could be causing the problem. But still the issue persists.
Can someone point out (for the benifit of my learning) where the mistake is happening and why?
This is my code for you to run and see the results for yourself:
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()
First 100,000 decimal places of Pi can be found here: http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
The issue is that you are missing a parenthesis in the computation of M :
M = Decimal(factorial(6*k))/(factorial(k)**3)*factorial(3*k)
should be :
M = Decimal(factorial(6*k))/((factorial(k)**3)*factorial(3*k))