If we want to calculate square root, we can use Math.sqrt() method. It is simple. Now do you know how to write such a method by yourself?
Here is the equation you need.
The first sqrt number should be the input number / 2.
Using the equation, we can come up with a Java Square Root method by ourselves.
public static double sqrt(int number) { double t; double squareRoot = number / 2; do { t = squareRoot; squareRoot = (t + (number / t)) / 2; } while ((t - squareRoot) != 0); return squareRoot; } |
U can check number begin lines of function. It’s doesn’t matter.
When the number is o or 1, this method does not work
Omg misaki u made our entire class laugh haha
Is this the babylonian method of square roots?