要求: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. 生成XML文件

    import java.io.FileOutputStream;import java.io.IOException; import org.jdom.Document;import org.jdom ...

  2. Android - 广播接收者 - BroadcastReceiver

    BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...

  3. 【转】request和response的页面跳转传参

    下面是一位园友的文章: jsp或Servlet都会用到页面跳转,可以用 request.getRequestDispatcher("p3.jsp").forward(request ...

  4. stanford Protege 4.3 ERROR: Bundle org.protege.common 解决方法

    我的java版本是jdk1.8.0_45,安装了protege后打开总显示: ERROR: Bundle org.protege.common [1] Error starting file:/hom ...

  5. MySQL日期 字符串 时间戳互转

    平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法:本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串.date转时间戳.字符串转dat ...

  6. WCF 服务器调用回调函数 单程-双程操作模式:(待补充Demo)

    服务器端Server 实现回调接口Interface定义.客户端实现回调接口Interface实现,从而实现服务器端通过  var channel = OperationContent.Current ...

  7. [Java Basics] Reflection

    For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.C ...

  8. UVA 10407 差分思想的运用

    就是每两项相减,肯定能被模数整除. #include <iostream> #include <cstring> #include <cstdio> #includ ...

  9. 百度之星热身赛-1001(dfs拓扑排序)

    题意:作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机.这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的).有一天,这台计算机得到了n个任务要处理, ...

  10. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...