33.4k views
4 votes
Write a program to compute the square root of a number. Do not use any math libraries/methods in this program.

1 Answer

7 votes

Answer:

private double sqrt(double x) {

double g = x / 2;

while(true)

double average = (g + x/g) / 2;

if(average == g

return g;

}

Step-by-step explanation:

Newton's method is to improve an initial guess "x / 2" by averaging between the guess and x / guess. You continue that until numerically your guess equals x/ guess.

User Tbjorch
by
5.7k points