Check Power of 3 in C#
What you’ll learn
- What a power of 3 means:
n = 3^kfor a whole numberk. - How to test by repeatedly dividing by
3until you reach1or find a remainder. - How to check
27and list all powers of 3 from1to20.
What is a power of 3?
A positive integer is a power of 3 when it equals 3 multiplied by itself k times. Examples: 1, 3, 9, 27, 81. Values like 2, 6, 10, 12 are not powers of 3.
Factor view
27 = 3 × 3 × 3 = 3³.
Divide loop
27 → 9 → 3 → 1 — every step divides cleanly by 3.
Range 1–20
Outputs 1 3 9 — only three fit in that range.
Prerequisites
Modulo %, integer division, and simple loops.
- You know that
n % 3 == 0meansnis divisible by 3. - You can read a
whileloop and aforloop in C#.
Understanding the concept
A power of 3 contains only the prime factor 3. Peel off one factor at a time: if 27 divides by 3 to give 9, then 3, then 1, it is 3³. If you ever get a remainder, stop — the number is not a power of 3.
Intuition examples
27 = 3³ (power of 3). 12 = 3 × 4 has a factor of 4, so dividing by 3 once leaves 4, which is not divisible by 3. 1 = 3&sup0; counts as a power of 3.
Live preview
Enter an integer and watch each divide-by-3 step until the verdict.
Algorithm
Require positive n
If n <= 0, return false.
Divide by 3 while possible
While n > 1, if n % 3 != 0 return false; otherwise set n = n / 3.
Return result
If the loop finishes with n == 1, the original value was a power of 3.
📜 Pseudocode
function isPowerOfThree(n):
if n <= 0:
return false
while n > 1:
if n % 3 != 0:
return false
n = n / 3
return true Check one number
Reference program: tests whether 27 is a power of 3 by peeling off factors of 3.
using System;
class Program
{
static bool IsPowerOfThree(int n)
{
if (n <= 0)
return false;
while (n > 1)
{
if (n % 3 != 0)
return false;
n /= 3;
}
return true;
}
static void Main()
{
int number = 27;
if (IsPowerOfThree(number))
Console.WriteLine($"{number} is a power of 3.");
else
Console.WriteLine($"{number} is not a power of 3.");
}
} Powers of 3 from 1 to 20
Reuses IsPowerOfThree inside a range loop — matches the reference range output.
using System;
class Program
{
static bool IsPowerOfThree(int n)
{
if (n <= 0)
return false;
while (n > 1)
{
if (n % 3 != 0)
return false;
n /= 3;
}
return true;
}
static void Main()
{
Console.WriteLine("Power of 3 in the range 1 to 20:");
for (int i = 1; i <= 20; i++)
{
if (IsPowerOfThree(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization and alternatives
Divide loop (preferred for learning). Clear, correct, and runs in O(log n) time.
32-bit shortcut. The largest power of 3 in a signed 32-bit int is 3^19 = 1162261467. For positive n, 1162261467 % n == 0 also identifies powers of 3 — handy in interviews after you understand the loop.
Logarithm check. Test whether Math.Log(n, 3) is (nearly) an integer — watch out for floating-point rounding on large values.
❓ FAQ
🔄 Input / output examples
Input n | Divide steps | Result |
|---|---|---|
27 | 27 → 9 → 3 → 1 | Power of 3 |
12 | 12 → 4 (stops: remainder) | Not a power of 3 |
1 | Already 1 | Power of 3 |
0 | Rejected by guard | Not a power of 3 |
Edge cases and pitfalls
n = 1
1 = 3^0 — the loop never runs and returns true.
n = 0
Return false immediately; zero is not a power of 3.
Not powers of three
Return false for n <= 0.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| Divide-by-3 loop | O(log n) | O(1) |
Modulo with 1162261467 | O(1) | O(1) |
Range 1..R | O(R log R) | O(1) |
Summary
- Powers of three are
3^k— only the factor 3 appears. - Divide by 3 while possible; any remainder means
false. - From
1to20, only1, 3, 9qualify.
Unlike powers of 2, powers of 3 have no simple one-bit binary pattern. The classic interview-friendly approach is to keep dividing by 3 until you reach 1 or hit a remainder.
8 people found this page helpful
