Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created May 27, 2017 23:00
Show Gist options
  • Save LuxXx/dd8c4028f230b128f1a2e764da072172 to your computer and use it in GitHub Desktop.
Save LuxXx/dd8c4028f230b128f1a2e764da072172 to your computer and use it in GitHub Desktop.
Numerical way to do the square root
public class Rooting {
public static void main(String[] args) {
System.out.println(next(1000, 2));
System.out.println(Math.sqrt(2));
}
public static double next(int n, double r) {
if (n <= 0) return 1;
double xn = next(n - 1, r);
return 0.5*(xn + r / xn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment