- Check
3 * 3 * 3
Check Cube Number in Python
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.
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 = 27Cube root of 27 is 3, and 33 = 27.
Intuition
- Near
- between
3³and4³
Takeaway: cubes quickly spread out: 1, 8, 27, 64, ...
Live preview
JavaScript safe integers. Uses integer scan on absolute value, then applies sign logic.
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
function is_perfect_cube(n):
x = abs(n)
k = 0
while k * k * k < x:
k = k + 1
return k * k * k == x Cube-root estimate + integer verification
Fast for one value. Estimate an integer root from floating-point cube root, then verify exactly with integer multiplication.
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.
Integer scan: cubes from 1 to 50
Pure integer logic, no floating point. Same output pattern as the C reference page.
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
🔄 Input / output examples
Try changing input_number in Example 1 or extending the range in Example 2.
| n | Perfect cube? | k |
|---|---|---|
27 | Yes | 3 |
-8 | Yes | -2 |
28 | No | — |
0 | Yes | 0 |
Edge cases and pitfalls
Cube-root estimation without final integer check can misclassify values near boundaries.
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.
Linear scan cost
Integer scan is simple but grows with |n|^(1/3). Use binary search if needed.
⏱️ 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) |
For scan-based methods, n denotes input magnitude and root growth is about |n|^(1/3).
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
