Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Snbig/da61552b0cc1a5e0e613e0af3e00c43c to your computer and use it in GitHub Desktop.
Save Snbig/da61552b0cc1a5e0e613e0af3e00c43c to your computer and use it in GitHub Desktop.
calculating the digits of π (Chudnovsky algorithm)
k,a,b,a1,b1=2,4,1,12,4
pi=''
digits=30000
while len(pi) < digits:
p,q,k=k*k,2*k+1,k+1
a,b,a1,b1=a1,b1,p*a+q*a1,p*b+q*b1
d,d1=a//b,a1//b1
while d == d1 and len(str(d)) < digits:
pi+=str(d)
a,a1=10*(a%b),10*(a1%b1)
d,d1=a//b,a1//b1
print(pi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment