''' Purpose: continue introduction to functions Name: Email id: Who checked your code: Whose code did you check: ''' def is_factor( x, y ) : ''' Parameters x and y are integers. The function returns whether y is a factor of x. ''' # compute remainder of x divided by y rem = x % y # check it out if ( rem == 0 ) : # check whether remainder is 0 result = True # if it is, return value is True else: result = False # if it is not, return value is False # return determination return result def are_relative_primes( x, y ) : ''' Parameters x and y are integers. The function returns whether x and y are relatively prime; i.e., whether y is not a factor of x and vice-versa. ''' # We can use is_factor() to determine whether y is a factor of x and vise versa! is_x_a_factor_of_y = is_factor( y , x ) # tells us if x is a factor of y is_y_a_factor_of_x = is_factor( x , y ) # tells us if y is a factor of x if ( is_x_a_factor_of_y == True ) : # if x is a factor of y then they are not relatively prime! result = False # Since they're not relatively prime, we would have the result be false elif ( is_y_a_factor_of_x == True ) : # if y is a factor of x then they are not relatively prime result = False # Since they're not relatively prime, we would have the result be false else: # This is the final condition where x and y are relatively prime given # that x is not a factor of y and y is not a factor of x # this means that is_x_a_factor_of_x is False and is_y_a_factor_of_x is False (satisfying the problem conditions!) result = True # result is true since they are relatively prime return result # return the final result after testing the conditions in the if, elif, and else statements def is_prime( x ) : ''' Parameter x is am integer. The function returns whether x is prime; i.e., its only factors are 1 and itself. ''' # We need to check out all other possible factors such as 2,3,4 all the way to n-1 # This is why we would want to use a range! # We're assuming the number is prime until we have a check to see if it's not result = True # We assumed the result is true ( x is prime ) for y in range( 2 , x ) : is_y_a_factor_of_x = is_factor( x , y ) # this checks if y is a factor of x if ( is_y_a_factor_of_x == True ): # if y is a factor of x then x has a factor other than 1 that is a factor of itself result = False # result is False given that x has another factor other than 1 break # Python keyword break gets you out of a loop! else: pass # We want to keep our existing result since we did not find another factor of x return result # return the final result whether x is prime or not if __name__ == "__main__": import primal a1, b1 = 6, 2 a2, b2 = 7, 3 a3, b3 = 4, 5 f1 = primal.is_factor( a1, b1 ) f2 = primal.is_factor( a2, b2 ) f3 = primal.is_factor( a3, b3 ) print( 'is_factor(', a1, ',' , b1, ') = ', f1 ) print( 'is_factor(', a2, ',' , b2, ') = ', f2 ) print( 'is_factor(', a3, ',' , b3, ') = ', f3 )