Check Cube Number in PHP

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

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

27
28Not a cube

Takeaway: cubes quickly spread out: 1, 8, 27, 64, ...

Live preview

Live result
Press "Check cube" to classify the number.

Algorithm

Goal: return true iff n = k3 for some integer k.

📜 Pseudocode

Pseudocode (integer route)
function isPerfectCube(n):
    x = abs(n)
    k = 0
    while k * k * k < x:
        k = k + 1
    return k * k * k == x
1

Cube-root estimate + integer verification

Fast for one value. Estimate an integer root, then verify exactly with integer multiplication.

php
<?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.";
?>
2

Integer scan: cubes from 1 to 50

Pure integer logic, no floating point. Same output pattern as the C reference page.

php
<?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

An integer n is a perfect cube if n = k^3 for some integer k. Examples: 0 = 0^3, 1 = 1^3, 8 = 2^3, 27 = 3^3, -8 = (-2)^3.
Floating-point values can be close to an integer but not exact. The safe approach is: estimate k, then verify k*k*k == n in integer arithmetic.
Yes for very large values. For typical interview-sized integers, round(cubeRoot(n)) plus final integer verification is usually fine.
Yes. Use an integer loop (or binary search) to find k such that k^3 reaches |n|, then compare.
Yes. 0 = 0^3, so it is a perfect cube.
It runs in O(|n|^(1/3)) iterations because k grows until k^3 is at least |n|.

🔄 Input / output examples

nPerfect cube?k
27Yes3
-8Yes-2
28No
0Yes0

Edge cases and pitfalls

Sign

Negative cubes

Negative perfect cubes exist (-8, -27). Make sure your method preserves sign correctly.

Zero

n = 0

Always a cube because 0 = 0^3.

Float

Precision limits

For very large magnitudes, floating-point root may be slightly off; verify with exact integer cube comparison.

⏱️ Time and space complexity

MethodTimeExtra space
Root estimate + verifyO(1) styleO(1)
Linear integer scanO(|n|^(1/3))O(1)
Binary search on kO(log |n|)O(1)

Summary

  • Definition: n is a perfect cube if n = k^3 for some integer k.
  • Methods: root estimate + integer verify, or pure integer search.
  • Watch-outs: negative values, zero handling, and floating-point precision.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful