Square Root
Write a function called sqrtN()
to estimate the square root of any number using Newton’s method:
Write your function so that the number N
whose square root is to be taken, and the number of iterations to use M
, are both input parameters of the function. The output of the function should be the estimate of the square root of N
. You can choose the initial guess of the value of the square root on your own, use whatever you like. I suggest the number 10.0 as an initial guess, as in the Wikipedia example.
Write a program that uses your sqrtN()
function to find the square root of N=612 using M=5 iterations. Try it again using M=10 iterations.
Your script should produce output like the following:
The square root of 612.000 using 5 iterations is 24.738633753766 The square root of 612.000 using 10 iterations is 24.738633753706