Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = 25
- low = 0
- high = x
- epsilon = .01
- increment = 0.001
- ans = (low + high)/2.0
- guesses = 0
- while abs(ans*ans - x) >= epsilon:
- if ans*ans > x:
- high = ans
- else:
- low = ans
- guesses += 1
- ans = (high + low)/2.0
- print guesses
- if abs(ans*ans - x) < epsilon:
- print "The square root of ", x, "within a margin of ", epsilon, " is ", ans
- else:
- print "no square root"
- # 1. 12.25*12.25 > x. Low = 0, High = 25, ans =(0+25)/2 = 12.25
- # 2. 6.25*6.25 > x. Low = 0, High = 12.25, (0 + 12.5)/2= 6.25
- # 3. 3.125*3.125 < x. low =0, High = 6.25, ans = (0, 6.25)/2 = 3.125
- # 4. low = ans 3.125, High = 6.25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement