0

For a=13 and a precision epsilon=10^-7. How many times do you apply the newton recursion formula in newton_sqrt(13,10^-7)? Hint: use global variables.

My current newton_sqrt(a, epsilon) function is the following:

def newton_sqrt(a, epsilon):
     global count  
     if a < 0:  
         print("Error: a < 0") 
         return -1  
     elif a == 0.0:  
         return 0  
     else:  
         x = abs(a)  
         newx = 0.5*(x + a/x)  
         if abs(x - newx) > epsilon:  
              newton_sqrt(newx, epsilon)  
              count = count + 1  
              if not abs(x-newx) > epsilon:  
                   print (count)  
                   return newx  
newton_sqrt(13, 0.000001)

For whatever reason, I get

918488688 None

as my output.

Please help.

0

2 Answers 2

1

There is no output since you never reach the print line:

basically, you have:

if x:
    if not x:
        print(something)

what you want, i'm guessing is:

if x:
    #do something
else:
    #do somthing else

not knowing the math of your function I would change it into:

def newton_sqrt(a, epsilon, count):
     if a < 0:  
         print("Error: a < 0") 
         return -1  
     elif a == 0.0:  
         return 0  
     else:  
         x = abs(a)  
         newx = 0.5*(x + a/x) 
         if abs(x - newx) > epsilon:  
              count = count + 1  
              newton_sqrt(newx, epsilon, count)  
         else:
              print (count)  
              return newx  

which will give you:

newton_sqrt(13, 0.000001, 0)
23
0

First, let's be clear that your newton_sqrt() function doesn't work. Either you're trying to instrument the recursion depth to fix it, or you're unaware it's broken.

A working newton_sqrt() would be along the lines of:

import sys

def newton_sqrt(a, epsilon, x=None):
    if a < 0:
        print("Error: a < 0", file=sys.stderr)
        return -1

    if a == 0:
        return 0

    if x is None:  # initial guess
        x = a

    new_x = (x + a / x) / 2  # refine guess

    if abs(new_x * new_x - a) < epsilon:  # test guess
        return new_x

    return newton_sqrt(a, epsilon, new_x)  # (better) guess again

print(newton_sqrt(13, 1e-06))

Once that's working, instrumenting the recursion depth using a global variable, count, is simple:

import sys

count = 0

def newton_sqrt(a, epsilon, x=None):
    global count

    count += 1

    if a < 0:
        print("Error: a < 0", file=sys.stderr)
        return -1

    if a == 0:
        return 0

    if x is None:  # initial guess
        x = a

    new_x = (x + a / x) / 2  # refine guess

    if abs(new_x * new_x - a) < epsilon:  # test guess
        return new_x

    return newton_sqrt(a, epsilon, new_x)  # (better) guess again

print(newton_sqrt(13, 1e-06), count)

OUTPUT

> python3 test.py
3.6055513629176015 5
>

Where 3.6055513629176015 is the square root of 13 and 5 is the recursion depth needed to compute it with an error of less than 1e-06.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.