Skip to content

Instantly share code, notes, and snippets.

@jtauber
Created August 13, 2009 05:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtauber/167009 to your computer and use it in GitHub Desktop.
Save jtauber/167009 to your computer and use it in GitHub Desktop.
Pi in Hex
# Implementation of Simon Plouffe's formula for Pi in Hex
#
# James Tauber 2007-03-14
# http://jtauber.com/blog/2007/03/14/generating_the_hex_digits_of_pi/
def pi():
N = 0
n, d = 0, 1
while True:
xn = (120*N**2 + 151*N + 47)
xd = (512*N**4 + 1024*N**3 + 712*N**2 + 194*N + 15)
n = ((16 * n * xd) + (xn * d)) % (d * xd)
d *= xd
yield 16 * n // d
N += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment