[Tutor] Python Program: Newton's Method

Donna Ibarra donnaibarra at gmail.com
Mon Jan 26 00:11:50 CET 2009


Hello,

I need to write a program that implements Newton's method ((guess + x/guess)
/ (2)). The program should prompt the user for the value to find the
squareroot of (x) and the number of times to improve the guess. Starting
with a guess value of x/2, your program should loop the specified number of
times applying newton's method and report the final value of guess. You
should also subtract your estimate from the value of math.sqrt() to show how
close it is.

So far.. I got:

 Help with Code
Tags<http://www.daniweb.com/forums/misc-explaincode.html?TB_iframe=true&height=400&width=680>
(Toggle Plain Text <http://www.daniweb.com/forums/post787759.html#>)

import math

def main():
    value, guess = input("Please enter the value, and the number of
times to loop: ")

    for i in range(guess):
        top = guess + value / guess
        final_value = float(top) / 2

    close = final_value - math.sqrt(x)
    close_two = math.sqrt(x)

    print "The guess is", final_value, "which is", close, "away from", close_two

main()


Could you please help me out? Thanks

- Donna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090125/b6d89d21/attachment-0001.htm>


More information about the Tutor mailing list