#sqrtIterative.py #square root, iteratively print("Determine the square root") x = float(input("Enter x, the number you want to find the square root of: ")) g = float(input("Enter initial guess: ")) loops = 0 #while difference between g^2 and x is more than one-millionth of x # i.e. until within one-millionth of x. while abs(g**2-x) > x / 1000000: g = (g + x/g) / 2 #average of guess and Moves g closer to sqrt of x. loops += 1 print("Loop #",loops," g=",g) print("Square root of",x,"=",g) #square root, bisection method. #same x, no initial guess. low = 0 high = x g = (low+high) / 2 #middle of low and high loops = 0 while abs(g**2-x) > x / 1000000: loops += 1 print("Loop #",loops," g=",g) if g**2 < x: low = g else: high = g g = (low+high) / 2 print("Using the bisection method: Square root of",x,"=",g)