[LeetCode] Problem 326 - Power of Three

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

Example

No.1

Input: 27

Output: true

No.2

Input: 0

Output: false

No.3

Input: 9

Output: true

No.4

Input: 45

Output: false

Follow up

Could you do it without using any loop / recursion?

Code

1
2
3
4
5
6
7
public boolean isPowerOfThree(int n) {
if (n <= 0)
return false;

// 3^19
return 1162261467 % n == 0;
}