Created
June 22, 2016 22:32
-
-
Save KoderDojo/1644c6e1586d9216c80466c1105f51e8 to your computer and use it in GitHub Desktop.
Bisection Method of Calculating Square Roots Using Python
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
number = abs(float(raw_input("Calculate square root of? "))) | |
lowerBound = abs(float(raw_input("Lower bound value? "))) | |
upperBound = abs(float(raw_input("Upper bound value? "))) | |
epsilon = 0.001 | |
while True: | |
guess = (lowerBound + upperBound) / 2 | |
difference = guess**2 - number | |
if abs(difference) <= epsilon: | |
break | |
if difference < 0: | |
lowerBound = guess | |
else: | |
upperBound = guess | |
print('The square root is approximately {}.'.format(round(guess, 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment