Skip to content

Instantly share code, notes, and snippets.

@kenmanheimer
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmanheimer/840834cfce780517d8d8 to your computer and use it in GitHub Desktop.
Save kenmanheimer/840834cfce780517d8d8 to your computer and use it in GitHub Desktop.
# Adapted from a friend's alternative solution to the Madhava implementation problem
# from question 16 in the Interactive Python Textbook problem set:
# http://interactivepython.org/runestone/static/thinkcspy/Functions/thinkcspyExercises.html
# Original gist: https://gist.github.com/anonymous/2f78d796e965d6abcaec/321252a19d85aebca2359188d9991f7472e9f786
def myPi(tolerance=0.01):
"""Compute Pi by Madhava's technique, to specified tolerance, default .01."""
accumulator = 0
n = 0
newvalue = abs(tolerance) + 1
while abs(newvalue) > tolerance:
newvalue = ( ( (-1) ** n) / ((2 * n) + 1) )
n += 1
accumulator += newvalue
# Show the newvalue formula result for each step in the loop:
print(n, accumulator*4)
return (accumulator * 4)
if __name__ == "__main__":
print(myPi())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment