📜  整数的平方根

📅  最后修改于: 2021-04-23 16:18:51             🧑  作者: Mango

给定一个整数x,找到它的平方根。如果x不是理想的正方形,则返回楼板(√x)。

例子 :

Input: x = 4
Output: 2
Explanation:  The square root of 4 is 2.

Input: x = 11
Output: 3
Explanation:  The square root of 11 lies in between
3 and 4 so floor of the square root is 3.

有很多方法可以解决此问题。例如,巴比伦方法是一种方法。
简单方法:要找到平方根的底数,请尝试从1开始的全自然数。继续增加数字,直到该数的平方大于给定数字。

  • 算法:
    1. 创建一个变量(计数器) i并处理一些基本情况,即当给定数字为0或1时。
    2. 循环运行,直到i * i <= n ,其中n是给定的数字。将i递增1。
    3. 数的平方根的底数是i – 1
  • 执行:
C++
// A C++ program to find floor(sqrt(x)
#include
using namespace std;
 
// Returns floor of square root of x
int floorSqrt(int x)
{
    // Base cases
    if (x == 0 || x == 1)
    return x;
 
    // Staring from 1, try all numbers until
    // i*i is greater than or equal to x.
    int i = 1, result = 1;
    while (result <= x)
    {
      i++;
      result = i * i;
    }
    return i - 1;
}
 
// Driver program
int main()
{
    int x = 11;
    cout << floorSqrt(x) << endl;
    return 0;
}


Java
// A Java program to find floor(sqrt(x))
 
class GFG {
     
    // Returns floor of square root of x
    static int floorSqrt(int x)
    {
        // Base cases
        if (x == 0 || x == 1)
            return x;
 
        // Staring from 1, try all numbers until
        // i*i is greater than or equal to x.
        int i = 1, result = 1;
         
        while (result <= x) {
            i++;
            result = i * i;
        }
        return i - 1;
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int x = 11;
        System.out.print(floorSqrt(x));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal.


Python3
# Python3 program to find floor(sqrt(x)
 
# Returns floor of square root of x
def floorSqrt(x):
 
    # Base cases
    if (x == 0 or x == 1):
        return x
 
    # Staring from 1, try all numbers until
    # i*i is greater than or equal to x.
    i = 1; result = 1
    while (result <= x):
     
        i += 1
        result = i * i
     
    return i - 1
 
# Driver Code
x = 11
print(floorSqrt(x))
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// A C# program to
// find floor(sqrt(x))
using System;
 
class GFG
{
    // Returns floor of
    // square root of x
    static int floorSqrt(int x)
    {
        // Base cases
        if (x == 0 || x == 1)
            return x;
 
        // Staring from 1, try all
        // numbers until i*i is
        // greater than or equal to x.
        int i = 1, result = 1;
         
        while (result <= x)
        {
            i++;
            result = i * i;
        }
        return i - 1;
    }
 
    // Driver Code
    static public void Main ()
    {
        int x = 11;
        Console.WriteLine(floorSqrt(x));
    }
}
 
// This code is contributed by ajit


PHP


Javascript


C++
// A C++ program to find floor(sqrt(x)
#include
using namespace std;
 
// Returns floor of square root of x        
int floorSqrt(int x)
{    
    // Base cases
    if (x == 0 || x == 1)
    return x;
 
    // Do Binary Search for floor(sqrt(x))
    int start = 1, end = x, ans;
    while (start <= end)
    {        
        int mid = (start + end) / 2;
 
        // If x is a perfect square
        if (mid*mid == x)
            return mid;
 
        // Since we need floor, we update answer when mid*mid is
        // smaller than x, and move closer to sqrt(x)
        if (mid*mid < x)
        {
            start = mid + 1;
            ans = mid;
        }
        else // If mid*mid is greater than x
            end = mid-1;        
    }
    return ans;
}
 
// Driver program
int main()
{    
    int x = 11;
    cout << floorSqrt(x) << endl;
    return 0;
}


Java
// A Java program to find floor(sqrt(x)
public class Test
{
    public static int floorSqrt(int x)
    {
        // Base Cases
        if (x == 0 || x == 1)
            return x;
 
 
        // Do Binary Search for floor(sqrt(x))
        long start = 1, end = x, ans=0;
        while (start <= end)
        {
            int mid = (start + end) / 2;
 
            // If x is a perfect square
            if (mid*mid == x)
                return (int)mid;
 
            // Since we need floor, we update answer when mid*mid is
            // smaller than x, and move closer to sqrt(x)
            if (mid*mid < x)
            {
                start = mid + 1;
                ans = mid;
            }
            else   // If mid*mid is greater than x
                end = mid-1;
        }
        return (int)ans;
    }
 
    // Driver Method
    public static void main(String args[])
    {
        int x = 11;
        System.out.println(floorSqrt(x));
    }
}
// Contributed by InnerPeace


Python3
# Python 3 program to find floor(sqrt(x)
 
# Returns floor of square root of x        
def floorSqrt(x) :
 
    # Base cases
    if (x == 0 or x == 1) :
        return x
  
    # Do Binary Search for floor(sqrt(x))
    start = 1
    end = x  
    while (start <= end) :
        mid = (start + end) // 2
         
        # If x is a perfect square
        if (mid*mid == x) :
            return mid
             
        # Since we need floor, we update
        # answer when mid*mid is smaller
        # than x, and move closer to sqrt(x)
        if (mid * mid < x) :
            start = mid + 1
            ans = mid
             
        else :
             
            # If mid*mid is greater than x
            end = mid-1
             
    return ans
 
# driver code   
x = 11
print(floorSqrt(x))
     
# This code is contributed by Nikita Tiwari.


C#
// A C# program to
// find floor(sqrt(x)
using System;
 
class GFG
{
    public static int floorSqrt(int x)
    {
        // Base Cases
        if (x == 0 || x == 1)
            return x;
 
        // Do Binary Search
        // for floor(sqrt(x))
        int start = 1, end = x, ans = 0;
        while (start <= end)
        {
            int mid = (start + end) / 2;
 
            // If x is a
            // perfect square
            if (mid * mid == x)
                return mid;
 
            // Since we need floor, we
            // update answer when mid *
            // mid is smaller than x,
            // and move closer to sqrt(x)
            if (mid * mid < x)
            {
                start = mid + 1;
                ans = mid;
            }
             
            // If mid*mid is
            // greater than x
            else
                end = mid-1;
        }
        return ans;
    }
 
    // Driver Code
    static public void Main ()
    {
        int x = 11;
        Console.WriteLine(floorSqrt(x));
    }
}
 
// This code is Contributed by m_kit


PHP


输出 :

3
  • 复杂度分析:
    • 时间复杂度: O(√n)。
      该解决方案仅需要遍历一次,因此时间复杂度为O(√n)。
    • 空间复杂度: O(1)。
      需要恒定的额外空间。

感谢Fattepur Mahesh提出了此解决方案。
更好的方法想法是找到平方小于或等于给定数字的最大整数i 。这个想法是使用二进制搜索来解决这个问题。 i * i的值单调增加,因此可以使用二进制搜索解决问题。

  • 算法:
    1. 请注意一些基本情况,即给定数字为0或1时。
    2. 创建一些变量,lowerbound l = 0 ,upperbound r = n ,其中n是给定的数字, midans来存储答案。
    3. 循环运行直到l <= r ,搜索空间消失
    4. 检查mid( mid =(l + r)/ 2 )的平方是否小于或等于n,如果是,则在搜索空间的后半部分搜索较大的值,即l = mid + 1,更新ans =中
    5. 否则,如果mid的平方小于n,则在oF搜索空间的前半部分搜索较小的值,即r = mid – 1
    6. 打印答案的值( ans )
  • 执行:

C++

// A C++ program to find floor(sqrt(x)
#include
using namespace std;
 
// Returns floor of square root of x        
int floorSqrt(int x)
{    
    // Base cases
    if (x == 0 || x == 1)
    return x;
 
    // Do Binary Search for floor(sqrt(x))
    int start = 1, end = x, ans;
    while (start <= end)
    {        
        int mid = (start + end) / 2;
 
        // If x is a perfect square
        if (mid*mid == x)
            return mid;
 
        // Since we need floor, we update answer when mid*mid is
        // smaller than x, and move closer to sqrt(x)
        if (mid*mid < x)
        {
            start = mid + 1;
            ans = mid;
        }
        else // If mid*mid is greater than x
            end = mid-1;        
    }
    return ans;
}
 
// Driver program
int main()
{    
    int x = 11;
    cout << floorSqrt(x) << endl;
    return 0;
}

Java

// A Java program to find floor(sqrt(x)
public class Test
{
    public static int floorSqrt(int x)
    {
        // Base Cases
        if (x == 0 || x == 1)
            return x;
 
 
        // Do Binary Search for floor(sqrt(x))
        long start = 1, end = x, ans=0;
        while (start <= end)
        {
            int mid = (start + end) / 2;
 
            // If x is a perfect square
            if (mid*mid == x)
                return (int)mid;
 
            // Since we need floor, we update answer when mid*mid is
            // smaller than x, and move closer to sqrt(x)
            if (mid*mid < x)
            {
                start = mid + 1;
                ans = mid;
            }
            else   // If mid*mid is greater than x
                end = mid-1;
        }
        return (int)ans;
    }
 
    // Driver Method
    public static void main(String args[])
    {
        int x = 11;
        System.out.println(floorSqrt(x));
    }
}
// Contributed by InnerPeace

Python3

# Python 3 program to find floor(sqrt(x)
 
# Returns floor of square root of x        
def floorSqrt(x) :
 
    # Base cases
    if (x == 0 or x == 1) :
        return x
  
    # Do Binary Search for floor(sqrt(x))
    start = 1
    end = x  
    while (start <= end) :
        mid = (start + end) // 2
         
        # If x is a perfect square
        if (mid*mid == x) :
            return mid
             
        # Since we need floor, we update
        # answer when mid*mid is smaller
        # than x, and move closer to sqrt(x)
        if (mid * mid < x) :
            start = mid + 1
            ans = mid
             
        else :
             
            # If mid*mid is greater than x
            end = mid-1
             
    return ans
 
# driver code   
x = 11
print(floorSqrt(x))
     
# This code is contributed by Nikita Tiwari.

C#

// A C# program to
// find floor(sqrt(x)
using System;
 
class GFG
{
    public static int floorSqrt(int x)
    {
        // Base Cases
        if (x == 0 || x == 1)
            return x;
 
        // Do Binary Search
        // for floor(sqrt(x))
        int start = 1, end = x, ans = 0;
        while (start <= end)
        {
            int mid = (start + end) / 2;
 
            // If x is a
            // perfect square
            if (mid * mid == x)
                return mid;
 
            // Since we need floor, we
            // update answer when mid *
            // mid is smaller than x,
            // and move closer to sqrt(x)
            if (mid * mid < x)
            {
                start = mid + 1;
                ans = mid;
            }
             
            // If mid*mid is
            // greater than x
            else
                end = mid-1;
        }
        return ans;
    }
 
    // Driver Code
    static public void Main ()
    {
        int x = 11;
        Console.WriteLine(floorSqrt(x));
    }
}
 
// This code is Contributed by m_kit

的PHP


输出 :

3
  • 复杂度分析:
    • 时间复杂度: O(log n)。
      二进制搜索的时间复杂度为O(log n)。
    • 空间复杂度: O(1)。
      需要恒定的额外空间。