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 the only positive integer that is a power of "1" is "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))

Respuesta :

Answer:

Here is the python method:

def is_power(n1, n2): # function that takes two positive integers n1 and n2 as arguments

   if(not n1>0 and not n2>0): #if n1 and n2 are not positive integers

       print("The number is not a positive integer so:") # print this message if n1 and n2 are negative

       return None # returns none when value of n1 and n2 is negative.

   elif n1 == n2: #first base case: if both the numbers are equal

       return True #returns True if n1=n2

   elif n2==1: #second base case: if the value of n2 is equal to 1

       return False #returns False if n2==1

   else: #recursive step

       return is_divisible(n1, n2) and is_power(n1/n2, n2) #call divisible method and is_power method recursively to determine if the number is the power of another

Explanation:

Here is the complete program.

def is_divisible(a, b):

   if a % b == 0:

       return True

   else:

       return False

def is_power(n1, n2):

   if(not n1>0 and not n2>0):

       print("The number is not a positive integer so:")  

       return None  

   elif n1 == n2:

       return True

   elif n2==1:

       return False

   else:

       return is_divisible(n1, n2) and is_power(n1/n2, n2)  

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))

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

The first method is is_divisible method that takes two numbers a and b as arguments. It checks whether a number a is completely divisible by number b. The % modulo operator is used to find the remainder of the division. If the remainder of the division is 0 it means that the number a is completely divisible by b otherwise it is not completely divisible. The method returns True if the result of a%b is 0 otherwise returns False.

The second method is is_power() that takes two numbers n1 and n2 as arguments. The if(not n1>0 and not n2>0) if statement checks if these numbers i.e. n1 and n2 are positive or not. If these numbers are not positive then the program prints the message: The number is not a positive integer so. After displaying this message the program returns None instead of True of False because of negative values of n1 and n2.

If the values of n1 and n2 are positive integers then the program checks its first base case: n1 == n2. Suppose the value of n1 = 1 and n2 =1 Then n1 is a  power of n2 if both of them are equal. So this returns True if both n1 and n2 are equal.

Now the program checks its second base case n2 == 1. Lets say n1 is 10 and n2 is 1 Then the function returns False because there is no positive integer that is the power of 1 except 1 itself.

Now the recursive case return is_divisible(n1, n2) and is_power(n1/n2, n2)  calls is_divisible() method and is_power method is called recursively in this statement. For example if n1 is 27 and n2 is 3 then this statement:

is_divisible(n1, n2) returns True because 27 is completely divisible by 3 i.e. 27 % 3 = 0

is_power(n1/n2,n2) is called. This method will be called recursively until the base condition is reached. You can see it has two arguments n1/n2 and n2. n1/n2 = 27/3 = 9 So this becomes is_power(9,3)

The base cases are checked. Now this else statement is again executed  return is_divisible(n1, n2) and is_power(n1/n2, n2) as none of the above base cases is evaluated to true. when is_divisible() returns True as 9 is completely divisible by 3 i.e. 9%3 =0 and is_power returns (9/3,3) which is (3,3). So this becomes is_power(3,3)

Now as value of n1 becomes 3 and value of n2 becomes 3. So the first base case elif n1 == n2: condition now evaluates to true as 3=3. So it returns True. Hence the result of this statement print("is_power(10, 2) returns: ", is_power(10, 2))  is:                                                                                                

is_power(27, 3) returns:  True

Ver imagen mahamnasir

Following are the program to the given question:

Program Explanation:

  • Defining a method "is_divisible" that takes two variable "a,b" inside the parameter.
  • Usinge the return keyword that modulas parameter value and checks its value equal to 0, and return its value.
  • In the next step, another method "is_power" is declared that takes two parameter "a,b".
  • Inside the method, a conditional statement is declared, in which three if block is used.
  • Inside the two if block it checks "a, b"  value that is "odd number" and return bool value that is "True, False".
  • In the last, if block is used checks "is_power" method value, and use multiple print method to call and prints its value.      

Program:

def is_divisible(a, b):#defining a method is_divisible that takes two parameters

   return a % b == 0#using return keyword that modulas parameter value and checks its value equal to 0

def is_power(a, b):#defining a method is_power that takes two parameters  

   if a == 1:#defining if block that checks a value equal to 1  or check odd number condition

       return True#return value True

   if b ==  1:#defining if block that checks b value equal to 1  or check odd number condition

       return False#return value False

   if not is_divisible(a, b):#defining if block that check method is_divisible value

       return False##return value False

   return is_power(a/b, b)#using return keyword calls and return is_power method                          

print("is_power(10, 2) returns: ", is_power(10, 2))#using print method that calls is_power which accepts two parameter

print("is_power(27, 3) returns: ", is_power(27, 3))#using print method that calls is_power which accepts two parameter

print("is_power(1, 1) returns: ", is_power(1, 1))#using print method that calls is_power which accepts two parameter

print("is_power(10, 1) returns: ", is_power(10, 1))#using print method that calls is_power which accepts two parameter

print("is_power(3, 3) returns: ", is_power(3, 3))#using print method that calls is_power which accepts two parameter

Output:

Please find the attached file.

Learn more:

brainly.com/question/24432065

Ver imagen codiepienagoya