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

Given a number N, the task is to find the minimum value of N by applying below operations any number of times:
- Multiply N by any positive integer
- Replace N with sqrt(N), only if N is a perfect square.
Examples:
Input: N = 20
Output: 10
Explanation:
Multiply -> 20 * 5 = 100
sqrt(100) = 10, which is the minimum value obtainable.Input: N = 5184
Output: 6
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:
- Keep replacing N to sqrt(N) until N is a perfect square.
- 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.
- 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 coden = 20# Function callMinValue(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)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



