📌  相关文章
📜  数字的最小除数,以使数字成为完美的立方

📅  最后修改于: 2021-04-29 17:19:52             🧑  作者: Mango

给定正整数N ,任务是找到最小除数,将其除以使其成为一个完美的立方。如果N已经是一个理想的立方体,则打印1。
例子:

Input : N = 128
Output : 2
By Dividing N by 2, we get 64 which is a perfect cube.

Input : n = 6
Output : 6
By Dividing N by 6, we get 1 which is a perfect cube.

Input : n = 64
Output : 1

如果所有数字的所有素数都以3的倍数出现,那么任何数字都是一个理想的立方体,如下图所示。

因此,该想法是找到N的素因数分解并找到每个素因数的幂。现在,找到并乘以其幂不能被3整除的所有素因子,作为primeFactor * power%3 。乘法的结果就是答案。
下面是上述方法的实现:

C++
// C++ program to find minimum number which divide n
// to make it a perfect cube
#include 
using namespace std;
 
// Returns the minimum divisor
int findMinNumber(int n)
{
    int count = 0, ans = 1;
 
    // Since 2 is only even prime, compute its
    // power seprately.
    while (n % 2 == 0) {
        count++;
        n /= 2;
    }
 
    // If count is not divisible by 3,
    // it must be removed by dividing
    // n by prime number power.
    if (count % 3 != 0)
        ans *= pow(2, (count % 3));
 
    for (int i = 3; i <= sqrt(n); i += 2) {
        count = 0;
        while (n % i == 0) {
            count++;
            n /= i;
        }
 
        // If count is not divisible by 3,
        // it must be removed by dividing
        // n by prime number power.
        if (count % 3 != 0)
            ans *= pow(i, (count % 3));
    }
 
    // if n is a prime number
    if (n > 2)
        ans *= n;
 
    return ans;
}
 
// Driven Program
int main()
{
    int n = 128;
    cout << findMinNumber(n) << endl;
    return 0;
}


Java
// Java program to find minimum number which divide n
// to make it a perfect cube
class GFG{
  
// Returns the minimum divisor
static int findMinNumber(int n)
{
    int count = 0, ans = 1;
  
    // Since 2 is only even prime, compute its
    // power seprately.
    while (n % 2 == 0) {
        count++;
        n /= 2;
    }
  
    // If count is not divisible by 3,
    // it must be removed by dividing
    // n by prime number power.
    if (count % 3 != 0)
        ans *= Math.pow(2, (count % 3));
  
    for (int i = 3; i <= Math.sqrt(n); i += 2) {
        count = 0;
        while (n % i == 0) {
            count++;
            n /= i;
        }
  
        // If count is not divisible by 3,
        // it must be removed by dividing
        // n by prime number power.
        if (count % 3 != 0)
            ans *= Math.pow(i, (count % 3));
    }
  
    // if n is a prime number
    if (n > 2)
        ans *= n;
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 128;
    System.out.print(findMinNumber(n) +"\n");
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find minimum number which divide n
# to make it a perfect cube
 
# Returns the minimum divisor
def findMinNumber(n):
    count = 0;
    ans = 1;
 
    # Since 2 is only even prime, compute its
    # power seprately.
    while (n % 2 == 0):
        count+=1;
        n /= 2;
     
    # If count is not divisible by 3,
    # it must be removed by dividing
    # n by prime number power.
    if (count % 3 != 0):
        ans *= pow(2, (count % 3));
 
    for i in range(3, int(pow(n, 1/2)), 2):
        count = 0;
        while (n % i == 0):
            count += 1;
            n /= i;
         
        # If count is not divisible by 3,
        # it must be removed by dividing
        # n by prime number power.
        if (count % 3 != 0):
            ans *= pow(i, (count % 3));
     
    # if n is a prime number
    if (n > 2):
        ans *= n;
 
    return ans;
 
# Driver code
if __name__ == '__main__':
    n = 128;
    print(findMinNumber(n));
 
# This code is contributed by 29AjayKumar


C#
// C# program to find minimum number which divide n
// to make it a perfect cube
using System;
 
class GFG{
   
// Returns the minimum divisor
static int findMinNumber(int n)
{
    int count = 0, ans = 1;
   
    // Since 2 is only even prime, compute its
    // power seprately.
    while (n % 2 == 0) {
        count++;
        n /= 2;
    }
   
    // If count is not divisible by 3,
    // it must be removed by dividing
    // n by prime number power.
    if (count % 3 != 0)
        ans *= (int)Math.Pow(2, (count % 3));
   
    for (int i = 3; i <= Math.Sqrt(n); i += 2) {
        count = 0;
        while (n % i == 0) {
            count++;
            n /= i;
        }
   
        // If count is not divisible by 3,
        // it must be removed by dividing
        // n by prime number power.
        if (count % 3 != 0)
            ans *= (int)Math.Pow(i, (count % 3));
    }
   
    // if n is a prime number
    if (n > 2)
        ans *= n;
   
    return ans;
}
   
// Driver code
public static void Main(String[] args)
{
    int n = 128;
    Console.Write(findMinNumber(n) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度: O(sqrt(n))

辅助空间: O(1)