Last active
August 29, 2015 14:17
-
-
Save kenmanheimer/840834cfce780517d8d8 to your computer and use it in GitHub Desktop.
This file contains 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
# 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