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

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

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
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +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.

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