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?
# | User | Rating |
---|---|---|
1 | ecnerwala | 3648 |
2 | Benq | 3580 |
3 | orzdevinwang | 3570 |
4 | cnnfls_csy | 3569 |
5 | Geothermal | 3568 |
6 | tourist | 3565 |
7 | maroonrk | 3530 |
8 | Radewoosh | 3520 |
9 | Um_nik | 3481 |
10 | jiangly | 3467 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | adamant | 164 |
2 | awoo | 164 |
4 | TheScrasse | 160 |
5 | nor | 159 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 150 |
7 | SecondThread | 150 |
9 | orz | 145 |
9 | pajenegod | 145 |
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?
Name |
---|
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.
Thank you.
I know a way how to check if a number is an integer or not.
if there is another way, please suggest me.
b divides a if ( (a/b)*b == a)
Its more efficient. Thanks for helping.
Thank you.
But I can't understand the condition for avoiding overflow. Can you please explain why this condition is needed? If I comment out this line, what error will occur.
n = 2*1e18 and b = 1e9
Thanks.
Hello
Check this problem : http://codeforces.com/contest/614/problem/A ,and you will see why this condition is important, and how can overflow occur.
Thanks