# Power of Three

<https://leetcode.com/problems/power-of-three/description/>

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

## Thoughts

如果是3的power, 那么能一直/3一直到 1且每次除后的数和3取余都为0.

## Code

```
class Solution {
    public boolean isPowerOfThree(int n) {
        while (n > 1) {
            if (n % 3 != 0) {
                return false;
            }
            n = n / 3;
        }

        return n == 1 ? true : false;
    }
}
```

## Analysis

时间复杂度是O(lgn).

## Ver.2

我们可以找到MaxPow(3)最大能取到的数, 给定的N如果是3的倍数, 那么MaxPow(3) % N == 0.

```
class Solution {
    public boolean isPowerOfThree(int n) {
        return n > 0 && 1162261467 % n == 0;
    }
}
```


---

# 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://hao-fu-1.gitbook.io/oj/math/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.
