![]() |
VOOZH | about |
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
Table of Content
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:
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
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:
Yes
Time Complexity: O(log N) because using inbuilt cbrt function
Auxiliary Space: O(1)
Find all the Prime Factors of the given number N using the approach in this article.
Below is the implementation of the above approach:
True
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.
Yes
Time Complexity : O(log N)
Auxiliary Space : O(1)
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.
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.