Skip to main content
LeetCode 326, Easy. Topics: Math, Recursion. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3^x.

Examples

Constraints

  • -2^31 <= n <= 2^31 - 1
Follow up: Could you solve it without loops/recursion?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026