less than 1 minute read

<-E 326> Power of Three

// Method 1
class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n==0)
            return false;
        while (n % 3 == 0) {
            n = n / 3;
        }
        return n == 1;
    }
};

// Method 2
class Solution {
public:
    bool isPowerOfThree(int n) {
        if(n == 0) return false;
        
        double x = log10(n) / log10(3.0);
        
        return ceil(x) == floor(x);
    }
};