My simple Math solution


  • 2
    N

    // The idea is to find max integer number of power of 3.
    // If n is power of 3, then max_pow3_value is divided by n.

    bool isPowerOfThree(int n) {
        if (n <= 0) return 0;
        
        int max_pow3 = log10(INT_MAX)/log10(3); // simple math formula to compute log_3(N)
        int max_pow3_val = pow(3, max_pow3);
        
        return max_pow3_val % n == 0;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.