Python program to calculate digits of PI
Python program to calculate π
Chundnovsky's formulae:
`\frac{426880\sqrt{10005}}{\pi}=`
`\sum _{k=0}^{\infty}\frac{(6k)!(13591409+545140134k)}{(3k)!(k!)^3(-640320)^{3k}}`
#made by Raxit Gupta
import decimal
import math
#we use Chundnovsky's formulae
def compute_pi(n):
decimal.getcontext().prec = n + 3
C = 426880 * decimal.Decimal(10005).sqrt()
K = decimal.Decimal(6)
M = decimal.Decimal(1)
X = decimal.Decimal(1)
L = decimal.Decimal(13591409)
S = L
# For better precision, we calculate to n+3 and remove the last two digit
for i in range(1, n+3):
M = decimal.Decimal(M* ((1728*i*i*i)-(2592*i*i)+(1104*i)-120)/(i*i*i))
L = decimal.Decimal(545140134+L)
X = decimal.Decimal(-262537412640768000*X)
S += decimal.Decimal((M*L) / X)
return str(C/S)[:-2]
n=int(input("Enter decimal places:"))
P=compute_pi(n)
print(P)
if there is any mistake, please comment!
ReplyDeleteimport decimal
import math
#we use Chundnovsky's formulae
def compute_pi(n):
decimal.getcontext().prec = n + 3
C = 426880 * decimal.Decimal(10005).sqrt()
K = decimal.Decimal(6)
M = decimal.Decimal(1)
X = decimal.Decimal(1)
L = decimal.Decimal(13591409)
S = L
# For better precision, we calculate to n+3 and truncate the last two digits
for i in range(1, n+3):
M = decimal.Decimal(M* ((1728*i*i*i)-(2592*i*i)+(1104*i)-120)/(i*i*i))
L = decimal.Decimal(545140134+L)
X = decimal.Decimal(-262537412640768000*X)
S += decimal.Decimal((M*L) / X)
return str(C/S)[:-2]
n=int(input("Enter decimal places:"))
P=compute_pi(n)
print(P)