Given an integer **N, **the task is to find the number of factors of **N** that are a perfect cube.

**Examples:**

Input:N = 27Output:2Explanation :

There are 2 factors of 27 (1, 27) that are perfect cube

Input:N = 216Output:4Explanation:

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 + a

_{1})*(1 + a_{2})*(1 + a_{3})*..*(1 + a_{n})

where a_{1}, a_{2}, a_{3}, .., a_{n}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 + a

_{1}/3)*(1 + a_{2}/3)*…*(1 + a_{n}/3)

where a_{1}, a_{2}, a_{3}, .., a_{n}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++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the count ` `// of factors that are perfect cube ` `int` `noOfFactors(` `int` `N) ` `{ ` ` ` `if` `(N == 1) ` ` ` `return` `1; ` ` ` ` ` `// To store the count of number ` ` ` `// of times a prime number ` ` ` `// divides N. ` ` ` `int` `count = 0; ` ` ` ` ` `// To store the number of factors ` ` ` `// that are perfect cube ` ` ` `int` `ans = 1; ` ` ` ` ` `// Count number of 2's that divides N ` ` ` `while` `(N % 2 == 0) { ` ` ` `count++; ` ` ` `N = N / 2; ` ` ` `} ` ` ` ` ` `// Calculate ans according ` ` ` `// to above formula ` ` ` `ans *= (count / 3 + 1); ` ` ` ` ` `// Check for all the possible ` ` ` `// numbers that can divide it ` ` ` `for` `(` `int` `i = 3; i * i <= N; i = i + 2) { ` ` ` `count = 0; ` ` ` ` ` `// Loop to check the number ` ` ` `// of times prime number ` ` ` `// i divides it ` ` ` `while` `(N % i == 0) { ` ` ` `count++; ` ` ` `N = N / i; ` ` ` `} ` ` ` ` ` `// Calculate ans according ` ` ` `// to above formula ` ` ` `ans *= (count / 3 + 1); ` ` ` `} ` ` ` ` ` `// Return final count ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given number ` ` ` `int` `N = 216; ` ` ` ` ` `// Function Call ` ` ` `cout << noOfFactors(N); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function that returns the count ` `// of factors that are perfect cube ` `static` `int` `noOfFactors(` `int` `N) ` `{ ` ` ` `if` `(N == ` `1` `) ` ` ` `return` `1` `; ` ` ` ` ` `// To store the count of number ` ` ` `// of times a prime number ` ` ` `// divides N. ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// To store the number of factors ` ` ` `// that are perfect cube ` ` ` `int` `ans = ` `1` `; ` ` ` ` ` `// Count number of 2's that divides N ` ` ` `while` `(N % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `count++; ` ` ` `N = N / ` `2` `; ` ` ` `} ` ` ` ` ` `// Calculate ans according ` ` ` `// to above formula ` ` ` `ans *= (count / ` `3` `+ ` `1` `); ` ` ` ` ` `// Check for all the possible ` ` ` `// numbers that can divide it ` ` ` `for` `(` `int` `i = ` `3` `; i * i <= N; i = i + ` `2` `) ` ` ` `{ ` ` ` `count = ` `0` `; ` ` ` ` ` `// Loop to check the number ` ` ` `// of times prime number ` ` ` `// i divides it ` ` ` `while` `(N % i == ` `0` `) ` ` ` `{ ` ` ` `count++; ` ` ` `N = N / i; ` ` ` `} ` ` ` ` ` `// Calculate ans according ` ` ` `// to above formula ` ` ` `ans *= (count / ` `3` `+ ` `1` `); ` ` ` `} ` ` ` ` ` `// Return final count ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given number ` ` ` `int` `N = ` `216` `; ` ` ` ` ` `// Function call ` ` ` `System.out.print(noOfFactors(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function that returns the count ` `# of factors that are perfect cube ` `def` `noofFactors(N): ` ` ` ` ` `if` `N ` `=` `=` `1` `: ` ` ` `return` `1` ` ` ` ` `# To store the count of number ` ` ` `# of times a prime number ` ` ` `# divides N ` ` ` `count ` `=` `0` ` ` ` ` `# To store the count of factors that ` ` ` `# are perfect cube ` ` ` `ans ` `=` `1` ` ` ` ` `# Count number of 2's that divides N ` ` ` `while` `(N ` `%` `2` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` ` ` `N ` `/` `/` `=` `2` ` ` ` ` `# Calculate ans according ` ` ` `# to above formula ` ` ` `ans ` `*` `=` `((count ` `/` `/` `3` `) ` `+` `1` `) ` ` ` ` ` `# Check for all possible ` ` ` `# numbers that can divide it ` ` ` `i ` `=` `3` ` ` `while` `((i ` `*` `i) <` `=` `N): ` ` ` `count ` `=` `0` ` ` ` ` `# Loop to check the number ` ` ` `# of times prime number ` ` ` `# i divides it ` ` ` `while` `(N ` `%` `i ` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` ` ` `N ` `/` `/` `=` `i ` ` ` ` ` `# Calculate ans according ` ` ` `# to above formula ` ` ` `ans ` `*` `=` `((count ` `/` `/` `3` `) ` `+` `1` `) ` ` ` `i ` `+` `=` `2` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` ` ` `# Given number ` `N ` `=` `216` ` ` `# Function call ` `print` `(noofFactors(N)) ` ` ` `# This code is contributed by VirusBuddah_ ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that returns the count ` `// of factors that are perfect cube ` `static` `int` `noOfFactors(` `int` `N) ` `{ ` ` ` `if` `(N == 1) ` ` ` `return` `1; ` ` ` ` ` `// To store the count of number ` ` ` `// of times a prime number ` ` ` `// divides N. ` ` ` `int` `count = 0; ` ` ` ` ` `// To store the number of factors ` ` ` `// that are perfect cube ` ` ` `int` `ans = 1; ` ` ` ` ` `// Count number of 2's that divides N ` ` ` `while` `(N % 2 == 0) ` ` ` `{ ` ` ` `count++; ` ` ` `N = N / 2; ` ` ` `} ` ` ` ` ` `// Calculate ans according ` ` ` `// to above formula ` ` ` `ans *= (count / 3 + 1); ` ` ` ` ` `// Check for all the possible ` ` ` `// numbers that can divide it ` ` ` `for` `(` `int` `i = 3; i * i <= N; i = i + 2) ` ` ` `{ ` ` ` `count = 0; ` ` ` ` ` `// Loop to check the number ` ` ` `// of times prime number ` ` ` `// i divides it ` ` ` `while` `(N % i == 0) ` ` ` `{ ` ` ` `count++; ` ` ` `N = N / i; ` ` ` `} ` ` ` ` ` `// Calculate ans according ` ` ` `// to above formula ` ` ` `ans *= (count / 3 + 1); ` ` ` `} ` ` ` ` ` `// Return final count ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given number ` ` ` `int` `N = 216; ` ` ` ` ` `// Function call ` ` ` `Console.Write(noOfFactors(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(log(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.

## Recommended Posts:

- Previous perfect square and cube number smaller than number N
- Maximum number of prime factors a number can have with exactly x factors
- Number of times the largest Perfect Cube can be subtracted from N
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Check if number formed by joining two Numbers is Perfect Cube
- C Program to check whether a number is a Perfect Cube or not
- Find number of factors of N when location of its two factors whose product is N is given
- Check if a number exists having exactly N factors and K prime factors
- Smallest perfect cube in an array
- Count of pairs in an Array whose sum is a Perfect Cube
- Perfect Cube
- Print N numbers such that their product is a Perfect Cube
- Construct an Array such that cube sum of all element is a perfect square
- Count of subarrays having sum as a perfect cube
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Number of factors of very large number N modulo M where M is any prime number
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Queries on sum of odd number digit sums of all the factors of a number
- Number which has the maximum number of distinct prime factors in the range M to N

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.