C Program to check whether a number is a Perfect Cube or not
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
// C program for the above approach #include <math.h> #include <stdio.h> // Function to check if a number is // a perfect Cube void perfectCube( int N) { for ( int i = 1; i < N; i++) { // If cube of i is equals to N // then print Yes and return if (i * i * i == N) { printf ( "Yes" ); return ; } } // No number was found whose cube // is equal to N printf ( "No" ); return ; } // Driver Code int main() { // Given Number int N = 216; // Function Call perfectCube(N); return 0; } |
Yes
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
// C program for the above approach #include <math.h> #include <stdio.h> // Function to check if a number is // perfect cube using inbuilt function void perfectCube( int N) { int cube_root; cube_root = ( int )round( pow (N, 1.0 / 3.0)); // If cube of cube_root is equals // to N, then print Yes else No if (cube_root * cube_root * cube_root == N) { printf ( "Yes" ); return ; } else { printf ( "No" ); return ; } } // Driver Code int main() { // Given number N int N = 216; // Function Call 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
// C program for the above approach #include <math.h> #include <stdio.h> // Function to check if a number is // a perfect Cube using binary search void perfectCube( int N) { int start = 1, end = N; while (start <= end) { // Calculating mid int mid = (start + end) / 2; // If N is a perfect cube if (mid * mid * mid == N) { printf ( "Yes" ); return ; } // If mid^3 is smaller than N, // then move closer to cube // root of N if (mid * mid * mid < N) { start = mid + 1; } // If mid^3 is greater than N else end = mid - 1; } printf ( "No" ); return ; } // Driver Code int main() { // Given Number N int N = 216; // Function Call perfectCube(N); return 0; } |
Yes
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.
Please Login to comment...