Method 1:2 points to find
x = 24epsilon = 0.01numGuesses = 0low = 0.0high = Max (1.0,x) ans = (high + low)/2.0while abs (ans*ans-x) >=epsilon: print ' Low: ', Low, ' High: ', high, ' ans = ', ans numguesses + = 1 if ans**2 <x: Low = ans else: high = Ans ans = (high + low)/2.0print ' numguesses = ', numguessesprint ans, ' was close to square root of ', X
low:0.0 high:24 ans = 12.0low:0.0 high:12.0 ans = 6.0low:0.0 high:6.0 ans = 3.0low:3.0 high:6.0 ans = 4.5low:4.5 high:6.0 ans = 5.25low:4.5 high:5.25 ans = 4.875low:4.875 high:5.25 ans = 5.0625low:4.875 high:5.0625 ans = 4.96875 low:4.875 high:4.96875 ans = 4.921875numGuesses = 94.8984375 is close to square root of 24
Method Two: Isaac Newton method
Epsilon = 0.01k = 24.0guess = K/2.0numguesses = 0while ABS (guess*guess-k) >=epsilon: guess = guess-((guess**2-k )/(2*guess)) numguesses + = 1print ' numguess = ', numguessesprint ' Square root of ', K, ' is about ', guess
Numguess = 4Square root of 24.0 is about 4.89898874321
Square root of X