326 - Power of Three
Written on January 8, 2016
Tweet
Given an integer, write a function to determine if it is a power of three.
public class Solution {
public boolean isPowerOfThree(int n) {
if (n <= 0) return false;
while (n % 3 == 0) {
n /= 3;
}
return n == 1;
}
public boolean isPowerOfThree(int n) {
return (Math.log10(n) / Math.log10(3)) % 1 == 0;
}
}
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
#3^20 is bigger than int
return n > 0 and 3 ** 19 % n == 0