Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 28, 2017 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/d99be5cb98101919c2c05709fe9d5aa6 to your computer and use it in GitHub Desktop.
Save jianminchen/d99be5cb98101919c2c05709fe9d5aa6 to your computer and use it in GitHub Desktop.
Root of number - newton method
private static double newtonMethod(int n, double target, double delta) {
double root = 1;
do {
double fx = fx(root, n, target);
double df = df(root, n);
root = root - fx/df;
} while (Math.abs(fx(root, n, target)) > delta);
return root;
}
private static double fx(double root, int n, double target) {
return Math.pow(root, n) - target;
}
private static double df(double root, int n) {
return n * Math.pow(root, n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment