Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Fix detail: y is *not* the exact value, just a closer approximation. Improve grammar. Improve code style. Fix typo (Newtown).
Source Link
wjandrea

Newton's Method (simple code)Newton's Method (simple code)

This is the method suggested in Think Python, 2nd edition, page 67, and doesn't need any library. The Newtown'sNewton's method takes a number a and returns its square root as follows:

 y = (x + (a/x)) / 2

where x is an arbitrary estimation and y is the true square roota better estimation of a.

def sqr_root (a): 
    if a <= 0:  #check# check whether *a* is a positive number.
        print ('Non-positive entry')
    else:
        epsilon = 0.0000001   # #anan arbitrary small number, for faster convergence. 
        x = a/2  #an# an estimation of square root. 
        while True:
            y = (x + (a/x)) / 2 
            if abs(y-x) < epsilon: #check # check the difference between x and y to be small enough
                break
            x = y
    print (x)

P.S: the Newton's Method formula in Latex)

Newton's Method (simple code)

This is the method suggested in Think Python, 2nd edition, page 67, and doesn't need any library. The Newtown's method takes a number a and returns its square root as follows:

 y = (x + (a/x))/2

where x is an arbitrary estimation and y is the true square root of a.

def sqr_root (a): 
    if a <= 0:  #check whether *a* is a positive number.
        print ('Non-positive entry')
    else:
        epsilon = 0.0000001    #an arbitrary small number, for faster convergence. 
        x = a/2  #an estimation of square root. 
        while True:
            y = (x + (a/x))/2 
            if abs(y-x) < epsilon: #check the difference between x and y to be small enough
                break
            x = y
    print (x)

P.S: the Newton's Method formula in Latex)

Newton's Method (simple code)

This is the method suggested in Think Python, 2nd edition, page 67, and doesn't need any library. Newton's method takes a number a and returns its square root as follows:

y = (x + a/x) / 2

where x is an arbitrary estimation and y is a better estimation of a.

def sqr_root(a): 
    if a <= 0:  # check whether *a* is a positive number.
        print('Non-positive entry')
    else:
        epsilon = 0.0000001  # an arbitrary small number, for faster convergence. 
        x = a/2  # an estimation of square root. 
        while True:
            y = (x + a/x) / 2 
            if abs(y-x) < epsilon:  # check the difference between x and y to be small enough
                break
            x = y
    print(x)

P.S: the Newton's Method formula in Latex

Source Link
Amirhossein Yaghoubi

Newton's Method (simple code)

This is the method suggested in Think Python, 2nd edition, page 67, and doesn't need any library. The Newtown's method takes a number a and returns its square root as follows:

 y = (x + (a/x))/2

where x is an arbitrary estimation and y is the true square root of a.

def sqr_root (a): 
    if a <= 0:  #check whether *a* is a positive number.
        print ('Non-positive entry')
    else:
        epsilon = 0.0000001    #an arbitrary small number, for faster convergence. 
        x = a/2  #an estimation of square root. 
        while True:
            y = (x + (a/x))/2 
            if abs(y-x) < epsilon: #check the difference between x and y to be small enough
                break
            x = y
    print (x)

P.S: the Newton's Method formula in Latex)

lang-py