@SAMM: Something like this might work: bool PowerOfThree(unsigned int n) { int powsof2[5] = {16,8,4,2,1}; int powsof3[32] = {1,3,0,9,27,0,81,243,0,729,0,2187,6561,0,19683,59049,0,177147,0,531441,1594323,0, 4782969,14348907,0,43046721,129140163,0,387420489,0,1162261467,3486784401}; int i,j=0; unsigned int m = n; for( i = 0 ; i < 5 ; ++i ) { if( m >> powsof2[i] ) { j += powsof2[i]; m >>= powsof2[i]; } } return powsof3[j] == n; }
The for loop determines the bit number of the highest-order bit set. The table of powers of three is indexed by the bit number of the highest bit set. Dave On Nov 28, 2:47 am, SAMMM <somnath.nit...@gmail.com> wrote: > Given a number u have to check whether the number(N) can be expressed > in the form 3^n = N i:e It can be expressed in terms of power of 3. > > It can be done taking log on both but I am looking an alternate > approach (bit manipulation) . -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.