## Algorithm

Problem Name: 326. Power of Three

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 == 3x`.

Example 1:

```Input: n = 27
Output: true
Explanation: 27 = 33
```

Example 2:

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

Example 3:

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

Constraints:

• `-231 <= n <= 231 - 1`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
bool isPowerOfThree(int n) {
#if 0
if (n == 1) return true;
if (n == 0 || n % 3) return false;
return isPowerOfThree(n / 3);
#else
return (n > 0 && (1162261467 % n) == 0);
#endif
}
``````
Copy The Code &

Input

cmd
n = 27

Output

cmd
true

### #2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public boolean isPowerOfThree(int n) {
int left = 0;
int right = n / 3;
while (left  < = right) {
int mid = (left + right) / 2;
double powValue = Math.pow(3, mid);
if (powValue == n) {
return true;
}
if (powValue > n) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return false;
}
}
``````
Copy The Code &

Input

cmd
n = 27

Output

cmd
true

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const isPowerOfThree = function(n) {
const maxInt = Math.pow(3,30)
if(n < 0> {
return false
}
return maxInt % n === 0
}
``````
Copy The Code &

Input

cmd
n = 0

Output

cmd
false

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def isPowerOfThree(self, n: int) -> bool:
i = 1
while i < n:
i *=3
return i == n
``````
Copy The Code &

Input

cmd
n = 0

Output

cmd
false

### #5 Code Example with C# Programming

```Code - C# Programming```

``````
namespace LeetCode
{
public class _0326_PowerOfThree
{
public bool IsPowerOfThree(int n)
{
return n > 0 && 1162261467 % n == 0;
}
}
}
``````
Copy The Code &

Input

cmd
n = -1

Output

cmd
false