Only if latest compiler were present..


  • -1
    A

    Proof of concept :

    #include <iostream>
    
    constexpr unsigned int pow3to(unsigned int n)
    {
    	if(n == 0) return 1;
    	if(n == 1) return 3;
    	unsigned int i = 3;
    	while(n > 1) {
    		i *= 3;
    		--n;
    	}
    	return i;
    }
    
    
    bool IsPowerOf3(unsigned int n)
    {
    	if(n % 3 != 0) return false;
    	switch(n) {
    		case pow3to(1):
    		case pow3to(2):
    		case pow3to(3):
    		case pow3to(4):
            //
            // Go till reasonable power and maybe fallback to them??
            //
    		case pow3to(15): return true; break;
    		default: return false;
    	}
    	return false;
    }
    
    int main()
    {
    	std::cout << "Enter input:";
    	unsigned int inp = 4782969;
    	std::cin >> inp;
    	std::cout << IsPowerOf3(inp) << std::endl;
    	return 0;
    }

  • 0
    A

    Lol downvote? Tough crowd huh..


Log in to reply
 

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