LeetCode #326 Easy

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.

  • n can be zero or negative; no 3^x is either.
  • n is a 32-bit signed int, so only 20 values pass: 3^0 = 1 up to 3^19 = 1,162,261,467.
  • Follow-up: answer without loops or recursion.
Constraints
  • -2³¹ <= n <= 2³¹ - 1
mathrecursion
Open on LeetCode ↗
02

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.

03

Approach

1

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.

2

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.

3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def isPowerOfThree(self, n: int) -> bool:
▶3 if n <= 0:
▶4 return False
▶5 return 1162261467 % n == 0
n27273^191162261467333333333333333333327 is positive → test whether it divides 3^19
n27
n > 0yesguard passes
27 is positive, so it gets past the guard. The bottom rows show 3^19 = 1162261467 as what it really is: nineteen 3s multiplied together. It is the largest power of 3 an int can hold.
n273333^191162261467333333333333333333327 = 3 × 3 × 3
factors of 273 × 3 × 3
threes3
Split 27 into prime factors: 3 × 3 × 3. It divides 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.
n273333^1911622614673333333333333333333every 3 in 27 lines up with a 3 above
matched 3s3
unmatchednone
Each of the 3 threes cancels against one of the nineteen 3s. Nothing is left over on top, so 27 divides 3^19.
n273333^1911622614673333333333333333333remainder 0 → return true
1162261467 % 270
quotient430467213^16, the green 3s
resulttrue
The modulo is 0. The 16 green 3s left over are the quotient, 3^16 = 43046721. So 27 = 3^3 and the answer is true.
05

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.

▶1class Solution:
▶2 def isPowerOfThree(self, n: int) -> bool:
▶3 if n <= 0:
▶4 return False
▶5 while n % 3 == 0:
▶6 n //= 3
▶7 return n == 1
06

Common pitfalls

Using logarithms

✗ Wrong
x = math.log(n) / math.log(3)
return n > 0 and x == int(x)
✓ Right
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

✗ Wrong
return 1162261467 % n == 0
✓ Right
if n <= 0:
    return False
return 1162261467 % n == 0

n = 0 crashes with a division by zero, and n = -3 gives remainder 0, so it would return true.

07

Edge cases

n = 1

1 = 3⁰, and 1 divides every number, so the modulo gives 0 and the answer is true with no special case.

08

Complexity

Time
O(1)
Space
O(1)
One comparison and one modulo. The division loop below takes up to 19 steps, O(log₃ n).
09

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.