Skip to content

Instantly share code, notes, and snippets.

@killerswan
Created May 8, 2012 21:57
Show Gist options
  • Save killerswan/2639705 to your computer and use it in GitHub Desktop.
Save killerswan/2639705 to your computer and use it in GitHub Desktop.
pi
# Ramanujan's PI algorithm, per Think Python
#
# >>> execfile("C:/[snip]/Desktop/ramanujan.py")
# >>> rpi()
import math
def rpi ():
k = 0
estimate = 0.0
start = True
while start == True or term >= 1e-15:
start = False
term = innerTerm(k) * 2 * math.sqrt(2) / 9801
estimate += term
k += 1
return 1/estimate
def innerTerm(k):
a = math.factorial(4*k) * (1103+26390*k)
b = (math.factorial(k)**4) * (396**(4*k))
return a / b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment