Four clear steps — then pick a divide-down loop or a multiply-up sequence.
Require n > 0.
While n is divisible by 3, set n = Math.floor(n / 3).
If the remaining value is 1, it was a power of three.
Or start at 1 and multiply by 3 until you match or pass n.
Method
Idea
Time
Best for
Divide by 3
Peel factors of 3 until leftover is not divisible by 3
O(log n)
Default interview answer
Multiply up
Build 1, 3, 9, … until ≥ n
O(log n)
Showing the sequence explicitly
Pseudocode
Pseudocode
function IsPowerOfThree(n):
if n <= 0: return false
while n % 3 == 0:
n = n / 3
return n == 1
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Test
Divide by 3 while possible; end at 1
1
3^0 — yes
0 / negatives
Not powers of three here
Avoid log
Floating point can misclassify
vs Power of 2
No simple n & (n-1) style trick
Classic checks
27 yes; 12 no; 9 yes
Try it
Live Preview
Enter an integer to see each divide-by-3 step and the final verdict.
Whole numbers only. Tap a chip or type a value.
Result
n = 27
Steps: 27 → 9 → 3 → 1
27 is a power of 3
Code
JavaScript Programs
Start with the divide loop — it is the clearest exact check. List matches in a range, then generate powers by multiplying. Each example has a Try it Yourself editor.
Example 1 — Divide-by-3 Loop
Keep dividing while divisible by 3; success means you finish at 1.
JavaScript
function isPowerOfThree(n) {
if (n <= 0) {
return false;
}
while (n % 3 === 0) {
n = Math.floor(n / 3);
}
return n === 1;
}
const number = 27;
if (isPowerOfThree(number)) {
console.log(number + " is a power of 3");
} else {
console.log(number + " is not a power of 3");
}
While n % 3 === 0, divide by 3 with Math.floor — peel off factors of three.
When the loop stops, n is not divisible by 3 anymore.
If that leftover is 1, every factor was a 3.
Any other leftover (2, 4, 5, …) means extra prime factors remain.
Example 2 — Powers of 3 from 1 to 20
Reuse the helper and print every match on one line.
JavaScript
function isPowerOfThree(n) {
if (n <= 0) {
return false;
}
while (n % 3 === 0) {
n = Math.floor(n / 3);
}
return n === 1;
}
console.log("Powers of 3 from 1 to 20:");
let line = "";
for (let i = 1; i <= 20; i++) {
if (isPowerOfThree(i)) {
line += i + " ";
}
}
console.log(line.trim());
Each value is tested with the same isPowerOfThree helper.
Matches are collected into one space-separated line.
Only 1, 3, 9 appear in this band (27 is past 20).
Keeping the helper pure makes range listing easy.
Example 3 — Generate Powers
Build 3k by multiplying a running power by 3.
JavaScript
const kMax = 5;
let power = 1;
console.log("Powers 3^k for k = 0 to " + kMax + ":");
for (let k = 0; k <= kMax; k++) {
console.log("3^" + k + " = " + power);
power *= 3;
}
Powers 3^k for k = 0 to 5:
3^0 = 1
3^1 = 3
3^2 = 9
3^3 = 27
3^4 = 81
3^5 = 243
How It Works
Start at power = 1 (that is 3^0).
Print the current power, then multiply by 3 for the next exponent.
No floating-point check is needed — you build exact powers.
Change kMax in the Try it Yourself editor to print a longer list.
The same multiply idea can also test a number: grow until you match or pass n.
Edge Cases & Pitfalls
Check these before calling the solution done.
1
30 counts
1 is a power of three. The divide loop never runs and returns true.
0
Not a power of 3
Guard with n > 0 before dividing.
Multiples
6, 12, 18…
Divisible by 3 once is not enough — leftover must be 1.
log
Prefer exact math
Avoid Math.log for the interview solution; use integer loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Divide-by-3 loop
O(log n)
O(1)
Range listing
O(R log R) for range size R
O(1)
Generate powers
O(kMax)
O(1)
Each divide or multiply step shrinks or grows by a factor of 3, so iterations follow the exponent.
Remember
Key Takeaways
Definition:n = 3k for some k ≥ 0.
Loop: divide by 3 while possible; success if you land on 1.
Alt: multiply 1 by 3 until you reach or pass n.
Test set: 1 → yes; 27 → yes; 12 → no; 0 → no.
One line: keep dividing by 3; if you finish at 1, n is a power of three.
Frequently Asked Questions
A positive integer of the form 3^k for some integer k ≥ 0. Examples: 1, 3, 9, 27, 81.
While n is divisible by 3, divide by 3 with Math.floor. If you end at 1, n is a power of three. Or multiply a running power by 3 until it reaches or passes n.
Yes. 1 = 3^0.
Logarithms use floating point and can misclassify near powers. Exact integer divide or multiply loops are safer for interviews.
Both the divide loop and the multiply-up approach are O(log n) and use O(1) extra space.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.