[Leetcode] SQRT (int x)

Source: Internet
Author: User

Question:

Implementint sqrt(int x).

Compute and return the square root of X.

An1_1:Bipartite

class Solution {public:    int sqrt(int x) {                if(x < 0) return -1; // assert(x >= 0);                long long x2 = (long long)x;        long long left = 0;        long long right = x2;        long long mid = 0;                while(left <= right){            mid = left + (right - left) / 2;                        if(mid * mid == x2 || (mid * mid < x2 && (mid + 1) * (mid + 1) > x2)){               return (int)mid;            } else if(mid * mid < x2){                left = mid + 1;            } else{                right = mid - 1;            }        }    }};

Note:

1) non-negative number judgment. The negative number has no square root.

2) value range: Mid = left + (right-left)/2. The value may exceed the maximum value range of Int. Therefore, set the mid type to long (C ++ without ulong)

An1_2:Newton Iteration Method

class Solution {public:    int sqrt(int x) {        if(x < 0) return -1; // assert(x >= 0);                double n = x;        while(abs(n * n - x) > 0.0001){            n = (n + x / n) / 2;        }                return (int)n;    }};

Note:

To solve the square root problem of A, we can convert it to x ^ 2-A = 0 to evaluate the X value, and then ABC (x ^ 2-A) <0.0001 (0.0001 is near precision)

F (x) = x ^ 2-A, F (x) is the range of Precision values (infinitely close to 0)

Evaluate the function f (x:

The conversion formula:

Evaluate the f (x) = x ^ 2-a formula and import it to: xn + 1 = xn-(XN ^ 2-A)/(2xn) = xn-(Xn-A/XN)/2 = (xn + A/XN)/2

Xn + 1 is infinitely close to XN, I .e.: xn = (xn + A/XN)/2

An1_3:Martian Algorithm

#include <stdio.h>int InvSqrt(int x){    float x2 = (float)x;    float xhalf = x2 / 2;    int i = *(int*) & x2;         // get bits for floating VALUE     i = 0x5f375a86 - (i>>1);     // gives initial guess y0    x2 = *(float*) & i;           // convert bits BACK to float    x2 = x2 * (1.5f - xhalf * x2 * x2); // Newton step, repeating increases accuracy    x2 = x2 * (1.5f - xhalf * x2 * x2); // Newton step, repeating increases accuracy    x2 = x2 * (1.5f - xhalf * x2 * x2); // Newton step, repeating increases accuracy    printf("\n\n1/x = %d\n", (int)(1/x2));    return (int)(1/x2);}int main(){    //InvSqrt(65535);    InvSqrt(10);    InvSqrt(2147395599);    InvSqrt(1297532724);    return 0;}

Note:

This method is highly efficient. I use the INT (parameter) written by someone else's float)

In GCC (Linux), the compilation is successful and the test results are correct, but the leetcode compilation fails. The compilation displays the following information:

Run Status: Internal error

Reference recommendations:

Leetcode.com

The murder caused by an SQRT Function

Calculate the square root using Newton Iteration Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.