Created
August 28, 2009 23:42
-
-
Save NicolasT/177325 to your computer and use it in GitHub Desktop.
Calculate Pi using the Leibniz formula
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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