Can the parameter value be expressed as the cube of an integer?
Perfect cubes are integers which are the result of some integer raised to the $3^\text{rd}$ power. For example, $1 = 1^3$, $8 = 2^3$, $27 = 3^3$, $64 = 4^3$, etc. Thus, $1, 8, 27, 64, \ldots$ are perfect cubes.
Your task is to complete the implementation of a method that takes a long
input parameter, and returns the boolean
value true
if the value of the parameter is a perfect cube, and false
otherwise.
In the edu.cnm.deepdive.Cube
class, the isPerfectCube
method is declared with the following signature, return type, and modifiers:
public static boolean isPerfectCube(long input)
The implementation must not change the modifiers, return type, method name, parameter types/number/order, or possible exceptions shown above. For more method declaration details, see the Javadoc documentation.
If input
is a perfect cube, isPerfectCube(input)
must return true
; if input
is not a perfect cube, it must return false
.
Remember that a negative number has a negative cube root. Thus, input
may be negative.
You may find it useful to create one or more additional static
methods as “helpers”; the problem can be solved without doing so, however.
Do not hesitate to declare any constants (static final
fields) that you feel might simplify or clarify your code.
The method to be completed includes a TODO
comment to that effect.
Your implementation will be verified using the following inputs and expected outputs.
input |
Expected return value of Cube.isPerfectCube(input) |
---|---|
0 |
true |
-1 |
true |
4_096 |
true |
549_755_813_888L |
true |
-1_152_924_803_144_876_033L |
true |
9_223_358_842_721_533_951L |
true |
4 |
false |
65_536 |
false |
549_755_813_889L |
false |
-1_152_924_803_144_876_032L |
false |
9_223_358_842_721_533_952L |
false |