-
-
Save zdway10/836c444274b738db00c92fea973e5d62 to your computer and use it in GitHub Desktop.
calculate pi using python generator.
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
#!/usr/bin/env python | |
def pi(): | |
# Wallis' product | |
numerator = 2.0 | |
denominator = 1.0 | |
while True: | |
yield numerator/denominator | |
if numerator < denominator: | |
numerator += 2 | |
else: | |
denominator += 2 | |
p = pi() | |
res = 1.0 | |
for i in range(10000000): | |
res *= next(p) | |
print res * 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment