Newton Raphson Method(a very optimized way to find root)

public class NewtonRaphson{
public static void main(String[] args) {
System.out.printf("%.3f",sqrt(423749));
}

static double sqrt(int n){
double x = n;
double root;

while(true){
root = (x + n/x)/2;

if(Math.abs(root-x) < 1){
break;
}

x = root;
}

return root;
}
}

--

--

Solutions to all your coding related problems at one point. DSA question on daily basis and much more.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Palakkgoyal

Solutions to all your coding related problems at one point. DSA question on daily basis and much more.