Skip to content

Instantly share code, notes, and snippets.

@xiaolai
Forked from ikautak/pi_generator.py
Created October 19, 2017 01:48
Show Gist options
  • Save xiaolai/acc81a681589a1d72c8e2b6b4fcfeb76 to your computer and use it in GitHub Desktop.
Save xiaolai/acc81a681589a1d72c8e2b6b4fcfeb76 to your computer and use it in GitHub Desktop.
calculate pi using python generator.
#!/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