📜  使用二进制搜索找到达到给定精度的数的平方根

📅  最后修改于: 2021-04-26 06:26:53             🧑  作者: Mango

给定正数n和精度p,请使用二分查找法找到数字的平方根直至p个小数位。
注意:先决条件:二进制搜索
例子:

Input : number = 50, precision = 3
Output : 7.071

Input : number = 10, precision = 4
Output : 3.1622

我们已经讨论了如何使用二进制搜索来计算平方根中平方根的积分值
方法 :
1)由于number的平方根位于0 <= squareRoot <= number范围内,因此,将start和end初始化为:start = 0,end = number。
2)将中间整数的平方与给定数字进行比较。如果等于数字,那么我们找到了整数部分,否则根据情况在左侧或右侧寻找相同的部分。
3)一旦找到整数部分,就开始计算分数部分。
4)用0.1初始化增量变量,并迭代计算小数部分直到p位。对于每次迭代,增量更改为先前值的1/10。
5)最后返回计算出的答案。
下面是上述方法的实现:

C++
// C++ implementation to find
// square root of given number
// upto given precision using
// binary search.
#include 
using namespace std;
 
// Function to find square root
// of given number upto given
// precision
float squareRoot(int number, int precision)
{
    int start = 0, end = number;
    int mid;
 
    // variable to store the answer
    float ans;
 
    // for computing integral part
    // of square root of number
    while (start <= end) {
        mid = (start + end) / 2;
        if (mid * mid == number) {
            ans = mid;
            break;
        }
 
        // incrementing start if integral
        // part lies on right side of the mid
        if (mid * mid < number) {
            start = mid + 1;
            ans = mid;
        }
 
        // decrementing end if integral part
        // lies on the left side of the mid
        else {
            end = mid - 1;
        }
    }
 
    // For computing the fractional part
    // of square root upto given precision
    float increment = 0.1;
    for (int i = 0; i < precision; i++) {
        while (ans * ans <= number) {
            ans += increment;
        }
 
        // loop terminates when ans * ans > number
        ans = ans - increment;
        increment = increment / 10;
    }
    return ans;
}
 
// Driver code
int main()
{
    // Function calling
    cout << squareRoot(50, 3) << endl;
 
    // Function calling
    cout << squareRoot(10, 4) << endl;
 
    return 0;
}


Java
// Java implementation to find
// square root of given number
// upto given precision using
// binary search.
import java.io.*;
 
class GFG {
     
    // Function to find square root
    // of given number upto given
    // precision
    static float squareRoot(int number, int precision)
    {
        int start = 0, end = number;
        int mid;
 
        // variable to store the answer
        double ans = 0.0;
 
        // for computing integral part
        // of square root of number
        while (start <= end)
        {
            mid = (start + end) / 2;
             
            if (mid * mid == number)
            {
                ans = mid;
                break;
            }
 
            // incrementing start if integral
            // part lies on right side of the mid
            if (mid * mid < number) {
                start = mid + 1;
                ans = mid;
            }
 
            // decrementing end if integral part
            // lies on the left side of the mid
            else {
                end = mid - 1;
            }
        }
 
        // For computing the fractional part
        // of square root upto given precision
        double increment = 0.1;
        for (int i = 0; i < precision; i++) {
            while (ans * ans <= number) {
                ans += increment;
            }
 
            // loop terminates when ans * ans > number
            ans = ans - increment;
            increment = increment / 10;
        }
        return (float)ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Function calling
        System.out.println(squareRoot(50, 3));
 
        // Function calling
        System.out.println(squareRoot(10, 4));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 implementation to find
# square root of given number
# upto given precision using
# binary search.
 
# Function to find square root of
# given number upto given precision
def squareRoot(number, precision):
 
    start = 0
    end,ans = number,1
 
    # For computing integral part
    # of square root of number
    while (start <= end) :
        mid = int((start + end) / 2)
         
        if (mid * mid == number) :
            ans = mid
            break
         
        # incrementing start if integral
        # part lies on right side of the mid
        if (mid * mid < number) :
            start = mid + 1
             
         
        # decrementing end if integral part
        # lies on the left side of the mid
        else :
            end = mid - 1
         
    # For computing the fractional part
    # of square root upto given precision
    increment = 0.1
    for i in range(0, precision):
        while (ans * ans <= number):
            ans += increment
         
        # loop terminates when ans * ans > number
        ans = ans - increment
        increment = increment / 10
     
    return ans
 
# Driver code
print(round(squareRoot(50, 3), 4))
print(round(squareRoot(10, 4), 4))
     
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# implementation to find
// square root of given number
// upto given precision using
// binary search.
using System;
class GFG{
     
// Function to find square root
// of given number upto given
// precision
static float squareRoot(int number,
                        int precision)
{
    int start = 0, end = number;
    int mid;
 
    // variable to store the answer
    double ans = 0.0;
 
    // for computing integral part
    // of square root of number
    while (start <= end)
    {
        mid = (start + end) / 2;
         
        if (mid * mid == number)
        {
            ans = mid;
            break;
        }
 
        // incrementing start if integral
        // part lies on right side of the mid
        if (mid * mid < number)
        {
            start = mid + 1;
            ans = mid;
        }
 
        // decrementing end if integral part
        // lies on the left side of the mid
        else
        {
            end = mid - 1;
        }
    }
 
    // For computing the fractional part
    // of square root upto given precision
    double increment = 0.1;
    for (int i = 0; i < precision; i++)
    {
        while (ans * ans <= number)
        {
            ans += increment;
        }
 
        // loop terminates when ans * ans > number
        ans = ans - increment;
        increment = increment / 10;
    }
    return (float)ans;
}
 
// Driver code
public static void Main()
{
    // Function calling
    Console.WriteLine(squareRoot(50, 3));
 
    // Function calling
    Console.WriteLine(squareRoot(10, 4));
}
}
 
// This code is contributed by Sheharaz Sheikh


PHP
 number
        $ans = $ans - $increment;
        $increment = $increment / 10;
    }
    return $ans;
}
 
    // Driver code
    // Function calling
    echo squareRoot(50, 3),"\n";
 
    // Function calling
    echo squareRoot(10, 4),"\n";
 
// This code is contributed by ajit.
?>


Javascript


输出:

7.071
3.1622

时间复杂度:计算整数部分所需的时间为O(log(number))和常数,即=计算分数部分的精度。因此,总体时间复杂度为O(log(number)+ precision),大约等于O(log(number))。