Created
May 27, 2017 23:00
-
-
Save LuxXx/dd8c4028f230b128f1a2e764da072172 to your computer and use it in GitHub Desktop.
Numerical way to do the square root
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
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