📜  数的第五根

📅  最后修改于: 2021-05-04 10:35:00             🧑  作者: Mango

给定一个数字,则打印数字为5的根。
例子:

Input  : n = 32
Output : 2
2 raise to power 5 is 32

Input  : n = 250
Output : 3
Fifth square root of 250 is between 3 and 4
So floor value is 3.

方法1(简单)
一个简单的解决方案是将结果初始化为0,在结果5小于或等于n时保持递增结果。最终返回结果-1。

C++
// A C++ program to find floor of 5th root
#include
using namespace std;
 
// Returns floor of 5th root of n
int floorRoot5(int n)
{
    // Base cases
    if (n == 0 || n == 1)
        return n;
 
    // Initialize result
    int res = 0;
 
    // Keep incrementing res while res^5 is
    // smaller than or equal to n
    while (res*res*res*res*res <= n)
        res++;
 
    // Return floor of 5'th root
    return res-1;
}
 
// Driver program
int main()
{
    int n = 250;
    cout << "Floor of 5'th root is "
         << floorRoot5(n);
    return 0;
}


Java
// Java program to find floor of 5th root
 
class GFG {
     
// Returns floor of 5th root of n
static int floorRoot5(int n)
{
     
    // Base cases
    if (n == 0 || n == 1)
        return n;
 
    // Initialize result
    int res = 0;
 
    // Keep incrementing res while res^5
    // is smaller than or equal to n
    while (res * res * res * res * res <= n)
        res++;
 
    // Return floor of 5'th root
    return res-1;
}
 
    // Driver Code
    public static void main(String []args)
    {
        int n = 250;
        System.out.println("Floor of 5'th root is "
                            + floorRoot5(n));
    }
}
 
// This code is contributed by Anshul Aggarwal.


Python3
# A Python3 program to find the floor
# of the 5th root
 
# Returns floor of 5th root of n
def floorRoot5(n):
 
    # Base cases
    if n == 0 and n == 1:
        return n
 
    # Initialize result
    res = 0
 
    # Keep incrementing res while res^5
    # is smaller than or equal to n
    while res * res * res * res * res <= n:
        res += 1
 
    # Return floor of 5'th root
    return res-1
 
# Driver Code
if __name__ == "__main__":
 
    n = 250
    print("Floor of 5'th root is",
                    floorRoot5(n))
 
# This code is contributed by Rituraj Jain


C#
// C# program to find floor of 5th root
using System;
 
class GFG {
     
// Returns floor of 5th root of n
static int floorRoot5(int n)
{
     
    // Base cases
    if (n == 0 || n == 1)
        return n;
 
    // Initialize result
    int res = 0;
 
    // Keep incrementing res while res^5
    // is smaller than or equal to n
    while (res * res * res * res * res <= n)
        res++;
 
    // Return floor of 5'th root
    return res-1;
}
 
    // Driver Code
    public static void Main()
    {
        int n = 250;
        Console.Write("Floor of 5'th root is "
                       + floorRoot5(n));
    }
}
 
// This code is contributed by Sumit Sudhakar.


PHP


Javascript


C++
// A C++ program to find floor of 5'th root
#include
using namespace std;
 
// Returns floor of 5'th root of n
int floorRoot5(int n)
{
    // Base cases
    if (n == 0 || n == 1)
       return n;
 
    // Do Binary Search for floor of 5th square root
    int low = 1, high = n, ans = 0;
    while (low <= high)
    {
        // Find the middle point and its power 5
        int mid = (low + high) / 2;
        long int mid5 = mid*mid*mid*mid*mid;
 
        // If mid is the required root
        if (mid5 == n)
            return mid;
 
        // Since we need floor, we update answer when
        // mid5 is smaller than n, and move closer to
        // 5'th root
        if (mid5 < n)
        {
            low = mid + 1;
            ans = mid;
        }
        else // If mid^5 is greater than n
            high = mid - 1;
    }
    return ans;
}
 
// Driver program
int main()
{
    int n = 250;
    cout << "Floor of 5'th root is "
         << floorRoot5(n);
    return 0;
}


Java
// A Java program to find
// floor of 5'th root
 
class GFG {
     
    // Returns floor of 5'th
    // root of n
    static int floorRoot5(int n)
    {
         
        // Base cases
        if (n == 0 || n == 1)
        return n;
     
        // Do Binary Search for
        // floor of 5th square root
        int low = 1, high = n, ans = 0;
        while (low <= high)
        {
             
            // Find the middle point
            // and its power 5
            int mid = (low + high) / 2;
            long mid5 = mid * mid * mid *
                            mid * mid;
     
            // If mid is the required root
            if (mid5 == n)
                return mid;
     
            // Since we need floor,
            // we update answer when
            // mid5 is smaller than n,
            // and move closer to
            // 5'th root
            if (mid5 < n)
            {
                low = mid + 1;
                ans = mid;
            }
             
            // If mid^5 is greater
            // than n
            else
                high = mid - 1;
        }
        return ans;
    }
     
    // Driver Code
    public static void main(String []args)
    {
        int n = 250;
        System.out.println("Floor of 5'th root is " +
                                     floorRoot5(n));
    }
}
 
// This code is contributed by Anshul Aggarwal.


Python3
# A Python3 program to find the floor
# of 5'th root
 
# Returns floor of 5'th root of n
def floorRoot5(n):
 
    # Base cases
    if n == 0 or n == 1:
        return n
 
    # Do Binary Search for floor of
    # 5th square root
    low, high, ans = 1, n, 0
    while low <= high:
     
        # Find the middle point and its power 5
        mid = (low + high) // 2
        mid5 = mid * mid * mid * mid * mid
 
        # If mid is the required root
        if mid5 == n:
            return mid
 
        # Since we need floor, we update answer
        # when mid5 is smaller than n, and move
        # closer to 5'th root
        if mid5 < n:
         
            low = mid + 1
            ans = mid
         
        else: # If mid^5 is greater than n
            high = mid - 1
     
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    n = 250
    print("Floor of 5'th root is", floorRoot5(n))
 
# This code is contributed by Rituraj Jain


C#
// A C# program to find
// floor of 5'th root
using System;
 
class GFG {
     
    // Returns floor of 5'th
    // root of n
    static int floorRoot5(int n)
    {
         
        // Base cases
        if (n == 0 || n == 1)
        return n;
     
        // Do Binary Search for
        // floor of 5th square root
        int low = 1, high = n, ans = 0;
        while (low <= high)
        {
             
            // Find the middle point
            // and its power 5
            int mid = (low + high) / 2;
            long mid5 = mid * mid * mid *
                            mid * mid;
     
            // If mid is the required root
            if (mid5 == n)
                return mid;
     
            // Since we need floor,
            // we update answer when
            // mid5 is smaller than n,
            // and move closer to
            // 5'th root
            if (mid5 < n)
            {
                low = mid + 1;
                ans = mid;
            }
             
            // If mid^5 is greater
            // than n
            else
                high = mid - 1;
        }
        return ans;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        int n = 250;
        Console.WriteLine("Floor of 5'th root is " +
                                     floorRoot5(n));
    }
}
 
// This code is contributed by Anshul Aggarwal.


PHP


Javascript


输出:

Floor of 5'th root is 3

上述解决方案的时间复杂度为O(n 1/5 ) 。我们可以做得更好。请参阅以下解决方案。
方法2(二元搜索)
这个想法是做二进制搜索。我们从n / 2开始,如果它的5次方大于n,则从n / 2 + 1到n的间隔递归。否则,如果功率较小,则以0到n / 2-1的间隔递归

C++

// A C++ program to find floor of 5'th root
#include
using namespace std;
 
// Returns floor of 5'th root of n
int floorRoot5(int n)
{
    // Base cases
    if (n == 0 || n == 1)
       return n;
 
    // Do Binary Search for floor of 5th square root
    int low = 1, high = n, ans = 0;
    while (low <= high)
    {
        // Find the middle point and its power 5
        int mid = (low + high) / 2;
        long int mid5 = mid*mid*mid*mid*mid;
 
        // If mid is the required root
        if (mid5 == n)
            return mid;
 
        // Since we need floor, we update answer when
        // mid5 is smaller than n, and move closer to
        // 5'th root
        if (mid5 < n)
        {
            low = mid + 1;
            ans = mid;
        }
        else // If mid^5 is greater than n
            high = mid - 1;
    }
    return ans;
}
 
// Driver program
int main()
{
    int n = 250;
    cout << "Floor of 5'th root is "
         << floorRoot5(n);
    return 0;
}

Java

// A Java program to find
// floor of 5'th root
 
class GFG {
     
    // Returns floor of 5'th
    // root of n
    static int floorRoot5(int n)
    {
         
        // Base cases
        if (n == 0 || n == 1)
        return n;
     
        // Do Binary Search for
        // floor of 5th square root
        int low = 1, high = n, ans = 0;
        while (low <= high)
        {
             
            // Find the middle point
            // and its power 5
            int mid = (low + high) / 2;
            long mid5 = mid * mid * mid *
                            mid * mid;
     
            // If mid is the required root
            if (mid5 == n)
                return mid;
     
            // Since we need floor,
            // we update answer when
            // mid5 is smaller than n,
            // and move closer to
            // 5'th root
            if (mid5 < n)
            {
                low = mid + 1;
                ans = mid;
            }
             
            // If mid^5 is greater
            // than n
            else
                high = mid - 1;
        }
        return ans;
    }
     
    // Driver Code
    public static void main(String []args)
    {
        int n = 250;
        System.out.println("Floor of 5'th root is " +
                                     floorRoot5(n));
    }
}
 
// This code is contributed by Anshul Aggarwal.

Python3

# A Python3 program to find the floor
# of 5'th root
 
# Returns floor of 5'th root of n
def floorRoot5(n):
 
    # Base cases
    if n == 0 or n == 1:
        return n
 
    # Do Binary Search for floor of
    # 5th square root
    low, high, ans = 1, n, 0
    while low <= high:
     
        # Find the middle point and its power 5
        mid = (low + high) // 2
        mid5 = mid * mid * mid * mid * mid
 
        # If mid is the required root
        if mid5 == n:
            return mid
 
        # Since we need floor, we update answer
        # when mid5 is smaller than n, and move
        # closer to 5'th root
        if mid5 < n:
         
            low = mid + 1
            ans = mid
         
        else: # If mid^5 is greater than n
            high = mid - 1
     
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    n = 250
    print("Floor of 5'th root is", floorRoot5(n))
 
# This code is contributed by Rituraj Jain

C#

// A C# program to find
// floor of 5'th root
using System;
 
class GFG {
     
    // Returns floor of 5'th
    // root of n
    static int floorRoot5(int n)
    {
         
        // Base cases
        if (n == 0 || n == 1)
        return n;
     
        // Do Binary Search for
        // floor of 5th square root
        int low = 1, high = n, ans = 0;
        while (low <= high)
        {
             
            // Find the middle point
            // and its power 5
            int mid = (low + high) / 2;
            long mid5 = mid * mid * mid *
                            mid * mid;
     
            // If mid is the required root
            if (mid5 == n)
                return mid;
     
            // Since we need floor,
            // we update answer when
            // mid5 is smaller than n,
            // and move closer to
            // 5'th root
            if (mid5 < n)
            {
                low = mid + 1;
                ans = mid;
            }
             
            // If mid^5 is greater
            // than n
            else
                high = mid - 1;
        }
        return ans;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        int n = 250;
        Console.WriteLine("Floor of 5'th root is " +
                                     floorRoot5(n));
    }
}
 
// This code is contributed by Anshul Aggarwal.

的PHP


Java脚本


输出:

Floor of 5'th root is 3