Skip to content

Instantly share code, notes, and snippets.

@markhamilton1
Created March 23, 2014 00:48
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save markhamilton1/9716714 to your computer and use it in GitHub Desktop.
Save markhamilton1/9716714 to your computer and use it in GitHub Desktop.
pi is a Python script that computes each digit of the value of pi. As long as this script runs it continues to generate digits. As a matter of full disclosure, I did not write this. I am posting it here as a means of preserving the algorithm and making it available to others.
import sys
def calcPi():
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
while True:
if 4*q+r-t < n*t:
yield n
nr = 10*(r-n*t)
n = ((10*(3*q+r))//t)-10*n
q *= 10
r = nr
else:
nr = (2*q+r)*l
nn = (q*(7*k)+2+(r*l))//(t*l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
pi_digits = calcPi()
i = 0
for d in pi_digits:
sys.stdout.write(str(d))
i += 1
if i == 50:
print("")
i = 0
@angeloped
Copy link

Wow! Thanks for this.

@ostap-tymchenko
Copy link

holly crap that took way to long to find one that works thanks!

@bert8877
Copy link

Perfect! Thanks

@Vj-pixel
Copy link

I ran it in repl.it, and it definitely works!

@behrad2k
Copy link

how

@ostap-tymchenko
Copy link

Hi, Can you elaborate on what you mean by “how”? How to use repl.it? How to use this script? How to get it to execute?

@behrad2k
Copy link

how does this even work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment