Check if one integer is an integer power of another
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether one integer is an exact power of another comes up in algorithm design, number theory exercises, and input validation. The problem sounds simple, but edge cases such as zero, one, and negative values matter if you want a correct implementation.
The cleanest general solution is usually repeated division. It stays in integer arithmetic, avoids floating-point rounding problems, and is easy to reason about.
The Core Idea
To decide whether x is an integer power of base, you want to know whether some integer exponent n exists such that base raised to n equals x.
For positive values greater than one, repeated division works well. If x is truly a power of base, then dividing by base repeatedly should eventually reduce x to 1 with no remainder at any step.
Integer Division Algorithm
Here is a straightforward Python implementation:
For 81 and 3, the loop produces 27, then 9, then 3, then 1, so the answer is True. For 72 and 3, the sequence stops at 8, so the answer is False.
Why Floating-Point Logarithms Are Risky
A tempting shortcut is to compute a logarithm and check whether the result is an integer. That looks compact, but floating-point arithmetic can introduce rounding errors:
This can work for many cases, but it is less reliable for large integers. Integer arithmetic is usually the better choice when exactness matters.
Handling Edge Cases
Several values need explicit rules.
If base is 1, only 1 is a power of 1. If base is -1, the results alternate between 1 and -1. If base is 0, then only 0 raised to a positive power stays 0, but 0 raised to zero is a separate mathematical discussion that most programming tasks avoid.
Negative numbers also matter. For example, -8 is a power of -2, because (-2) cubed equals -8. A repeated-division solution can still support this as long as the division loop and base rules are defined carefully.
Bit Tricks For Special Cases
If the base is 2, there is a fast bitwise shortcut:
This works because powers of two have exactly one bit set in binary representation.
Common Pitfalls
The most common mistake is ignoring the special cases for 0, 1, and negative bases. A loop that works for 3 and 81 can still produce wrong answers for those boundary values.
Another pitfall is using floating-point logs and trusting exact integer comparison on the result. Large numbers can land very close to an integer exponent without being represented exactly.
A third issue is forgetting that 1 is a power of any nonzero integer when exponent zero is allowed. If your problem statement excludes exponent zero, document that rule explicitly because it changes the result for several inputs.
Summary
- Repeated integer division is the most reliable general method.
- Floating-point logarithms are concise but can introduce rounding errors.
- Special cases for
0,1, and negative bases must be handled explicitly. - Powers of two can be checked efficiently with a bitwise test.
- Define whether exponent zero is allowed before writing the final rule.

