Given a number N, the task is to write C program to check if the given number is 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
Method 1: Naive Approach To find the cube root of the given number iterate over all the natural numbers from 1 till N and check if cube of any number in this range is equal to the given number N then print Yes else print No Below is the implementation of the above approach:
C
#include <math.h>
#include <stdio.h>
void perfectCube( int N)
{
for ( int i = 1; i < N; i++) {
if (i * i * i == N) {
printf ( "Yes" );
return ;
}
}
printf ( "No" );
return ;
}
int main()
{
int N = 216;
perfectCube(N);
return 0;
}
|
Complexity Analysis:
- Time Complexity: O(N), only one traversal of the solution is needed, so the time complexity is O(N).
- Auxiliary Space: O(1). Constant extra space is needed.
Method 2: Using inbuilt function The idea is to use the inbuilt function pow() 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:
C
#include <math.h>
#include <stdio.h>
void perfectCube( int N)
{
int cube_root;
cube_root = ( int )round( pow (N, 1.0 / 3.0));
if (cube_root * cube_root * cube_root == N) {
printf ( "Yes" );
return ;
}
else {
printf ( "No" );
return ;
}
}
int main()
{
int N = 216;
perfectCube(N);
return 0;
}
|
Output:
Yes
Complexity Analysis:
- Time Complexity: O(1), since here the pow() function takes constant time, since second argument to pow function is constant
- Auxiliary Space: O(1). Constant extra space is needed.
Method 3: Using Binary Search The idea is to use Binary Search to solve the problem. The values of i * i * i is monotonically increasing, so the problem can be solved using binary search.
Below are the steps:
- Initialise low and high as 0 and N respectively.
- Iterate until low ? high and do the following:
- Find the value of mid as = (low + high)/2.
- Check if mid*mid*mid is equals to N then print “Yes”.
- If the cube of mid is less than N then search for a larger value in the second half of search space by updating low to mid + 1.
- If the cube of mid is greater than N then search for a smaller value in the first half of search space by updating high to mid – 1.
- If cube of N is not obtained in the above step then print “No”.
Below is the implementation of the above approach:
C
#include <math.h>
#include <stdio.h>
void perfectCube( int N)
{
int start = 1, end = N;
while (start <= end) {
int mid = (start + end) / 2;
if (mid * mid * mid == N) {
printf ( "Yes" );
return ;
}
if (mid * mid * mid < N) {
start = mid + 1;
}
else
end = mid - 1;
}
printf ( "No" );
return ;
}
int main()
{
int N = 216;
perfectCube(N);
return 0;
}
|
Complexity Analysis:
- Time Complexity: O(log N). The time complexity of binary search is O(log N).
- Auxiliary Space: O(1). Constant extra space is needed.
Iterative Approach
- Initialize a variable “i” to 1.
- While “i” raised to the power of 3 is less than or equal to the given number N, increment “i”.
- If “i” raised to the power of 3 is equal to N, then the number is a perfect cube, else it is not.
C
#include <stdio.h>
int main() {
int N = 216;
int i = 1;
while (i*i*i <= N) {
if (i*i*i == N) {
printf ( "Yes\n" );
return 0;
}
i++;
}
printf ( "No\n" );
return 0;
}
|
Time Complexity: O(N^(1/3)).
Auxiliary Space: O(1).