Ishtiaq11's blog

By Ishtiaq11, history, 9 years ago, In English

I have two integer b,n. I need to check if the number n is integer power of number b. For example: 100 is an integer power of 10. As 10^2 = 100. But 110 is not an integer power of 10. How to determine this in coding?

  • Vote: I like it
  • -7
  • Vote: I do not like it

»
9 years ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

Try to do like this:

Find log2 of n. Then divide it by log2 of b.

If the result is integer, then n is power of b.

»
9 years ago, hide # |
Rev. 2  
Vote: I like it +7 Vote: I do not like it
k=b
while(k<n){
  if((n/k)<b)break; // to avoid overflow
  k*=b
}
cout << ((k==n) ? "yes" : "no");