Question: Python3 Convert Newtons method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of the square root should
Python3 Convert Newtons method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of the square root should be passed as a second argument to the function.)
Old Code:
import math
# Initialize the tolerance TOLERANCE = 0.000001
def newton(x): """Returns the square root of x.""" # Perform the successive approximations estimate = 1.0 while True: estimate = (estimate + x / estimate) / 2 difference = abs(x - estimate ** 2) if difference <= TOLERANCE: break return estimate
def main(): """Allows the user to obtain square roots.""" while True: # Receive the input number from the user x = input("Enter a positive number or enter/return to quit: ") if x == "": break x = float(x) # Output the result print("The program's estimate is", newton(x)) print("Python's estimate is ", math.sqrt(x))
if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
