c# newton's method


  • 0
    R
    public int MySqrt (int x) {
        if (x == 0) return 0;
        double approx = 0.5 * x;
        double betterapprox = 0.0;
        while (true) {
            betterapprox = 0.5 * (approx + x / approx);
            if (approx - betterapprox < 0.001) {
                break;
            }
            approx = betterapprox;
        }
        return (int) betterapprox;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.