- Check
3·3·3
Check Cube Number in C
What you’ll learn
- What it means for an integer to be a perfect cube (
n = k3). - A compact check using
cbrtandround(with a final integer cube test), plus an all-integer loop like the range sample. - Sign, zero, overflow, and floating-point caveats, plus a browser live preview.
Overview
Given an integer n, decide whether n = k3 for some integer k. The reference idea uses the real cube root from math.h; we round it correctly for negatives, then verify with integer cubing.
Two programs
cbrt + round for a single value, then a linear k scan for the 1–50 listing.
Live preview
Try integers in the safe JS range; uses integer cube detection (no math.h in the browser).
Fixes vs naive round
Avoid (int)(cbrt(n)+0.5) on negative n; widen cubes when needed.
Prerequisites
#include <stdio.h>, integer multiplication, and (for Example 1) math.h (cbrt, round).
int main(void)andprintf.- Optional:
long longwhen cubing large candidates.
What is a perfect cube?
An integer n is a perfect cube (cube number) if n = k3 for some integer k. That includes 0 = 03 and negative examples such as -8 = (-2)3.
Do not confuse “cube number” with “multiple of 3” or “square”; the operation is cubing, not squaring.
Cube root characterization
For real x, the equation k3 = x has a unique real cube root k. For integer n, n is a perfect cube iff that real root is an integer—equivalently some rounded float estimate k satisfies k3 = n once checked in exact integer arithmetic.
n = 27Real cube root of 27 is 3, and 33 = 27.
Intuition
- Near
- between
33and43
Takeaway: perfect cubes grow quickly in gaps: 1, 8, 27, 64, …
Live preview
JavaScript safe integers. Integer cube test: adjust sign for nonzero n, grow k until k3 ≥ |n|, then compare.
Algorithm
Goal: return true iff n = k3 for some integer k.
Floating route
Compute k = round(cbrt(n)) as a signed integer, then verify k*k*k == n using a wide enough type.
Integer route
For nonnegative n, increment k from 0 until k3 ≥ n; answer yes iff equality.
📜 Pseudocode
function isPerfectCubeNonneg(n): // n ≥ 0
k ← 0
while k * k * k < n:
k ← k + 1
return k * k * k = n cbrt + round + integer check
Uses math.h. round fixes the reference’s (int)(cbrt(n)+0.5) bug on negative cubes such as -27. Cubes are evaluated in long long to reduce overflow risk for borderline int values.
#include <stdio.h>
#include <math.h>
int isCube(int number) {
long long k = (long long)llround(cbrt((double)number));
long long c = k * k * k;
return c == (long long)number;
}
int main(void) {
int inputNumber = 27;
if (isCube(inputNumber)) {
printf("%d is a cube number.\n", inputNumber);
} else {
printf("%d is not a cube number.\n", inputNumber);
}
return 0;
} Explanation
llround returns long long; casting double may lose precision only for integers too large to represent exactly as double—outside typical int interview scope.
long long c = k * k * k;Verify in integers. Confirms the rounded root truly cubes back to number.
Integer scan: cubes from 1 to 50
Same output as the reference: 1 8 27. Uses nonnegative k only; for larger num switch the cube to long long.
#include <stdio.h>
int isCubeNumber(int num) {
int k = 0;
while ((long long)k * k * k < (long long)num) {
k++;
}
return (long long)k * k * k == (long long)num;
}
int main(void) {
printf("Cube numbers in the range 1 to 50:\n");
for (int i = 1; i <= 50; i++) {
if (isCubeNumber(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} Explanation
The loop finds the smallest k with k3 ≥ num. If num is a perfect cube, equality holds; otherwise k3 overshoots.
(long long)k * k * kWiden before multiply so intermediate products stay defined for larger inputs.
Optimization
Binary search on k. For huge nonnegative n, binary-search k in [0, n] (or a tighter upper bound) instead of stepping by one.
No libm. Newton’s method on f(k)=k3-n can approximate k, but you still need an exact integer final check.
Interview: mention sign, 0, overflow, and why you verify k3 in integers after any float step.
❓ FAQ
🔄 Input / output examples
Swap inputNumber in Example 1 or extend the loop in Example 2.
| n | Perfect cube? | k |
|---|---|---|
27 | Yes | 3 |
-8 | Yes | -2 |
28 | No | — |
0 | Yes | 0 |
Edge cases and pitfalls
Rounding cube roots in floating point without a final integer check is fragile; naive +0.5 truncation is wrong for several negative values.
cbrt on negatives
cbrt is defined for negative reals. Pair it with round/llround, not biased-half truncation via + 0.5 and (int).
n = 0
Perfect cube: 03. The integer loop must start at k = 0 (Example 2 already does when reused for nonnegative num).
k * k * k in int
For large k, the product can overflow before comparison. Cast or store in long long.
Huge integers
IEEE double cannot represent all integers past 253 exactly; stick to integer methods for big integers.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
cbrt + verify | O(1) typical library cost | O(1) |
Linear k scan (nonnegative) | O(n1/3) iterations | O(1) |
Binary search on k | O(log n) iterations | O(1) |
Here n denotes the magnitude of the input for the scan bound k ≈ n1/3.
Summary
- Definition:
n = k3for some integerk(includes0and negatives). - Code:
llround(cbrt(n))then cube-check, or pure integer increment / binary search. - Watch-outs: negative rounding, overflow in
k3,doubleprecision limits.
A nonzero integer n is a perfect cube iff in its prime factorization every exponent is a multiple of three. The cube roots of 0 and 1 are 0 and 1 themselves.
8 people found this page helpful
