# import SQRT function to compare the calculated result with the computer result
from math import sqrt
testdata = [
2,
9.8696044010893586188344909998762,
1000000
]
#Function to calculate each iteration of the sqrt calculation
def calc(x1):
xi2 = x1 ** 2;
_2xi = 2 * xi
xi1 = xi - (((xi2) - k)/(_2xi))
print(xi1);
return xi1;
# ===========================================================
print ("Newton Raphson Method");
# process each entry in test data list as "k" - the number to find sqrt of
for k in testdata:
xi = float(k);
xi1 = float(0);
while True:
xi1 = calc(xi);
if (abs(xi1 - xi) < 1E-10) :
break; # repeat until the diff is minimal
xi = xi1
print ('Input K = {} -> Calculated SQRT is xi1 = {}, Computer Sqrt = {}'.format(k, xi1, sqrt(k)));