Calculating Pi to the Nth digit
13,607
Solution 1
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.
Solution 2
I just added round function in the return statement in your code and hope and it works for you as it for me.
from math import factorial
from decimal import Decimal, getcontext
# Chudnovsky algorithm for figuring out pi
getcontext().prec=1000
pi_input = input('How many digits of pi would you like?')
n = int(pi_input)
def cal(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 round(pi,n)
print(cal(n))
Related videos on Youtube
Author by
Blooze
Updated on June 04, 2022Comments
-
Blooze almost 2 years
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
-
klutt almost 7 yearsWhat makes you think it will produce
n
decimals? As far as I can see it will produce better results with a highern
but certainly not exactlyn
digits. And no, the result is different if you changen
. -
sascha almost 7 yearsWithout analyzing the code: what do you mean with your output? Show an example! It is possible, that the output is just a floating-point print and therefore the number of digits seen is not dependent on how many digits you computed? Be more verbose
-
klutt almost 7 yearsTo be clear, you will output the same amount of digits regardless of what n you pick, but the result will be more accurate with higher n.
-
Blooze almost 7 yearsUpdated my output, I guess I am confused how I can make my function perform the calculation based on the input
-
-
il_raffa almost 6 yearsAlong with the code, you should also add a description of the solution you're proposing.
-
Luo Hongshuai over 4 yearsnot work for an N greater than 50
3.14159265358979311599796346854418516159057617187500000000000000000
-
PirateNinjas about 4 yearsrather than iterating you could use a slice, given that you're not calculating pi, per se.
-
Ruli over 3 yearsSuch sulution was already proposed by multiple answers, why to add one more?
-
OctopuSS7 about 3 yearsBut this is calculating the whole thing first, so is very resourcce inneficient.
-
Sreekant Shenoy about 3 yearsThis works only up to a limit of 15 decimal places since
pi
value imported from numpy or math consists of3.141592653589793
. -
Mutex over 2 yearsnope after 48 digits it just shows 0000000000........
-
Jules G.M. over 2 yearsit's in the comment