Check Cube Number in Python

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.

Two programs

Cube-root estimate + verify for single input, and integer scan for range output.

Live preview

Try positive, negative, and zero values with integer-safe browser logic.

Key caution

Never trust floating estimate alone; always verify k * k * k == n.

Prerequisites

Basic Python functions, loops, integer multiplication, and optional use of round.

  • Define and call Python functions with def.
  • 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.

Examples: 8 = 23, 27 = 33, -8 = (-2)3, but 9 is not a cube.

8
27
9 not a cube

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.

n = 27

Cube root of 27 is 3, and 33 = 27.

Intuition

27
Check
3 * 3 * 3
28 Not a cube
Near
between and

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

Live preview

JavaScript safe integers. Uses integer scan on absolute value, then applies sign logic.

Try -8, 0, or 28.

Live result
Press “Check cube” to classify the number.

Algorithm

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

Root + verify route

Estimate k from cube root and rounding, then verify k * k * k == n.

Integer route

For x = |n|, increment k until k^3 >= x; cube iff equality holds.

📜 Pseudocode

Pseudocode (integer route)
function is_perfect_cube(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 from floating-point cube root, then verify exactly with integer multiplication.

python
def is_cube(number: int) -> bool:
    k = round(abs(number) ** (1 / 3))
    if number < 0:
        k = -k
    return k * k * k == number


input_number = 27
if is_cube(input_number):
    print(f"{input_number} is a cube number.")
else:
    print(f"{input_number} is not a cube number.")

Explanation

The rounded root is only a candidate. The final check k * k * k == number is what guarantees correctness.

k = round(abs(number) ** (1 / 3))

Estimate step. Gives the nearest integer cube-root magnitude before sign handling and exact verification.

2

Integer scan: cubes from 1 to 50

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

python
def is_cube_number(num: int) -> bool:
    k = 0
    while k * k * k < num:
        k += 1
    return k * k * k == num


print("Cube numbers in the range 1 to 50:")
for i in range(1, 51):
    if is_cube_number(i):
        print(i, end=" ")

Explanation

The loop finds the smallest k with k^3 >= num; equality means num is a perfect cube.

while k * k * k < num:

Stopping rule. Stops right when cube reaches or crosses the target.

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(abs(n)**(1/3)) 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

Try changing input_number in Example 1 or extending the range in Example 2.

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

Edge cases and pitfalls

Cube-root estimation without final integer check can misclassify values near boundaries.

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.

Performance

Linear scan cost

Integer scan is simple but grows with |n|^(1/3). Use binary search if needed.

⏱️ 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)

For scan-based methods, n denotes input magnitude and root growth is about |n|^(1/3).

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