C Program to check whether a number is a Perfect Cube or not - GeeksforGeeks

19 Aug, 2020
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 = 216Output: YesExplanation:As 216 = 6*6*6.Therefore the cube root of 216 is 6.
Input: N = 100Output: 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 Cubevoid 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 Codeint 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 functionvoid 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 Codeint main(){    // Given number N    int N = 216;      // Function Call    perfectCube(N);    return 0;}
Yes

Complexity Analysis:
Time Complexity: O(N), since the pow() function works in O(N), so the time complexity is O(N).
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”.
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.
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 searchvoid 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 Codeint 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.
Binary Search
Maths
maths-cube
maths-perfect-cube
maths-power
C Programs
Mathematical
School Programming
Mathematical
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Producer Consumer Problem in C
C program to find the length of a string
Exit codes in C/C++ with Examples
Regular expressions in C
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7