Sqrt(x) II

Implement double sqrt(double x) and x >= 0.

Code:

public class Solution {

    public double sqrt(double x) {
        double guess = 1;
        while (Math.abs(guess * guess - x) / x > 1e-10) {
            guess = (guess + x / guess) / 2;
        }
        return guess;
    }
}

results matching ""

    No results matching ""