Sqrt (int x)

Source: Internet
Author: User

Question: Implement int sqrt (int x ). compute and return the square root of x. an1_1: bipartite [cpp] class Solution {public: int sqrt (int x) {if (x <0) return-1; // assert (x> = 0 ); long x2 = (long) x; long left = 0; long right = x2; 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, negative number does not have an open square root 2) value range, mid = left + (right-left) /2; may exceed the maximum int value range. Therefore, you must set the mid type to long (C ++ without ulong) an1_2: Newton iteration method [cpp] 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 accuracy value range (infinitely close to 0). Evaluate f (x) function: transformation 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 where Xn + 1 is infinitely close to Xn, I .e: xn = (Xn + a/Xn)/2 anw.3: Martian algorithm [cpp] # 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;} description: this method is highly efficient. I use the int (parameter) written by someone else's float)

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.