📜  通过乘以任意数或取平方根来减少N的最小运算

📅  最后修改于: 2021-05-05 03:10:29             🧑  作者: Mango

给定数字N ,任务是通过多次应用以下运算来找到N的最小值:

  • N乘以任何正整数
  • 仅当N是一个完美的正方形时,才用sqrt(N)替换N。

例子:

方法:可以使用贪婪方法解决此问题。步骤如下:

  1. 继续将N替换为sqrt(N),直到N是一个完美的正方形为止。
  2. 完成上述步骤后,从sqrt(N)迭代到2 ,并且对于每一次,如果N被i 2整除,我将继续用N / i替换N。
  3. 在上述步骤之后的N值将是最小可能值。

下面是上述方法的实现:

C++
// C++ program for above approach 
#include  
using namespace std; 
  
// Function to reduce N to its minimum 
// possible value by the given operations 
void minValue(int n) 
{ 
    // Keep replacing n until is 
    // an integer 
    while (int(sqrt(n)) == sqrt(n) 
        && n > 1) { 
        n = sqrt(n); 
    } 
  
    // Keep replacing n until n 
    // is divisible by i * i 
    for (int i = sqrt(n); 
        i > 1; i--) { 
  
        while (n % (i * i) == 0) 
            n /= i; 
    } 
  
    // Print the answer 
    cout << n; 
} 
  
// Driver Code 
int main() 
{ 
    // Given N 
    int N = 20; 
  
    // Function Call 
    minValue(N); 
}


Java
// Java implementation of the above approach 
import java.lang.Math; 
  
class GFG{ 
  
// Function to reduce N to its minimum 
// possible value by the given operations 
static void minValue(int n) 
{ 
      
    // Keep replacing n until is 
    // an integer 
    while ((int)Math.sqrt(n) == 
                Math.sqrt(n) && n > 1) 
    { 
        n = (int)(Math.sqrt(n)); 
    } 
  
    // Keep replacing n until n 
    // is divisible by i * i 
    for(int i = (int)(Math.sqrt(n)); 
            i > 1; i--)
    { 
        while (n % (i * i) == 0) 
            n /= i;
    } 
      
    // Print the answer 
    System.out.println(n); 
}
  
// Driver code 
public static void main(String args[])
{
      
    // Given N 
    int N = 20; 
      
    // Function call 
    minValue(N); 
} 
}
  
// This code is contributed by vikas_g


Python3
# Python3 program for the above approach 
import math 
  
# Function to reduce N to its minimum 
# possible value by the given operations 
def MinValue(n):
      
    # Keep replacing n until is 
    # an integer 
    while(int(math.sqrt(n)) == 
              math.sqrt(n) and n > 1):
        n = math.sqrt(n)
          
    # Keep replacing n until n 
    # is divisible by i * i 
    for i in range(int(math.sqrt(n)), 1, -1):
        while (n % (i * i) == 0):
            n /= i
              
    # Print the answer 
    print(n)
  
# Driver code
n = 20
  
# Function call
MinValue(n)
  
# This code is contributed by virusbuddah_


C#
// C# implementation of the approach 
using System; 
  
class GFG{ 
      
// Function to reduce N to its minimum 
// possible value by the given operations 
static void minValue(int n) 
{ 
      
    // Keep replacing n until is 
    // an integer 
    while ((int)Math.Sqrt(n) == 
                Math.Sqrt(n) && n > 1)
    { 
        n = (int)(Math.Sqrt(n)); 
    } 
      
    // Keep replacing n until n 
    // is divisible by i * i 
    for (int i = (int)(Math.Sqrt(n));
             i > 1; i--)
    { 
        while (n % (i * i) == 0) 
            n /= i; 
    } 
      
    // Print the answer 
    Console.Write(n); 
}
  
// Driver code 
public static void Main() 
{
      
    // Given N 
    int N = 20; 
      
    // Function call 
    minValue(N);
}
}
  
// This code is contributed by vikas_g


输出:
10

时间复杂度: O(N)
辅助空间: O(1)