[LeetCode] Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

百度百科里,牛顿迭代解释:http://baike.baidu.com/view/643093.htm?fr=aladdin

 
已经证明,如果是连续的,并且待求的零点是孤立的,那么在零点周围存在一个区域,只要初始值位于这个邻近区域内,那么牛顿法必定收敛。
class Solution {
public:
    int sqrt(int x) {
        double ans = x;
        while(abs(ans*ans-x)>0.0001)
        {
            ans = (ans+x/ans)/2;
        }
        return ans;
    }
};
原文地址:https://www.cnblogs.com/Xylophone/p/3859274.html