Skip to content

Instantly share code, notes, and snippets.

@zdway10
Forked from xiaolai/pi_generator.py
Created March 17, 2020 22:54
Show Gist options
  • Save zdway10/836c444274b738db00c92fea973e5d62 to your computer and use it in GitHub Desktop.
Save zdway10/836c444274b738db00c92fea973e5d62 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