Open In App

Min operations to reduce N by multiplying by any number or taking square root

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the minimum value of N by applying below operations any number of times: 

Examples: 
 

Input: N = 20 
Output: 10
Explanation: 
Multiply -> 20 * 5 = 100 
sqrt(100) = 10, which is the minimum value obtainable.

Input: N = 5184 
Output:
Explanation:
sqrt(5184) = 72. 
Multiply -> 72*18 = 1296 
sqrt(1296) = 6, which is the minimum value obtainable.
 

 

Approach: This problem can be solved using Greedy Approach. Below are the steps:

  1. Keep replacing N to sqrt(N) until N is a perfect square.
  2. After the above step, iterate from sqrt(N) to 2, and for every, i keep replacing N with N / i if N is divisible by i2.
  3. The value of N after the above step will be the minimum possible value.

Below is the implementation of the above approach:

C++




// C++ program for above approach
#include <bits/stdc++.h>
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


Javascript




<script>
 
// Javascript program for above approach
 
// Function to reduce N to its minimum
// possible value by the given operations
function minValue(n)
{
 
    // Keep replacing n until is
    // an integer
    while (parseInt(Math.sqrt(n)) == Math.sqrt(n)
        && n > 1)
    {
        n = parseInt(Math.sqrt(n));
    }
 
    // Keep replacing n until n
    // is divisible by i * i
    for (var i = parseInt(Math.sqrt(n));
        i > 1; i--) {
 
        while (n % (i * i) == 0)
            n /= i;
    }
 
    // Print the answer
    document.write(n);
}
 
// Driver Code
 
// Given N
var N = 20;
 
// Function Call
minValue(N);
 
// This code is contributed by rutvik_56.
</script>


Output:

10

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 22 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads