Sunday, 17 July 2016

Power of 3

/*  Power of 3 */
/*
    Given an integer, check if it is a power of 4
    Approach : Check all power till 20, limit of INT_MAX (2^32)
*/

 #include<iostream>  
 #include<cmath>  
 using namespace std;  
 bool isPowerOfThree(int n) {  
   if(n < 0)  
     return false;  
   for(int i = 0; i <= 20; i++){  
     int out = pow(3, i);  
     if(out == n)  
       return true;  
   }  
   return false;  
 }  
 int main(){  
   int n = 19;  
   cout << isPowerOfThree(n);  
   return 0;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive