# 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:

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   ` `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 `

Output:

```10
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : VirusBuddah_, vikas_g