Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Aniket-508
Created January 15, 2022 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aniket-508/c4ced2741ed4f80acfe31c70ad624d7c to your computer and use it in GitHub Desktop.
Save Aniket-508/c4ced2741ed4f80acfe31c70ad624d7c to your computer and use it in GitHub Desktop.
Python script to find pi to the Nth digit
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment