Title: Newton Square Root Problem
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Newton Square Root Problem

Create a package named newton_sqrt and write a program to use the "Newton's method" for computing a square root.

Newton's method for calculating the square root of N starts by making a guess at the square root. I would recommend starting with an initial guess of N/2.

It then computes a better guess, according to the following formula:

new_guess = ((N/last_guess) + last_guess)/2;

You will want to use a while loop for this algorigthm. Each time you do the calculation you will get a more accurate answer. Have your while loop continue executing until the

accuracy is < .000001

It can be shown that the accuracy of your "new_guess" is:

accuracy = absolute_value of (new_guess - last_guess)

If you are unfamiliar with absolute value, then we would say that for some variable x:


double x, absolute_value_of_x;

// ..... x gets a value somehow
if (x >= 0)
    absolute_value_of_x = x;
else
    absolute_value_of_x = -x;


Print the "Newton_sqrt" answer for computing a square root at the end of your while loop.

Compare it with the Java Math function:

Math.sqrt(N);

Sample output might look like:

Enter in N for Newton:  200
Newton(200.0)=14.142135623730955
Math.sqrt =14.142135623730951



Run your program with the inputs shown above and insert the results in the appropriate section of the JH worksheet.

November 22, 2014 11:40 AM