How to check if an integer is a power of 3?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Checking if an integer is a power of a specific base, such as 3, is a common problem in computer science. Given an integer, the objective is to determine whether it can be expressed as for some integer . This article will explore various methods to approach this problem, focusing on their technical aspects and providing illustrative examples.
Method 1: Iterative Division
A straightforward method to determine if a number is a power of 3 is through iterative division. The key idea is to continuously divide the number by 3 and check if the resultant quotient eventually becomes 1.
Algorithm:
- If `n` is less than 1, return `false`.
- While `n` is divisible by 3: • Divide `n` by 3.
- If the final `n` is 1, return `true`; otherwise, return `false`.
Example:
Let's check if 81 is a power of 3:
• Start with . • . • . • . • .
Since we arrived at 1, 81 is a power of 3.
Limitations:
• The method involves division, which can be computationally expensive if `n` is large. • It doesn't work for negative numbers.
Method 2: Using Logarithms
This technique leverages the properties of logarithms to quickly check if a number is a power of 3.
Algorithm:
- Calculate the logarithm base 3 of `n` using .
- Round this result and check if raising 3 to the rounded value equals `n`.
Example:
Check if 81 is a power of 3:
• Calculate . • Compute: and . • . • , so 81 is a power of 3.
Limitations:
• This method can suffer from precision issues due to floating-point arithmetic. • Not suitable for very large numbers due to logarithmic calculations.
Method 3: Precomputation
Another approach uses precomputation, especially useful when you need to check multiple numbers.
Algorithm:
- Precompute all powers of 3 up to a predefined limit, say where is less than the maximum integer value.
- Store these values in a set.
- To check if a number is a power of 3, simply look up the presence of the number in the set.
Example:
Suppose you want to check numbers up to .
• Precomputed set: {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683}. • For any number, a membership check is .
Limitations:
• Requires additional memory to store precomputed values. • Only works for numbers less than the largest precomputed power.
Summary Table
| Method | Advantages | Limitations |
| Iterative Division | Simplicity | Slow for large numbers Fails for negatives |
| Logarithms | Efficiency | Precision issues with floats Struggles with large numbers |
| Precomputation | Constant time checks | Memory overhead Limited by precomputation scope |
Conclusion
Different techniques for determining if an integer is a power of 3 cater to various use cases and constraints. The iterative method is easy to implement but might be slow for large numbers. The logarithmic method is faster but can be hamstrung by precision issues. Precomputation offers a quick lookup but at a cost of memory. Selecting the right approach depends on specific application needs, including performance and limitations imposed by the operating environment.
Related reading
- How to check if two permutations are symmetric?
- How to check if two words are anagrams
- How to check/find if an item is in a DEQUE
- How to checkout old git commit including all submodules recursively?
- How to check if line segment intersects a rectangle?
- How to compute locations of mesh points when resolution is increased?
- How to check if an object is a generator object in Python?
- How to check if an object is a list or tuple but not string?

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 courseTrack 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.