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)