Python returns the square root of an integer based on binary search

Source: Internet
Author: User
This article mainly introduces Python's method for finding the square root of an integer based on binary search, involving Python's binary search algorithm and mathematical computation related skills, for more information, see the example in this article. We will share this with you for your reference. The details are as follows:

X = int (raw_input ('Please input a int: ') if x <0: retrun-1low = 0 high = xans = (low + high) /2.0 sign = answhile ans ** 2! = X: if ans ** 2> x: high = ans else: low = ans = (low + high)/2.0 if sign = ans: breakprint ans


For more Python articles about how to find the square root of an integer based on binary search, refer to the PHP Chinese website!

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.