The reason your source code doesn't work is this: you just check (c%b) == 0) aka (a/b) is divisible by b , which is much weaker than part of the definition of a/b is a power of b .
If you want to solve such a problem, you should always start with trivial cases. In this case, there are two such cases: is_power(x,x) and is_power(1,x) - in the answer True , because x**1==x and x**0==1 .
Once you cover these cases, you just need to write down the rest of the definition. Write the code for (a is divisible by b) and (a/b is a power of b) and put it all together.
The last function will look like this:
def is_power(a,b): if <trivial case 1> or <trivial case 2>: return True
It remains only to answer the question <a/b is a power of b> . The easiest way to do this is to use the is_power function is_power - this is called recursion.
Jochen ritzel
source share