A clear explanation of the Power of Three problem using repeated division and integer arithmetic.
Problem Restatement
We are given an integer n.
Return true if n is a power of three. Otherwise, return false.
A number is a power of three when it can be written as:
n = 3^xwhere x is an integer.
Examples:
27 = 3^3
9 = 3^2
3 = 3^1
1 = 3^0So 27, 9, 3, and 1 are powers of three.
Numbers like 0, 2, 6, 12, and 45 are not powers of three.
Input and Output
| Item | Meaning |
|---|---|
| Input | An integer n |
| Output | true if n is a power of three, otherwise false |
| Important condition | n may be zero or negative |
Function shape:
def isPowerOfThree(n: int) -> bool:
...Examples
Example 1:
Input: n = 27
Output: trueExplanation:
27 = 3 * 3 * 3 = 3^3So 27 is a power of three.
Example 2:
Input: n = 0
Output: falseNo integer power of three gives 0.
Example 3:
Input: n = -1
Output: falsePowers of three are positive, so -1 cannot be a power of three.
First Thought: Brute Force
One direct way is to keep generating powers of three:
1, 3, 9, 27, 81, ...Stop when the generated value becomes greater than or equal to n.
If we reach n, return true.
Otherwise, return false.
class Solution:
def isPowerOfThree(self, n: int) -> bool:
if n <= 0:
return False
power = 1
while power < n:
power *= 3
return power == nThis works, but there is another equally simple way that follows the definition more directly.
Key Insight
If n is a power of three, then it can be divided by 3 repeatedly until it becomes 1.
For example:
27 -> 9 -> 3 -> 1Every step divides cleanly by 3.
But consider 45:
45 -> 15 -> 5Now 5 cannot be divided by 3 cleanly.
So 45 is not a power of three.
The key rule is:
A positive number is a power of three if repeated division by 3 eventually reaches 1 with no remainder.Algorithm
First handle non-positive numbers.
If n <= 0, return false.
Then repeatedly divide n by 3 while it is divisible by 3.
At the end, check whether the result is exactly 1.
Steps:
- If
n <= 0, returnfalse. - While
n % 3 == 0, dividenby3. - Return whether
n == 1.
Walkthrough
Take:
n = 27Since 27 is divisible by 3, divide:
27 / 3 = 9Since 9 is divisible by 3, divide:
9 / 3 = 3Since 3 is divisible by 3, divide:
3 / 3 = 1Now n is 1.
So the answer is true.
Now take:
n = 45Divide by 3:
45 / 3 = 15Divide by 3 again:
15 / 3 = 5Now 5 % 3 != 0.
We stop. The final value is 5, not 1.
So the answer is false.
Correctness
The algorithm only divides by 3 when the current value is exactly divisible by 3.
If the original number is 3^x, then each division removes one factor of 3:
3^x -> 3^(x - 1) -> 3^(x - 2) -> ... -> 3^0Since 3^0 = 1, every valid power of three eventually becomes 1.
For a number that has any prime factor other than 3, repeated division by 3 cannot remove that factor. The process will stop at a value greater than 1.
For zero or negative numbers, the answer is immediately false, since powers of three are positive.
Therefore, the algorithm returns true exactly when n is a power of three.
Complexity
| Metric | Value | Why |
|---|---|---|
| Time | O(log n) | Each loop divides n by 3 |
| Space | O(1) | Only a few variables are used |
More precisely, the number of loop iterations is proportional to log_3(n).
Implementation
class Solution:
def isPowerOfThree(self, n: int) -> bool:
if n <= 0:
return False
while n % 3 == 0:
n //= 3
return n == 1Code Explanation
We first reject zero and negative numbers:
if n <= 0:
return FalseThen we divide by 3 as long as division is exact:
while n % 3 == 0:
n //= 3For a true power of three, this loop eventually reaches 1.
Finally:
return n == 1This returns true for values like 1, 3, 9, and 27.
It returns false for values like 0, -1, 2, 6, 12, and 45.
Testing
def run_tests():
s = Solution()
assert s.isPowerOfThree(27) == True
assert s.isPowerOfThree(9) == True
assert s.isPowerOfThree(3) == True
assert s.isPowerOfThree(1) == True
assert s.isPowerOfThree(0) == False
assert s.isPowerOfThree(-1) == False
assert s.isPowerOfThree(2) == False
assert s.isPowerOfThree(6) == False
assert s.isPowerOfThree(12) == False
assert s.isPowerOfThree(45) == False
print("all tests passed")
run_tests()Test meaning:
| Test | Why |
|---|---|
27 | Standard positive power of three |
9 | Smaller positive power |
3 | Base itself |
1 | Handles 3^0 |
0 | Zero cannot be a power of three |
-1 | Negative numbers cannot be powers of three |
6 | Divisible by 3 once, but not a pure power |
45 | Divisible by 3 several times, then fails |