Answer to Question #336526 in Python for john

Question #336526

Exercise 6.4.

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. Note: you will have to think about the base case.


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
2022-05-02T13:36:37-0400
def is_power(a,b):
	power = b
	if a == b:
		return True
	if b == -1 or b == 1 or b == 0:
		return False
	while abs(a) > abs(power):
		power *= b
		if power == a:
			return True
	return False




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
New on Blog
APPROVED BY CLIENTS