This is identical to the question found on Check if one integer is an integer power of another, but I am wondering about the complexity of a method that I came up with to solve this problem.
Given an integer n
and another integer m
, is n = m^p
for some integer p
. Note that ^
here is exponentiation and not xor
.
There is a simple O(log_m n) solution based on dividing n
repeatedly by m
until it's 1 or until there's a non-zero remainder.
I'm thinking of a method inspired by binary search, and it's not clear to me how complexity should be calculated in this case.
Essentially you start with m
, then you go to m^2
, then m^4
, m^8
, m^16
, .....
When you find that m^{2^k} > n
, you check the range bounded by m^{2^{k-1}}
and m^{2^k}
. Is this solution O(log_2 (log_m(n)))
?
Somewhat related to this, if I do something like
m^2 * m^2
vs.
m * m * m * m
Do these 2 have the same complexity? If they do, then I think the algorithm I came up with is still O(log_m (n))