A cube number (perfect cube) is an integer n that equals k3 for some integer k. Both 0 and negative cubes count.
Remember
n is a cube number ⇔ n = k × k × k for some integer k
Examples:
0 = 0³
1 = 1³
8 = 2³
27 = 3³
−8 = (−2)³
10 → not a cube
k
k³
Cube?
0
0
Yes
2
8
Yes
3
27
Yes
—
10
No
First non-negative cubes
List
0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000
Approach
How to Check a Cube Number in PHP
Four clear steps — then pick an integer scan or a cube-root check.
Work with |n| if you want a nonnegative search (re-apply the sign for negatives).
Find an integer k whose cube might equal the value.
Verify with exact integer multiplication: k * k * k == n.
Never trust a floating-point cube root alone — always verify.
Method
Idea
Time
Best for
Integer scan
Increase k while k³ <= |n|
O(∛|n|)
Interviews, no floating point
pow + verify
Round pow(|n|, 1/3), then check k³ == |n|
O(1)
Quick answers, typical int sizes
Pseudocode
Pseudocode
function IsCube(n):
target = abs(n)
k = 0
while k * k * k < target:
k = k + 1
if k * k * k != target:
return false
// For negatives, k itself can be treated as signed later
return true
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Definition
n = k³ for some integer k
Always cubes
0, 1, −1
Verify
Use integer k * k * k == n
Avoid
Trusting pow(..., 1/3) without a re-check
Overflow tip
Keep multiplication within PHP integer range
Classic check
27 → yes (3³); 10 → no
Try it
Live Preview
Enter any integer to see whether it is a perfect cube and which k produces it.
Whole numbers (capped at ±999999999). Tap a chip or type a value.
Result
n = 27
k = 3
Check: 3 × 3 × 3 = 27
Verdict: CUBE NUMBER
Trace
Worked Walkthrough — n = 27
Increase k until its cube reaches or passes 27, then compare exactly.
Calculation
k = 0 → 0³ = 0 < 27
k = 1 → 1³ = 1 < 27
k = 2 → 2³ = 8 < 27
k = 3 → 3³ = 27 = 27 ✓
Result: 27 is a cube number (k = 3)
Code
PHP Programs
Start with a pure integer scan. Then use pow with an integer verification for a short production-style check.
Example 1 — Integer Scan
Walk k upward until k³ meets or passes |n|, then compare.
php
<?php
function isCube(int $n): bool
{
$target = abs($n);
$k = 0;
while ($k * $k * $k < $target) {
$k++;
}
return $k * $k * $k === $target;
}
$number = 27;
if (isCube($number)) {
echo "$number is a cube number.\n";
} else {
echo "$number is not a cube number.\n";
}
Output
27 is a cube number.
How It Works
Store |n| so the scan works the same for negatives.
Start $k at 0 and increase while $k³ < $target.
When the loop stops, either $k³ == $target (cube) or $k³ > $target (not a cube).
Negatives work because cubing preserves sign — matching on absolute value is enough.
For 27, the loop lands on $k = 3 and confirms 27.
Example 2 — pow + Verify
Estimate the cube root, round to the nearest integer, then re-check with exact multiplication.