Definition
3^k
Positive integers of form 3 to a power.
Powers of three are numbers like 1, 3, 9, 27, 81… — each is 3^k for some whole k >= 0. The clearest check is: keep dividing by 3 while exact, and see if you finish at 1. This tutorial covers that loop, a live step tracer, worked Python examples, edge cases, and complexity.
3^k
Positive integers of form 3 to a power.
While exact
n //= 3 while n % 3 == 0.
Success
Only pure powers reduce to 1.
Float risk
Integer loop is safer than log.
Try 27 / 10
See each division step.
Same idea
Divisor 3 instead of 2.
A power of three is a positive integer of the form 3^k. So 27 passes (3^3) while 6 fails — it divides by 3 once and stops at 2.
Unlike power of 2, there is no popular bitwise shortcut. Interviews usually want the integer divide-by-3 loop (and a clear rejection of n <= 0).
It reinforces exact integer reasoning and pairs naturally with the power-of-2 cousin interviewers often ask next.
1, 3, 9, 27, 81…
Exact // 3 until stuck.
Zero and negatives fail.
Skip float log checks.
In short: while divisible by 3, divide; succeed only if you reach 1.
Given an integer n, decide whether it is a power of three (3^k for some k >= 0).
# 27 -> 27/3=9, 9/3=3, 3/3=1 -> yes
# 9 -> 9/3=3, 3/3=1 -> yes
# 6 -> 6/3=2, stop at 2 -> no
# 1 -> already 1 -> yes (3^0) | Item | Type | Description |
|---|---|---|
n | int | Value to test (must be > 0 for yes). |
| Return | bool | True when n is 3^k. |
| Key loop | modulo / floor | while n % 3 == 0: n //= 3 |
function is_power_of_three(n):
if n <= 0:
return false
while n mod 3 == 0:
n = n / 3
return n == 1 | Method | Idea | Notes |
|---|---|---|
| Divide by 3 | Peel factors until stuck; check == 1 | Interview default — exact |
| Generate 3^k | Multiply up and compare | Great for listing powers |
| log base 3 | Check if log is whole | Float rounding risk — avoid |
| Goal | Pattern |
|---|---|
| Reject non-positive | if n <= 0: return False |
| Divisible by 3? | n % 3 == 0 |
| Peel a factor | n //= 3 |
| Success | return n == 1 |
| Generate next | p *= 3 |
| vs power of 2 | Same loop; divisor 2 instead of 3 |
Same question — different reliability.
n //= 3Clearest exact check
p *= 3Best for listing powers
avoid floatsRounding can lie
no & trickUsually loop-only here
Reach for a power-of-three check whenever you need an exact 3^k test.
Often asked right after power of 2.
Practice exact integer division loops.
List 3^k values inside a band.
Contrast logs with integer loops.
One % 3 check is a different problem.
Key benefit: one short loop that proves exactness without floating-point surprises.
Shows each division step until the value is no longer divisible by 3.
Three complete Python programs — check 27, list powers from 1 to 20, and generate powers by multiplying. Click View Output to reveal sample console results.
The classic divide-until-one helper.
Checks whether a number is exactly 3^k.
def is_power_of_3(n: int) -> bool:
if n <= 0:
return False
while n % 3 == 0:
n //= 3
return n == 1
number = 27
if is_power_of_3(number):
print(f"{number} is a power of 3.")
else:
print(f"{number} is not a power of 3.") 27 becomes 9, then 3, then 1. Because the final value is 1, the helper returns True.
Reuse the helper to list nearby powers of three.
Reuses helper function for range listing.
def is_power_of_3(n: int) -> bool:
if n <= 0:
return False
while n % 3 == 0:
n //= 3
return n == 1
start, end = 1, 20
print(f"Power of 3 in the range {start} to {end}:")
for i in range(start, end + 1):
if is_power_of_3(i):
print(i, end=" ") Within 1..20 the powers are 1, 3, and 9. 27 is the next power and sits just outside this band.
Build 3^k directly instead of filtering every integer.
print("Powers 3^k for k = 0 to 5:")
power = 1
for k in range(0, 6):
print(f"3^{k} = {power}")
power *= 3 When you only need the sequence, multiplying by 3 is cheaper than testing every n in a large range.
Zero and negatives are not powers of three here.
While n % 3 == 0, set n = n // 3.
n == 1 means only factors of 3 were present.
True for 3^k, else False.
Compare the divide loop on a yes case and a no case.
| Start | Steps | Stops at | Verdict |
|---|---|---|---|
27 | 27 → 9 → 3 → 1 | 1 | Yes |
9 | 9 → 3 → 1 | 1 | Yes |
6 | 6 → 2 | 2 | No |
1 | (no division) | 1 | Yes (3^0) |
Ending at 1 means every factor peeled was a 3.
Where power-of-three checks show up beyond the interview prompt.
Exact integer factor peeling.
Example: is_pow3(27).
List 3^k inside a band.
Example: 1, 3, 9 in 1..20.
Build powers with *= 3.
Example: Example 3.
Same loop idea, different divisor.
Example: related topic.
Teach float-rounding traps.
Example: prefer // loop.
Continue the interview chain.
Example: related CTA.
Pro Tip: say “I’ll peel factors of 3 until I can’t, then check for 1” before coding.
Why the divide-by-3 loop works well for beginners and interviews.
Dry-run 27 on paper in a few steps.
No float rounding surprises from logs.
Same pattern with a different divisor.
*= 3 builds the sequence without scanning.
Pro Tip: mention that power-of-2 has a bitwise shortcut, but power-of-3 usually stays with the loop.
Small habits that keep power-of-three solutions interview-ready.
Reject zero and negatives first.
Prefer n //= 3 over float division.
One yes and one no seals understanding.
Use *= 3 if you need the sequence itself.
Mention the trap, then use integers.
Pro Tip: sanity-check 1, 27, 6, and 0 — if those four behave, your logic is solid.
Mistakes that commonly break power-of-three programs.
0 is divisible by 3 forever in careless loops.
→ Require n > 0 before dividing.
Floating roots can round incorrectly.
→ Prefer the integer divide loop.
Checking only n % 3 == 0.
→ Keep dividing until you cannot.
1 = 3^0 is a power of three.
→ Include 1 in yes cases.
n /= 3 can leave floats.
→ Use n //= 3.
Handle these before claiming the check is complete.
Already at 1; no divisions needed.
n <= 0 fails immediately.
Stops at 2, not 1.
Return false here.
27 → 9 → 3 → 1.
Not divisible by 3 at all.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
n % 3 == 0, do n //= 3; then n == 1.Quick Takeaway: peel factors of 3 with integer division; succeed only if you finish at 1.
| Approach | Time | Extra space |
|---|---|---|
| Divide-by-3 loop | O(log n) | O(1) |
| Generate up to bound | O(log bound) | O(1) or list |
| Range 1..U scan | O(U log U) worst | O(1) |
Each successful division shrinks n by a factor of 3, so the loop is logarithmic in n.
A power of three reduces to 1 by exact repeated division by 3. Guard n > 0, loop with n //= 3 while divisible, and prefer integers over logarithms.
Practice the three examples above, then continue to composite numbers.
Divide by 3 while exact; n == 1 means power of three.
Decide 3^k the interview-friendly way.
end at 1
Definitionn //= 3
Methodn > 0
Edgesp *= 3
ListingO(log n)
AnalysisPowers of three grow as 1, 3, 9, 27, 81... Repeated division by 3 is the clearest exact integer check for this pattern.
Learn how to check whether a number is composite in Python.
8 people found this page helpful