Issue
I'm trying to enter in a number and calculate pi to that digit input. I managed to be able to calculate Pi, however no matter what number I type it will still generate the same amount of Pi numbers.
I'm a bit confused at what point it's causing to do that
from math import factorial
from decimal import Decimal, getcontext
# Chudnovsky algorithm for figuring out pi
getcontext().prec=100
pi_input = input('How many digits of pi would you like?')
n = int(pi_input)
def calc(n):
t= Decimal(0)
pi = Decimal(0)
deno= Decimal(0)
for k in range(n):
t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += Decimal(t)/Decimal(deno)
pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
pi = 1/pi
return pi
print calc(n)
Here is my output
How many digits of pi would you like? 5
3.141592653589793238462643383279502884197169399375105820974944592307816346
94690247717268165239156011
Solution
Using the Chudnovsky algorithm, the calculation produces about 14.18 decimal digits per iteration: log10((640320^3)/(24*6*2*6)) ~= 14.18
. This can be more clearly seen in the formula for ak / ak-1 as shown on this web page:
https://www.craig-wood.com/nick/articles/pi-chudnovsky
For n = 5, the result has about 70 digits of precision.
Answered By - rcgldr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.