Ishtiaq11's blog

By Ishtiaq11, history, 8 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

»
8 years ago, # |
  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.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thank you.

    I know a way how to check if a number is an integer or not.

        if int(res) == ceil(res):
            integer
        else:
            not integer
    

    if there is another way, please suggest me.

»
8 years ago, # |
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");