Minimum divisor of a number to make the number perfect cube
Last Updated :
20 Dec, 2022
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 = 128
Output : 2
By Dividing N by 2, we get 64 which is a perfect cube.
Input : n = 6
Output : 6
By Dividing N by 6, we get 1 which is a perfect cube.
Input : n = 64
Output : 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++
#include <bits/stdc++.h>
using namespace std;
int findMinNumber( int n)
{
int count = 0, ans = 1;
while (n % 2 == 0) {
count++;
n /= 2;
}
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 % 3 != 0)
ans *= pow (i, (count % 3));
}
if (n > 2)
ans *= n;
return ans;
}
int main()
{
int n = 128;
cout << findMinNumber(n) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int findMinNumber( int n)
{
int count = 0 , ans = 1 ;
while (n % 2 == 0 ) {
count++;
n /= 2 ;
}
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 % 3 != 0 )
ans *= Math.pow(i, (count % 3 ));
}
if (n > 2 )
ans *= n;
return ans;
}
public static void main(String[] args)
{
int n = 128 ;
System.out.print(findMinNumber(n) + "\n" );
}
}
|
Python3
def findMinNumber(n):
count = 0 ;
ans = 1 ;
while (n % 2 = = 0 ):
count + = 1 ;
n / = 2 ;
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 % 3 ! = 0 ):
ans * = pow (i, (count % 3 ));
if (n > 2 ):
ans * = n;
return ans;
if __name__ = = '__main__' :
n = 128 ;
print (findMinNumber(n));
|
C#
using System;
class GFG{
static int findMinNumber( int n)
{
int count = 0, ans = 1;
while (n % 2 == 0) {
count++;
n /= 2;
}
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 % 3 != 0)
ans *= ( int )Math.Pow(i, (count % 3));
}
if (n > 2)
ans *= n;
return ans;
}
public static void Main(String[] args)
{
int n = 128;
Console.Write(findMinNumber(n) + "\n" );
}
}
|
Javascript
<script>
function findMinNumber(n)
{
var count = 0, ans = 1;
while (n % 2 == 0) {
count++;
n /= 2;
}
if (count % 3 != 0)
ans *= Math.pow(2, (count % 3));
for ( var i = 3; i <= Math.sqrt(n); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n /= i;
}
if (count % 3 != 0)
ans *= Math.pow(i, (count % 3));
}
if (n > 2)
ans *= n;
return ans;
}
var n = 128;
document.write(findMinNumber(n));
</script>
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...