mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
@@ -19,11 +19,13 @@ import java.util.Scanner;
|
||||
public class SquareRootWithNewtonRaphsonMethod {
|
||||
|
||||
public static double squareRoot(int n) {
|
||||
double x = n; //initially taking a guess that x = n.
|
||||
double root = 0.5 * (x + n / x); //applying Newton-Raphson Method.
|
||||
double x = n; // initially taking a guess that x = n.
|
||||
double root = 0.5 * (x + n / x); // applying Newton-Raphson Method.
|
||||
|
||||
while (Math.abs(root - x) > 0.0000001) { //root - x = error and error < 0.0000001, 0.0000001 is the precision value taken over here.
|
||||
x = root; //decreasing the value of x to root, i.e. decreasing the guess.
|
||||
while (
|
||||
Math.abs(root - x) > 0.0000001) { // root - x = error and error < 0.0000001, 0.0000001
|
||||
// is the precision value taken over here.
|
||||
x = root; // decreasing the value of x to root, i.e. decreasing the guess.
|
||||
root = 0.5 * (x + n / x);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user