127k views
5 votes
Convert Newton’s 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.) An example of the program

User Syockit
by
8.4k points

1 Answer

3 votes

Answer:

import math

tolerance=0.00001

approximation=1.0

x = float(input("Enter a positive number: "))

def newton(number,approximation):

approximation=(approximation+number/approximation)/2

difference_value =abs(number-approximation**2)

if difference_value<= tolerance:

return approximation

else:

return newton(number,approximation)

print("The approximation of program = ", newton(x, approximation))

print("The approximation of Python = ", math.sqrt(x))

Step-by-step explanation:

  • Create a recursive function called newton that calls itself again and again to approximate square root for the newton technique.
  • Apply the formulas to find the estimate value and the difference value .
  • Check whether the difference_value is less than the tolerance value and then return the value of approximation else make a recursive call to the newton function by passing the user input and the new approximation value .
  • Finally display all the results using the print statement.
User Zulander
by
8.8k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.