# Leet Code # SQRT

Source: Internet
Author: User

Description: log (n)

Code:

 1 class Solution: 2     # @param x, an integer 3     # @return an integer 4     def getVal(self, begin, end, x): 5         if end == begin : 6             return begin 7         if end == begin + 1: 8             return begin 9 10         while True:11             mid = (begin + end) / 212             tmp = mid * mid 13 14             if tmp == x:15                 return mid16             elif tmp < x: 17                 return self.getVal(mid, end, x)18             elif tmp > x:19                 return self.getVal(begin, mid, x)20 21     def sqrt(self, x):22         if x == 0: return 023 24         val = 1 25         while True:26             tmp  = val * val27 28             if tmp == x:29                 return val30             elif tmp > x:31                 return self.getVal(val/2, val, x)32             else:33                 val *= 2

 

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.