Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = 25
- epsilon = 0.01 #how close we want to get
- numGuesses = 0 #just keeps track
- low = 0.0 #low end of range to check
- high = x #high end
- ans = (high + low)/2.0 #initial guess in the middle
- while abs(ans**2 - x) >= epsilon:
- print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
- numGuesses += 1
- if ans**2 < x:
- low = ans
- else:
- high = ans
- ans = (high + low)/2.0
- print('numGuesses = ' + str(numGuesses))
- print(str(ans) + ' is close to square root of ' + str(x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement