Skip to content

Instantly share code, notes, and snippets.

@KoderDojo
Created June 22, 2016 22:32
Show Gist options
  • Save KoderDojo/1644c6e1586d9216c80466c1105f51e8 to your computer and use it in GitHub Desktop.
Save KoderDojo/1644c6e1586d9216c80466c1105f51e8 to your computer and use it in GitHub Desktop.
Bisection Method of Calculating Square Roots Using Python
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