Check Power of 3 in Python

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • Which numbers are powers of three.
  • How repeated division by 3 proves the check.
  • How to test one value and print range results.

Overview

Powers of three are 1, 3, 9, 27, 81... To test n, divide by 3 repeatedly while divisible; end at 1 means true.

Integer-only check

No floating math needed.

Live preview

Shows each divide-by-3 step.

Range list

From 1 to 20: 1, 3, 9.

Prerequisites

Integer division and modulo operation in Python.

  • Use while loops and conditions.
  • Know n % 3 == 0 means divisible by 3.

What is a power of 3?

A power of 3 is any number equal to 3^k for k >= 0.

The sequence is 1, 3, 9, 27, 81...

Why repeated division works

Pure powers of three can keep dividing by 3 and eventually reach 1. Others stop at a non-1 value.

Loop ending rule

After division loop, n == 1 means power of three.

Quick examples

27Yes
Steps
27 -> 9 -> 3 -> 1
15No
Steps
15 -> 5 (stop)
1Yes
Reason
3^0 = 1

Live preview

Shows each division step until the value is no longer divisible by 3.

Try 9, 10, 1, and 0.

Live result
Press "Run check" to see steps.

Algorithm

Goal: return true only if n can be reduced to 1 by repeated exact division by 3.

Reject n <= 0

Only positive values are valid here.

Divide while divisible

While n % 3 == 0, set n = n // 3.

Check final value

n == 1 means power of 3.

📜 Pseudocode

Pseudocode
function is_power_of_three(n):
    if n <= 0:
        return false
    while n mod 3 == 0:
        n = n / 3
    return n == 1
1

Check a single number

Checks whether a number is exactly 3^k.

python
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.")
2

Powers of 3 from 1 to 20

Reuses helper function for range listing.

python
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=" ")

Notes

Integer-safe. Division loop avoids floating-point issues from logarithms.

Alternative. For bounded ranges, precompute powers of 3 and check membership.

Comparison. Power-of-2 has a bitwise shortcut; power-of-3 usually does not.

❓ FAQ

A number of the form 3^k for whole k >= 0. Examples: 1, 3, 9, 27.
Yes. 3^0 = 1.
Powers of 3 are made only of factor 3. If repeated division ends at 1, it is a pure power of 3.
This tutorial returns false for n <= 0.
Yes, same repeated-division idea; only divisor changes from 2 to 3.
Possible but floating rounding can fail for large values; integer division loop is safer.

🔄 Input / output examples

Test these values in example 1.

Input numberTypical line
2727 is a power of 3.
1010 is not a power of 3.
11 is a power of 3.
00 is not a power of 3.

Edge cases

n = 1

True

1 = 3^0.

n <= 0

False

Powers of 3 here are positive only.

Multiples of 3

Not always true

6 divides by 3 once to 2, so fails.

⏱️ Time and space complexity

ApproachTime (single n)Extra space
Divide-by-3 loopO(log_3 n)O(1)
Range scan [1, U]O(U log U)O(1)

Summary

  • Power of 3 means n = 3^k for k >= 0.
  • Use repeated exact division by 3 and finish at 1.
  • Reject n <= 0 in this tutorial.
Did you know?

Powers of three grow as 1, 3, 9, 27, 81... Repeated division by 3 is the clearest exact integer check for this pattern.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful