69 sqrt


title: sqrtx
tags:
- sqrtx
- No.69
- simple
- binary-search
- mathematical-analysis
- overflow


Problem

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Input: 4
Output: 2

Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since 
             the decimal part is truncated, 2 is returned.

Corner Cases

  • Integer overflow sqrt(x) >= sqrt(2147483647) = 51453
  • zero

Solutions

Binary Search

For function \sqrt{x}, its derivative is:

\frac{d}{dx} \sqrt{x} = \frac{1}{2 \sqrt{x}}

thus, for line:

y = \frac{1}{2 \sqrt{x_0}} (x - x_0) + \sqrt{x_0}

we have inequalities:

\frac{1}{2 y_0} (x - y_0^2) + y_0 \geq \sqrt{x}, x \geq x_0

For y_0 = 1, we can narrow the range to the half before binary search.

Note that the limits of int type, for a > 2^{16}, we cannot compare a^2 since it is greater than 2^{32}.

long type can solve this problem. Since for int, a < 2^{32}, then a^2 < 2^{64}. Another way is to turn int to uint. Though java do not have unsigned type, there exists a method to compare them.

Running time is O(lg(x)).

class Solution {
    public int mySqrt(int x) {
        if (x == 0) {return 0;}

        long i = 1;
        long j = x / 2 + 1;
        long m;

        while (i != j && i+1 != j) {
            m = (i + j) / 2;
            i = (x < m*m) ? i : m;
            j = (x < m*m) ? m : j;
        }

        return (int)i;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,918评论 0 38
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • #1996 AHSME ##1996 AHSME Problems/Problem 1 The addition ...
    abigtreenj阅读 1,441评论 0 0
  • 何以解忧 《解忧杂货店》是暑假里看完的小说,是东野圭吾诸多小说中我最喜欢的一本。之后就介绍给孩子们看,列入阅读计划...
    鹿之言语阅读 319评论 0 0
  • 工作再忙,也还是要抽时间好好研究如何更快实现自己的目标的;起步再晚,也还是要尽早迈出自己的第一步的;前面的路再无坦...
    拾秋兔阅读 200评论 0 2