Created
August 16, 2012 13:51
-
-
Save yadakhov/3370248 to your computer and use it in GitHub Desktop.
Square Root using Newton's Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Computes the square root of a nonnegative number c using | |
* http://en.wikipedia.org/wiki/Newton's_method | |
* Newton's method: | |
* - initialize t = c | |
* - replace t with the average of c/t and t | |
* - repeat until desired accuracy reached | |
* | |
* % java Sqrt 2 | |
* 1.414213562373095 | |
*/ | |
public class SqrtNewtonMethod { | |
public static void main(String[] args) { | |
double c = Double.parseDouble(args[0]); | |
double epsilon = 1e-15; // relative error tolerance | |
double t = c; // estimate of the square root of c | |
// repeatedly apply Newton update step until desired precision is achieved | |
while (Math.abs(t - c/t) > epsilon*t) { | |
t = (c/t + t) / 2.0; | |
} | |
// print out the estimate of the square root of c | |
System.out.println(t); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment