Given a positive integer **N**, the task is to find the minimum divisor by which it shall be divided to make it a perfect cube. If N is already a perfect cube, then print 1.

**Examples:**

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

Any number is a perfect cube if all prime factors of it appear in multiples of 3, as you can see in the below figure.

Therefore, the idea is to find the prime factorization of N and find power of each prime factor. Now, find and multiply all the prime factors whose power is not divisible by 3 as primeFactor*^{power%3}. The resultant of the multiplication is the answer.

Below is the implementation of the above approach:

## C++

`// C++ program to find minimum number which divide n ` `// to make it a perfect cube ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Check if a number is multiple of 5 without using / and % operators
- Given a number, find the next smallest palindrome
- Make a fair coin from a biased coin
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Program to convert a given number to words
- Efficient program to print all prime factors of a given number
- Check if a number is Palindrome
- Random number generator in arbitrary probability distribution fashion
- Count digits in given number N which divide N
- How to check if a given number is Fibonacci number?
- Program for nth Catalan Number
- Count trailing zeroes in factorial of a number

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.