Power of Three
Power of Three is LeetCode 326 (Easy). Given an integer n, return true if n = 3^x for some integer x ≥ 0, and false otherwise.
ncan be zero or negative; no3^xis either.nis a 32-bit signed int, so only 20 values pass:3^0 = 1up to3^19 = 1,162,261,467.- Follow-up: answer without loops or recursion.
- -2³¹ <= n <= 2³¹ - 1
Intuition
Bit tricks do not help here, because powers of 3 have no special shape in binary. Use prime factors instead.
Because 3 is prime, the only numbers that divide 3^19 are 3^0, 3^1, …, 3^19. Any other positive number has a prime factor other than 3 (45 = 3 × 3 × 5), and 3^19 has no such factor to cancel it.
So take the largest power of 3 that fits in an int, 3^19 = 1,162,261,467, and check whether n divides it. One modulo answers the question, with no loop.
Approach
Two ways to solve it
Reject n ≤ 0, then check 1162261467 % n == 0.
- Speed: one modulo for any
n. - Follow-up: no loop and no recursion.
- Idea: 3 is prime, so
3^19has only powers of 3 as divisors.
Reject n ≤ 0, divide by 3 while the remainder is 0, and check that 1 is left.
- Simple: needs no number theory.
- Speed: at most 19 divisions for an int.
- Works for any base, prime or not.
The divisor test answers in one step and meets the no-loop follow-up. The division loop is the natural first idea and is shown further down.
Reject n ≤ 0
Return false for zero and negative numbers. Zero would make the modulo divide by zero, and a negative power like -27 divides 3^19 evenly, so the next check would wrongly accept it.
Fix the largest power of 3 in range
3^19 = 1,162,261,467 fits in a signed 32-bit int, while 3^20 = 3,486,784,401 does not. Every valid n is one of its 20 divisors, so it is the one number to test against.
Test divisibility
Return 1162261467 % n == 0. A power of 3 divides it exactly. Any other positive n brings a prime factor that is not 3, so a remainder is left over.
Code in Python, C++ and Java
3^19 = 1162261467 as what it really is: nineteen 3s multiplied together. It is the largest power of 3 an int can hold.3^19 exactly when every one of these factors can be cancelled against a 3 below it. The code never does this split; it is the reason one modulo is enough.3^19.3^16 = 43046721. So 27 = 3^3 and the answer is true.3^19 = 1162261467 as what it really is: nineteen 3s multiplied together. It is the largest power of 3 an int can hold.3^19 exactly when every one of these factors can be cancelled against a 3 below it. The code never does this split; it is the reason one modulo is enough.3^19 contains no factor 5 anywhere. A number with any factor other than 3 can never divide it.3^19 = 1162261467 as what it really is: nineteen 3s multiplied together. It is the largest power of 3 an int can hold.3^0, the empty product: it has no prime factors at all, so nothing can fail to match. And 1 divides every whole number.x = 0.Divide by 3 in a loop
3^x is 3 multiplied by itself x times, so stripping every factor of 3 must leave exactly 1. Any other leftover, like the 5 in 45, means the answer is false.
Common pitfalls
Using logarithms
x = math.log(n) / math.log(3) return n > 0 and x == int(x)
return n > 0 and 1162261467 % n == 0
Floating point rounds. For n = 243 = 3^5, log(243) / log(3) comes out as 4.999999999999999, so a real power of 3 is rejected. Integer division has no rounding to go wrong.
Leaving out the n ≤ 0 check
return 1162261467 % n == 0
if n <= 0:
return False
return 1162261467 % n == 0n = 0 crashes with a division by zero, and n = -3 gives remainder 0, so it would return true.
Edge cases
1 = 3⁰, and 1 divides every number, so the modulo gives 0 and the answer is true with no special case.
Complexity
Power of Three FAQ
Why doesn't the same trick work for powers of 4 or 6?
It needs the base to be prime. 2^30 % 8 == 0, so testing against the largest power of 4 would accept 8, which is not a power of 4. For a power-of-4 check, see the even-bit mask in Power of Four, and for powers of 2 the n & (n - 1) test in Power of Two.
What are the powers of 3?
1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, … Each is three times the one before. In a 32-bit signed int the list stops at 3¹⁹ = 1,162,261,467.