Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

For what value of positive integer does the bisection search code fails? As explained in the Lecture 3, for bisection method the code is pasted below ``` x = int(raw_input("Enter a Number to find the square root: ")) epsilon = 0.01 numOfGuesses = 0 low = 0 high = x ans = (low + high)/2.0 while abs(ans**2 - x) > epsilon and ans <= x: print "LOW: ", low, " HIGH: ", high, " ANS: ", ans numOfGuesses += 1 if ans**2 < x: low = ans else: high = ans ans = (low + high)/2.0 print 'numOfGuesses =', numOfGuesses print ans, 'is close to square root of', x ```

OpenStudy (anonymous):

The code does work for all positive Here it the output for integers for 0 to 9 >>> numOfGuesses = 0 0.0 is close to square root of 0 LOW: 0 HIGH: 1 ANS: 0.5 LOW: 0.5 HIGH: 1 ANS: 0.75 LOW: 0.75 HIGH: 1 ANS: 0.875 LOW: 0.875 HIGH: 1 ANS: 0.9375 LOW: 0.9375 HIGH: 1 ANS: 0.96875 LOW: 0.96875 HIGH: 1 ANS: 0.984375 LOW: 0.984375 HIGH: 1 ANS: 0.9921875 numOfGuesses = 7 0.99609375 is close to square root of 1 LOW: 0 HIGH: 2 ANS: 1.0 LOW: 1.0 HIGH: 2 ANS: 1.5 LOW: 1.0 HIGH: 1.5 ANS: 1.25 LOW: 1.25 HIGH: 1.5 ANS: 1.375 LOW: 1.375 HIGH: 1.5 ANS: 1.4375 LOW: 1.375 HIGH: 1.4375 ANS: 1.40625 LOW: 1.40625 HIGH: 1.4375 ANS: 1.421875 numOfGuesses = 7 1.4140625 is close to square root of 2 LOW: 0 HIGH: 3 ANS: 1.5 LOW: 1.5 HIGH: 3 ANS: 2.25 LOW: 1.5 HIGH: 2.25 ANS: 1.875 LOW: 1.5 HIGH: 1.875 ANS: 1.6875 LOW: 1.6875 HIGH: 1.875 ANS: 1.78125 numOfGuesses = 5 1.734375 is close to square root of 3 numOfGuesses = 0 2.0 is close to square root of 4 LOW: 0 HIGH: 5 ANS: 2.5 LOW: 0 HIGH: 2.5 ANS: 1.25 LOW: 1.25 HIGH: 2.5 ANS: 1.875 LOW: 1.875 HIGH: 2.5 ANS: 2.1875 LOW: 2.1875 HIGH: 2.5 ANS: 2.34375 LOW: 2.1875 HIGH: 2.34375 ANS: 2.265625 LOW: 2.1875 HIGH: 2.265625 ANS: 2.2265625 LOW: 2.2265625 HIGH: 2.265625 ANS: 2.24609375 numOfGuesses = 8 2.236328125 is close to square root of 5 LOW: 0 HIGH: 6 ANS: 3.0 LOW: 0 HIGH: 3.0 ANS: 1.5 LOW: 1.5 HIGH: 3.0 ANS: 2.25 LOW: 2.25 HIGH: 3.0 ANS: 2.625 LOW: 2.25 HIGH: 2.625 ANS: 2.4375 LOW: 2.4375 HIGH: 2.625 ANS: 2.53125 LOW: 2.4375 HIGH: 2.53125 ANS: 2.484375 LOW: 2.4375 HIGH: 2.484375 ANS: 2.4609375 numOfGuesses = 8 2.44921875 is close to square root of 6 LOW: 0 HIGH: 7 ANS: 3.5 LOW: 0 HIGH: 3.5 ANS: 1.75 LOW: 1.75 HIGH: 3.5 ANS: 2.625 LOW: 2.625 HIGH: 3.5 ANS: 3.0625 LOW: 2.625 HIGH: 3.0625 ANS: 2.84375 LOW: 2.625 HIGH: 2.84375 ANS: 2.734375 LOW: 2.625 HIGH: 2.734375 ANS: 2.6796875 LOW: 2.625 HIGH: 2.6796875 ANS: 2.65234375 LOW: 2.625 HIGH: 2.65234375 ANS: 2.638671875 numOfGuesses = 9 2.6455078125 is close to square root of 7 LOW: 0 HIGH: 8 ANS: 4.0 LOW: 0 HIGH: 4.0 ANS: 2.0 LOW: 2.0 HIGH: 4.0 ANS: 3.0 LOW: 2.0 HIGH: 3.0 ANS: 2.5 LOW: 2.5 HIGH: 3.0 ANS: 2.75 LOW: 2.75 HIGH: 3.0 ANS: 2.875 LOW: 2.75 HIGH: 2.875 ANS: 2.8125 LOW: 2.8125 HIGH: 2.875 ANS: 2.84375 numOfGuesses = 8 2.828125 is close to square root of 8 LOW: 0 HIGH: 9 ANS: 4.5 LOW: 0 HIGH: 4.5 ANS: 2.25 LOW: 2.25 HIGH: 4.5 ANS: 3.375 LOW: 2.25 HIGH: 3.375 ANS: 2.8125 LOW: 2.8125 HIGH: 3.375 ANS: 3.09375 LOW: 2.8125 HIGH: 3.09375 ANS: 2.953125 LOW: 2.953125 HIGH: 3.09375 ANS: 3.0234375 LOW: 2.953125 HIGH: 3.0234375 ANS: 2.98828125 LOW: 2.98828125 HIGH: 3.0234375 ANS: 3.005859375 LOW: 2.98828125 HIGH: 3.005859375 ANS: 2.9970703125 numOfGuesses = 10 3.00146484375 is close to square root of 9

OpenStudy (anonymous):

The program can be adapted to run for negative integers, as well for floats, but then there is a problem with the ans <= x: because then you're comparing floating points nrumbers. Here it will be ok, because 1 works out ok. Comparing a float and an integers might also lead to problems In case of floating points The other condition abs(ans**2 - x) > epsilon, won't work in the domain x in (0,1) x²<x and if adapted for negative numbers it is in the domain x in (-1,0) also x²< |x|

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!