mathematics
integer powers
algorithms
number theory
computational methods

Check if one integer is an integer power of another

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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:

python
1def is_power(x: int, base: int) -> bool:
2    if base in (0, 1, -1):
3        if base == 0:
4            return x == 0
5        if base == 1:
6            return x == 1
7        return x in (1, -1)
8
9    if x == 1:
10        return True
11
12    value = x
13    while value % base == 0:
14        value //= base
15
16    return value == 1
17
18
19print(is_power(81, 3))
20print(is_power(72, 3))
21print(is_power(16, 2))

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:

python
1import math
2
3
4def is_power_with_logs(x: int, base: int) -> bool:
5    if x <= 0 or base <= 1:
6        return False
7    exponent = math.log(x, base)
8    return math.isclose(exponent, round(exponent))

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:

python
1def is_power_of_two(x: int) -> bool:
2    return x > 0 and (x & (x - 1)) == 0
3
4
5print(is_power_of_two(16))
6print(is_power_of_two(18))

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.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms