Answer to Question #165756 in Python for Paul Carpenter

Question #165756

Do Exercise 6.4 from your textbook using recursion and the is_divisible function from Section 6.4. Your program may assume that both arguments to is_power are positive integers. Note that every positive integer that has an exponent of 0 is a power of "1". This includes "0" and "1", itself.


After writing your is_power function, include the following test cases in your script to exercise the function and print the results:


print("is_power(10, 2) returns: ", is_power(10, 2))

print("is_power(27, 3) returns: ", is_power(27, 3))

print("is_power(1, 1) returns: ", is_power(1, 1))

print("is_power(10, 1) returns: ", is_power(10, 1))

print("is_power(3, 3) returns: ", is_power(3, 3))


1
Expert's answer
2021-02-23T04:03:31-0500
def is_divisible(x,y):
    if(x%y==0):
        return True;
    else:
        return False;
def is_power(x,y):
    if(x==1):
        return True;
    if(y==1):
        return False;
    if(is_divisible(x,y)==False):
        return False;
    return is_power(x//y,y);
print("is_power(10, 2) returns: ", is_power(10, 2))

print("is_power(27, 3) returns: ", is_power(27, 3))

print("is_power(1, 1) returns: ", is_power(1, 1))

print("is_power(10, 1) returns: ", is_power(10, 1))

print("is_power(3, 3) returns: ", is_power(3, 3))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS