Jordan
Jordan

Reputation: 23

Recursive square root loop in python with a epsilon of .0001

I need help with a square root calculator. The directions are as follows:

"You need to implement the Babylonian Method for computing square roots. Based on that core functionality, you are to write an interactive program that:

Prompts the user to enter an integer value above zero. Checks to ensure that the value is indeed above zero. If not, the program displays an error message and asks for the input again. Computes the square root of the value using the Babylonian Method outlined above. Displays the square root to the user formatted to show exactly 3 decimal places (no more than 3, and no less than 3)."

I can't use the sqrt function. I'm getting really close but can't quite get to the right loop. Here's what I have so far

# Ask user if they would like to calculate a square root for a single number or range.
single = input("Enter 'single' or 'range' to solve for a single square root or a range of values, respectively: ")

# Set error message to let the user know 'range' calculation are not currently available.
if single == 'single' or 'Single':

    # Ask user to enter a positive integer.
    number = int(input("Enter a positive integer value: "))

    # Set error message asking for valid number if user enters something other than a positive integer.
    while number < 0:
        print("Please enter a valid number.")
    else:
        # Choose epsilon
        epsilon = .0001

        # Choose estimate for the square root of x
        estimate = 4

        # Evaluate the estimate
        while number - estimate > epsilon:
            # Calculate the square root using the Babylonian Method.
            estimate = (number + 1.0) / 2.0
            second_estimate = (estimate + number / estimate) / 2.0

            # Print the users selected value and its square root.
            print("Value", "   ", "Square Root")
            print(" ", number, "        ", format(second_estimate, '.3f'))
else:
    # Tell user 'range' calculation are not currently available.
    print("That function is not currently available.")

Upvotes: 1

Views: 1643

Answers (1)

Jonas
Jonas

Reputation: 1847

The condition for the while loop is wrong. number is 9 and estimate goes to 3. So you have to check number - estimate*estimate (take the absolute value because estimate converges to 3 from above for a starting value of 4). You dont need the first estimate according to Babylonian method on wikipedia. Also estimate is always 5 in your code. (estimate = (number + 1.0) / 2.0 where number is always 9)

number = 9
epsilon = .0001
estimate = 4
while abs(number - estimate*estimate) > epsilon:
    estimate = (estimate + number/estimate) / 2.0
    print("Value", "   ", "Square Root")
    print(" ", number, "        ", format(estimate, '.4f'))

Result:

Value     Square Root
  9          3.1250
Value     Square Root
  9          3.0025
Value     Square Root
  9          3.0000

Upvotes: 1

Related Questions