Code:
def is_divisible(x, y):
return x % y == 0
def is_power(x, y):
if x == 1:
return True
if y == 1:
return False
if is_divisible(x, y):
return is_power(x//y, y)
return False
Result:
$python is_power.py
is_power(10, 2) returns: False
is_power(27, 3) returns: True
is_power(1, 1) returns: True
is_power(10, 1) returns: False
is_power(3, 3) returns: True
Comments
Leave a comment