''' 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 is_prime( x ) : ''' Parameter x is an integer. The function returns whether x is prime; i.e., its only factors are 1 and itself. ''' result = True # Accumulator is set to True since we have not found a factor yet for possible_factor in range( 2, x ): # possible_factor will assume values 2 to x - 1 # Determine if possible_factor is a factor of x using is_factor print( 'possible factor:', possible_factor ) if (is_factor( x, possible_factor ) == True ): # returns True if possible_factor is a factor of x result = False # We found another factor besides 1 and x so x is not prime! is_prime should return False break # This basically stops the loop (doesn't execute the next values of the loop variable) # ^ This line basically says "Hey! The minute we find one other factor, we set the return value to False" # If we go through the entire loop and find no additional factors (aka when result is never set to False), # then the number is prime since we had no other factors besides 1 and x itself (thus it is prime). 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. ''' # This function will make use of the function is_factor # if ( is_factor(x,y) == True ): # result = False # elif ( is_factor( y, x ) == True ): # result = False # else: # result = True if ( ( is_factor(x,y) == False ) and ( is_factor( y, x ) == False ) ): return True else: return False # check1 = is_factor( y, x ) # check2 = is_factor( x, y ) # if ( ( check1 == False ) and ( check2 == False ) ): # return True # else: # return False 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 )