Check Cube Number in PHP
What you’ll learn
- What it means for an integer to be a perfect cube (
n = k3). - A practical check using rounded cube root + integer verification, and a pure integer search method.
- Sign, zero, and precision caveats, with a browser live preview.
Overview
Given an integer n, decide whether n = k3 for some integer k. A common approach estimates k from cube root, then confirms in integer arithmetic.
Prerequisites
Basic PHP functions, loops, integer multiplication, and optional use of pow/round.
- Define and call PHP functions.
- Use loops and integer arithmetic for exact verification.
What is a perfect cube?
An integer n is a perfect cube if there exists an integer k such that n = k3. This includes positive, negative, and zero values.
Cube root characterization
An integer n is a perfect cube iff its real cube root is an integer. In code, estimate root first, then verify exactly by integer cubing.
Intuition
Takeaway: cubes quickly spread out: 1, 8, 27, 64, ...
Live preview
Algorithm
Goal: return true iff n = k3 for some integer k.
📜 Pseudocode
function isPerfectCube(n):
x = abs(n)
k = 0
while k * k * k < x:
k = k + 1
return k * k * k == xCube-root estimate + integer verification
Fast for one value. Estimate an integer root, then verify exactly with integer multiplication.
<?php
function isCube(int $number): bool
{
$k = (int) round(pow(abs($number), 1 / 3));
if ($number < 0) {
$k = -$k;
}
return $k * $k * $k === $number;
}
$inputNumber = 27;
echo isCube($inputNumber) ? "{$inputNumber} is a cube number." : "{$inputNumber} is not a cube number.";
?>Integer scan: cubes from 1 to 50
Pure integer logic, no floating point. Same output pattern as the C reference page.
<?php
function isCubeNumber(int $num): bool
{
$k = 0;
while ($k * $k * $k < $num) {
$k++;
}
return $k * $k * $k === $num;
}
echo "Cube numbers in the range 1 to 50:\n";
for ($i = 1; $i <= 50; $i++) {
if (isCubeNumber($i)) {
echo $i . " ";
}
}
?>Optimization
Binary search on root. For huge magnitudes, binary search k on [0, |n|] instead of linear increment.
Keep final check exact. Even if you estimate with floating point, always confirm using integer cube comparison.
Interview: mention sign handling, zero case, and why verify-after-rounding is necessary.
❓ FAQ
🔄 Input / output examples
| n | Perfect cube? | k |
|---|---|---|
27 | Yes | 3 |
-8 | Yes | -2 |
28 | No | — |
0 | Yes | 0 |
Edge cases and pitfalls
Negative cubes
Negative perfect cubes exist (-8, -27). Make sure your method preserves sign correctly.
n = 0
Always a cube because 0 = 0^3.
Precision limits
For very large magnitudes, floating-point root may be slightly off; verify with exact integer cube comparison.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| Root estimate + verify | O(1) style | O(1) |
| Linear integer scan | O(|n|^(1/3)) | O(1) |
| Binary search on k | O(log |n|) | O(1) |
Summary
- Definition:
nis a perfect cube ifn = k^3for some integerk. - Methods: root estimate + integer verify, or pure integer search.
- Watch-outs: negative values, zero handling, and floating-point precision.
A nonzero integer n is a perfect cube iff in its prime factorization every exponent is a multiple of three. Also, 0 and 1 are perfect cubes.
8 people found this page helpful
