For integers x, y, and n, (only x and y are given) test if xn = y? If x = 8, y = 512, then n = 3 so that is true. But if x = 8 and y = 500, n would have to be around 2.98 (not an integer) so the statement evaluates as false. Is using a logarithm the best method to do this test?
Check if one integer is an integer power of another offers some solutions:
int n = y; while(n < x) n *= y; return n == x
,
while (x%y == 0) x = x / y
return x == 1
and the logarithm method (this is my version):
return ((log(y, x) % 1) == 0) // log(expression, base)
log(y, x) = log x y
Which method evaluates faster, especially for big numbers?