> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Power of Three Python Solution with Tests

> Tested Python solution for LeetCode 326 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 326, [Easy](/catalog/easy). Topics: [Math](/catalog/topics/math), [Recursion](/catalog/topics/recursion). [View on LeetCode](https://leetcode.com/problems/power-of-three/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 326   # by problem number
lcpy gen -s power_of_three   # by problem name
```

## 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

```
Input: n = 27
Output: true
Explanation: 27 = 3^3
```

```
Input: n = 0
Output: false
Explanation: There is no x where 3^x = 0.
```

```
Input: n = -1
Output: false
Explanation: There is no x where 3^x = (-1).
```

### Constraints

* -2^31 \<= n \<= 2^31 - 1

**Follow up:** Could you solve it without loops/recursion?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/power_of_three/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/power_of_three/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1)
    # Space: O(1)
    def is_power_of_three(self, n: int) -> bool:
        # 3^19 = 1162261467 is the largest power of three fitting in a signed
        # 32-bit int; it is divisible by every smaller power of three and by
        # no other positive integer in range. Constant time, no loops.
        return n > 0 and 1162261467 % n == 0
```

## Complexity

| Time | Space |
| - | - |
| O(1) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.