Michelle Chan Michelle Chan - 3 months ago 25
Python Question

Python Recursion Exercise

I am doing exercise on Singpath and I am stuck at this question. This question is under recursion exercises but I have no idea what the question means.


A number,
a
, is a power of
b
if it is
divisible by
b
and
a/b
is a power of
b
.

Write a function called
is_power

that takes parameters
a
and
b
and
returns
True
if
a
is a power of
b
.


Update:

Just thought of the answer and I've posted it below.

Answer

Here is my answer...

def is_power(a,b):
    if(a%b != 0):
        return False
    elif(a/b == 1):
        return True
    else:
        return is_power(a/b,b)