- Peel 3s
- 27 → 9 → 3 → 1
Check Power of 3 in Java
What you’ll learn
- Which numbers are powers of three:
1, 3, 9, 27, 81, … - The divide-by-3 loop: peel factors of 3 until you stop; you should land on
1only for pure powers. - Two Java programs: one number (sample 27) and a 1–20 list (1 3 9).
- A live preview that shows each division step, plus usual edge cases.
Overview
Picture multiplying by 3 again and again: 1 → 3 → 9 → 27 → 81 → … Any value on that train is a power of 3. To test a number n, run backward: while n is divisible by 3, replace n with n / 3. If you end exactly at 1, it is a power of 3.
Integer-safe logic
Only integer math with % and /, so no floating-point precision issues.
Live preview
Try 27 and watch 27 → 9 → 3 → 1 step-by-step.
Range practice
From 1 to 20, valid values are 1, 3, 9.
Prerequisites
Integer division and the remainder operator % in Java.
whileloops andifconditions.- Knowing
27 % 3 == 0means 27 divides evenly by 3.
What is a power of 3?
A power of three is any whole number 3k where k ≥ 0, such as 1, 3, 9, 27, 81, …
Every factor must be 3 only. That is why dividing by 3 until you cannot continue reveals whether the number is a pure power of 3.
Why repeated division works
If n = 3k, one division by 3 gives 3k-1. Keep doing this and powers of three end at 1. If you get stuck above 1 with n % 3 != 0, then n is not a pure power of 3.
Stopping when n % 3 != 0 leaves a core value. The core is exactly 1 for valid powers of 3.
Quick examples
- Peel 3s
- 15 → 5 (stuck; 5 is not 1)
- Because
- 30 = 1; no division needed.
Live preview
Mirrors Java logic: reject non-positive values, then divide by 3 while remainder is 0. Uses safe integer checks.
Algorithm
Goal: return true iff positive n equals 3k for some integer k ≥ 0.
Reject n <= 0
Powers of three are positive integers in this problem.
Strip factors of 3
While n % 3 == 0, set n = n / 3.
Finish
If final n is 1, answer true; otherwise false.
📜 Pseudocode
function isPowerOfThree(n):
if n <= 0:
return false
while n mod 3 == 0:
n = n / 3
return n == 1Check one number
This checks whether 27 is a power of 3.
public class Main {
static boolean isPowerOfThree(int n) {
if (n <= 0) return false;
while (n % 3 == 0) {
n /= 3;
}
return n == 1;
}
public static void main(String[] args) {
int number = 27;
if (isPowerOfThree(number)) {
System.out.println(number + " is a power of 3.");
} else {
System.out.println(number + " is not a power of 3.");
}
}
}Power of 3 in range 1 to 20
This prints all powers of 3 from 1 to 20.
public class Main {
static boolean isPowerOfThree(int n) {
if (n <= 0) return false;
while (n % 3 == 0) {
n /= 3;
}
return n == 1;
}
public static void main(String[] args) {
System.out.println("Power of 3 in the range 1 to 20:");
for (int i = 1; i <= 20; i++) {
if (isPowerOfThree(i)) {
System.out.print(i + " ");
}
}
}
}Optimization tips
- Use integer operations only; avoid floating-point logarithms for exact checks.
- Short-circuit early for
n <= 0. - For repeated queries in a small bound, precompute known powers.
❓ FAQ
Input/output examples
| Input n | Output |
|---|---|
27 | Power of 3 |
45 | Not a power of 3 |
1 | Power of 3 |
Edge cases
Watch input validation and stop conditions.
n = 1No division required
1 = 30, so it is a valid power of 3.
Not every multiple qualifies
6 becomes 2 after one divide, so answer is false.
Reject quickly
For interview constraints, values <= 0 are not powers of 3.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| Repeated divide-by-3 loop | O(log n) | O(1) |
| Scan range 1..U | O(U log U) | O(1) |
Summary
- Idea: powers of 3 are
1, 3, 9, 27, …and contain only factor 3. - Code: reject
n <= 0, divide by 3 while possible, then testn == 1. - Complexity:
O(log n)time andO(1)extra space.
Powers of three are 1, 3, 9, 27, 81... and each term is 3 times the previous one.
8 people found this page helpful
