Perfect Cube factors of a Number
Given an integer N, the task is to find the number of factors of N that are a perfect cube.
Examples:
Input: N = 27
Output: 2
Explanation :
There are 2 factors of 27 (1, 27) that are perfect cube
Input: N = 216
Output: 4
Explanation:
There are 4 factors of 216 (1, 8, 27, 216) that are perfect cube
Naive Approach: The naive idea is to find all possible factors of the given number N and count if each factor is a perfect cube or not. If yes then count this factor and check for the next factor.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use mathematical observation to find a formula to calculate the number of factors that are a perfect cube. The number of factors for a number is given by:
Factors of N = (1 + a1)*(1 + a2)*(1 + a3)*..*(1 + an)
where a1, a2, a3, .., an are the count of distinct prime factors of N.
In a perfect cube, the count of distinct prime factors must be divisible by 3. Therefore, the count of factors that are a perfect cube is given by:
Factors of N that are perfect cube = (1 + a1/3)*(1 + a2/3)*…*(1 + an/3)
where a1, a2, a3, .., an are the count of distinct prime factors of N.
Illustration:
The factors of N = 216 are 2, 2, 2, 3, 3, 3.
Therefore, number of factors that are perfect cube are (1 + 3/3) * (1 + 3/3) = 4. The factors are 1, 8, 27, and 216.
Therefore, find the count of prime factors and apply the above formula to find the count of factors that are a perfect cube.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int noOfFactors( int N)
{
if (N == 1)
return 1;
int count = 0;
int ans = 1;
while (N % 2 == 0) {
count++;
N = N / 2;
}
ans *= (count / 3 + 1);
for ( int i = 3; i * i <= N; i = i + 2) {
count = 0;
while (N % i == 0) {
count++;
N = N / i;
}
ans *= (count / 3 + 1);
}
return ans;
}
int main()
{
int N = 216;
cout << noOfFactors(N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int noOfFactors( int N)
{
if (N == 1 )
return 1 ;
int count = 0 ;
int ans = 1 ;
while (N % 2 == 0 )
{
count++;
N = N / 2 ;
}
ans *= (count / 3 + 1 );
for ( int i = 3 ; i * i <= N; i = i + 2 )
{
count = 0 ;
while (N % i == 0 )
{
count++;
N = N / i;
}
ans *= (count / 3 + 1 );
}
return ans;
}
public static void main(String[] args)
{
int N = 216 ;
System.out.print(noOfFactors(N));
}
}
|
Python3
def noofFactors(N):
if N = = 1 :
return 1
count = 0
ans = 1
while (N % 2 = = 0 ):
count + = 1
N / / = 2
ans * = ((count / / 3 ) + 1 )
i = 3
while ((i * i) < = N):
count = 0
while (N % i = = 0 ):
count + = 1
N / / = i
ans * = ((count / / 3 ) + 1 )
i + = 2
return ans
N = 216
print (noofFactors(N))
|
C#
using System;
class GFG{
static int noOfFactors( int N)
{
if (N == 1)
return 1;
int count = 0;
int ans = 1;
while (N % 2 == 0)
{
count++;
N = N / 2;
}
ans *= (count / 3 + 1);
for ( int i = 3; i * i <= N; i = i + 2)
{
count = 0;
while (N % i == 0)
{
count++;
N = N / i;
}
ans *= (count / 3 + 1);
}
return ans;
}
public static void Main(String[] args)
{
int N = 216;
Console.Write(noOfFactors(N));
}
}
|
Javascript
<script>
function noOfFactors(N)
{
if (N == 1)
return 1;
let count = 0;
let ans = 1;
while (N % 2 == 0) {
count++;
N = parseInt(N / 2);
}
ans *= (parseInt(count / 3) + 1);
for (let i = 3; i * i <= N; i = i + 2) {
count = 0;
while (N % i == 0) {
count++;
N = parseInt(N / i);
}
ans *= (parseInt(count / 3) + 1);
}
return ans;
}
let N = 216;
document.write(noOfFactors(N));
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...