Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created August 28, 2009 23:42
Show Gist options
  • Save NicolasT/177325 to your computer and use it in GitHub Desktop.
Save NicolasT/177325 to your computer and use it in GitHub Desktop.
Calculate Pi using the Leibniz formula
def gen_pi():
denom = 1.
positive = True
value = 0.
while True:
value += (1. / denom) if positive else -(1. / denom)
denom += 2.
positive = not positive
yield 4. * value
gen = gen_pi()
import sys
for i in xrange(int(sys.argv[1])):
pi = gen.next()
print pi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment