[Tutor] newton's square root formula
WM.
wferguson1 at socal.rr.com
Tue Feb 3 03:59:55 CET 2009
# program to find square root
square = float(raw_input ("Please enter a number to be rooted, "))
guess = input("Please guess at the root, ")
i = 0
while guess**2 != square:
i+=1
# Newton's formula
guess = guess - (guess * guess - square) / (guess * 2)
print i
print "\n\n\n%s is the square root of %s" % (guess, square)
print "\n%s loops were run." % (i)
print "\n\n\nbye"
#
Here is my program, enhanced by Alan's good advice. The reason I wanted
to re-write the other program was, it had a limited number of loops and
I felt accuracy should be the measure. So, just now, I added a loop
counter here and found that the formula is a little buggy. Make 'square
= 7' and you will be in the 'i = 500' area before you can find ControlC.
More information about the Tutor
mailing list