My simple problem is to create a function that determines if a number N can be written as a^n
for some given n, i.e. I need to check if N^(1/n)
is a whole number. Somehow this function yields wrong results:
def is_power(N, n):
r = float(N) ** ( 1. / float(n) )
return r.is_integer()
For n=2
it works.
For n=3
and N=1,8,27 the function yields True, which is correct. But from then on False, e.g. for 4*4*4=64
or 5*5*5=125
. How can I create a working function that finds numbers that are squares/cubes/etc.?