SQRT (x)

Source: Internet
Author: User
Tags truncated

Implement int sqrt(int x) .

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

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

Example 1:

Input:4output:2

Example 2:

Input:8output:2explanation:the Square root of 8 is 2.82842 ..., and since the            decimal part was truncated, 2 is Retu rned.
1 intMYSQRT (intx) {2     intmid,low=1, high=x;3     if(x<2)4         returnx;5      while(low<=High ) {6         //if the direct mid= (Low+high)/2 words may overflow, the number is large enough in case7mid=low+ (high-low)/2; 8         if(Mid==x/mid) Break;9         Else if(Mid<x/mid) low=mid+1;Ten         Elsehigh=mid-1;  One     } A     if(mid>x/mid) -         returnmid-1; -     Else the         returnmid; -}

Sqrt (x)

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.