要求:Implement int sqrt(int x).  Compute and return the square root of x.

解决方法:

1.牛顿法Newton's method

化解:计算  x2 = a 的解,令 y = x2 - a,相当于求解 y = 0 的解,f(x) 如图。

第一步:取 x0 = a / 2 , 如果  x0 不是解,在点( x0,y0) 做切线 y - y0 = (x - x0) * y'0 ,与 x 轴的交点为 x1 = x0 - y0 / y'0 = x0 - (x02 - a) / (2x0) = (x0 + a / x0) / 2 

第二步:如果 x1 不是解,做一个经过  ( x1,y1) 这个点的切线,与x轴的交点为 x2

…………

结束条件:

a.  f( xi ) ≈ 0;

b. xi xi-1.

代码:

inline double ABS(double x){
return x > 0 ? x : (-1 * x);
} class Solution {
public:
int sqrt(int x) {
/* 用牛顿迭代法求浮点数的平方根 */
double g0 = 0, g1 = x;
while(ABS(g1-g0) > 0.9)
{
g0 = g1;
g1 = (g0 + (x / g0)) / 2;
}
return floor(g1); // (int)g1
}
};

 2. 分治法Divide and conquer

a = 0 时,返回 0.

a = 1 时,返回 1.

a > 1 时,返回的数在序列 1 与 a/2 之间,序列有序,所以可以用二分法查找。

代码如下:

class Solution {
public:
int sqrt(int x){
/*********** 二分法 ************/
/*********************************/
if(x == 0) return 0;
if(x == 1) return 1;
unsigned long long begin = 1, end = x >> 1;
while(begin < end)
{
unsigned long long mid = (begin + end) >> 1;
unsigned long long tem = mid * mid;
if(tem == x) return mid;
else if(tem > x) end = mid -1;
else begin = mid + 1;
}
if(begin = end)
{
unsigned long long tem = end * end;
if(tem <= x) return end;
if(tem > x) return end - 1;
}
else return end;
}
};

3. 构造数字

从高位到低位,依次试探添加 1。

class Solution {
public:
int sqrt(int x) {
int ans = 0;
int bit = 0X40000000;
while(bit > 0) {
ans |= bit;
if (ans > x / ans) {
ans ^= bit;
}
bit >>= 1;
}
return ans; }
};

3.Sqrt(x)的更多相关文章

  1. 速算1/Sqrt(x)背后的数学原理

    概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...

  2. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  3. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

  4. 欧几里得证明$\sqrt{2}$是无理数

    选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...

  5. 求sqrt()底层效率问题(二分/牛顿迭代)

    偶然看见一段求根的神代码,于是就有了这篇博客: 对于求根问题,通常我们可以调用sqrt库函数,不过知其然需知其所以然,我们看一下求根的方法: 比较简单方法就是二分咯: 代码: #include< ...

  6. 【leetcode】Sqrt(x)

    题目描述: Implement int sqrt(int x). Compute and return the square root of x. 实现开根号,并且返回整数值(这个很重要,不是整数的话 ...

  7. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  8. Sqrt(x) - LintCode

    examination questions Implement int sqrt(int x). Compute and return the square root of x. Example sq ...

  9. UVa 12505 Searching in sqrt(n)

    传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...

随机推荐

  1. IT公司100题-28-整数的二进制表示中1的个数

    问题描述: 输入一个整数n,求n的二进制表示中,一共有多少个1.例如n=8,二进制表示为00001000,二进制表示中有1个1.     分析: 如果一个数n不为0,那么n-1的二进制表示,与n的二进 ...

  2. Rhel6-piranha配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...

  3. find_in_set()

    $where[]="find_in_set('".$grades."',a.grades)";

  4. 团队开发——冲刺1.f

    冲刺阶段一(第六天) 1.昨天做了什么? 为解决自己电脑的问题,查找关于C#的资料,后期做准备. 2.今天准备做什么? 把最初版与交予代码书写的同学,进行整合:测试程序. 3.遇到什么困难? 第一次整 ...

  5. javascript 中的 delete

    那么,为什么我们能删除一个对象的属性: var x = { a: 1 }; delete x.a; // true x.a; // undefined 但却不能删除一个变量: var x = 1; d ...

  6. 一些sql语句的常用总结(重要)

    select primary_flag from tc_contact where primary_flag !=0 select dept_id,dept_name,tree_level,tree_ ...

  7. Myeclipse+Axis2+Tomcat开发webService

    1.  下载文件: 需要在axis2官网下载两种类型的axis2文件,bin版和war版(下载地址:http://axis.apache.org/axis2/java/core/download.cg ...

  8. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  9. 该应用的登录功能版本较旧,无法使用QQ账号登录,请升级到最新版本,如果还无法解决,请联系开发者升级。(错误码:100044)

    该原因应该是你的应用数据签名更改的原因 解决步骤已经写到我的公众号,二维码在下面. 欢迎观看我的CSDN学院课程,地址:http://edu.csdn.net/course/detail/2877 本 ...

  10. Javascript 中的 && 和 || 使用小结

    准备两个对象用于下面的讨论. var alice = { name: "alice", toString: function () { return this.name; } }; ...