VOOZH about

URL: https://www.geeksforgeeks.org/dsa/perfect-cube/

⇱ Check if given number is perfect cube - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if given number is perfect cube

Last Updated : 12 Jul, 2025

Given a number N, the task is to check whether the given number N is a perfect cube or not. 
Examples:

Input: N = 216 
Output: Yes 
Explanation:
As 216 = 6*6*6. Therefore the cube root of 216 is 6.

Input: N = 100 
Output: No 

Naive Approach:

The idea is to check for each number from 1 to N if the cube of any of these numbers equals N. If so, then that number is the cube root of N and the N is a perfect cube.

Below is the implementation of the above approach:  


Output
Yes

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

Using Build-in Functions:

The idea is to use the inbuilt function (cbrt()) to find the cube root of a number which returns floor value of the cube root of the number N. If the cube of this number equals N, then N is a perfect cube otherwise N is not a perfect cube.

Below is the implementation of the above approach: 


Output
Yes

Time Complexity: O(log N) because using inbuilt cbrt function
Auxiliary Space: O(1)

Using Prime Factors :

Find all the Prime Factors of the given number N using the approach in this article.

  1. Store the frequency of all the prime factors obtained above in a Hash Map.
  2. Traverse the Hash Map and if the frequency of every prime factors is not a multiple of 3, then the given number N is not a perfect cube.

Below is the implementation of the above approach:  


Output
True

Using Binary Search:

The idea is to use the Binary Search technique where we will be searching in a range of [1, N] iteratively by dividing the range halve every time until we get the resultant output.

  1. Initialize two variables, left and right, to 0 and the given number, respectively.
  2. While left is less than or equal to right follow the below steps.
  3. Compute the mid point and cube value.
  4. If cube is equal to N then return N, else change the left and right pointers accordingly.
  5. If no perfect cube is found in the above steps, return false, indicating that the given number is not a perfect cube.

Output
Yes

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

Using Mathematical Fact:

Every perfect cube can n (say n = x^3) can be written as sum of x consecutive odd numbers.

1^3 = 1 = 1
2^3 = 8 = 3 + 5
3^3 = 27 = 7 + 9 + 11
4^3 = 64 = 13 + 15 + 17 + 19
5^3 = 125 = 21 + 23 + 25 + 27 + 29
6^3 = 216 = 31 + 33 + 35 + 37 + 39 + 41

If you take a closer look, you can notice that we consider different set of consecutive odd numbers for every cube.


Output
1 8 27 64 

How does this work?

Let n be perfect square and its square root be x.

We can write x^3 = n

We can also say the following
(x^2) * x = n^3
(x^2 + x^2 + x^2 .......... x-times) = n^3

If x is an odd number:
Examples:
x = 3, we can write the above as ((x^2 - 1) + x^2 + (x^2 + 1)) = n [Sum of 3 Consecutive Odd numbers]
For example say x = 5, we can write it as ((x^2 - 2) + (x^2 - 1) + x^2 + (x^2 + 1) + (x^2 + 2)) = n [Sum of 5 Consecutive Odd Numbers]
In General, ((x^2 - i + 1) + (x^2 - i + 2) ........ + x^2 + .......... (x^2 + i - 1) + (x^2 + i - 2)) where i is floor(x/2)

If x is an even number:
Examples:
x = 2, we can write the above as ((x^2 - 1) + (x^2 + 1)) = n [Sum of 2 Consecutive Odd numbers]
For example say x = 4, we can write it as ((x^2 - 2) + (x^2 - 1) + (x^2 + 1) + (x^2 + 2)) = n [Sum of 4 Consecutive Odd Numbers]
In General, ((x^2 - i) + (x^2 - i + 1) ........ (x^2 + i ) + (x^2 + i)) where i is x/2


Time Complexity : If we take a closer look, we mainly generate odd numbers 1 to a number. The number of times the innermost statement is executed is equal to the number of odd numbers generated. For example of n = 125, we generate all numbers from 1 to 29. If n = x^3, then the maximum odd numbers generated is close to x^2 + x (as explained above). We can upper bound the total iterations by O(Log n). We have explained how is the time complexity of checking if a number is perfect square is O(Log n) using the mathematical approach in the last approach discussed here.

Comment
Article Tags:
Article Tags: