Блог пользователя Ishtiaq11

Автор Ishtiaq11, история, 7 лет назад, По-английски

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?

  • Проголосовать: нравится
  • -7
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

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.

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +7 Проголосовать: не нравится
k=b
while(k<n){
  if((n/k)<b)break; // to avoid overflow
  k*=b
}
cout << ((k==n) ? "yes" : "no");