# 326 Power of Three

Given an integer, write a function to determine if it is a power of three.

Follow up: Could you do it without using any loop / recursion?

**The Idea:** 3^i = x, solve for i, we get the algebraic expression log\_3(x) = i. So we're solving for the power i. Now if this power is an integer, then we know it is an integer power of 3, and we return true. One of the problems I found was that `log_3(243)` returns 4.99999999999, while the symbolic solution should be 5.0. To account for these errors we account for some small delta, and so the integer difference must be smaller than or equal to delta.

**Complexity:** O(1) time and space

```python
import math

class Solution:
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False
        sol = math.log(n, 3)
        delta = .0000000001
        return sol - int(sol + delta) <= delta
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/326-power-of-three.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
