Sqrt(x)

来源:互联网 发布:阿里云cdn加速怎么用 编辑:程序博客网 时间:2024/05/17 07:10
    //newton iterative    public int sqrt(int x) {        // Start typing your Java solution below        // DO NOT write main() function        final double EPS = 0.000001;          double result = (double) x;          double tmp;          do{              tmp = result;              result = (result + (double)x / result) / 2;          }while(Math.abs(tmp - result) > EPS);          int r = (int)result; //consider the value of EPS, this is very important, or it will overflow.        if(r * r - x > 0) r--;        return r;      }


原创粉丝点击