''' Purpose: continue introduction to functions ''' def is_factor( x, y ) : ''' Parameters x and y are integers. The function returns whether y is a factor of x. ''' # print( 'x =', x ) # print( 'y =', y ) # 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 # when a function hits its FIRST return statement, the function automatically stops and ANY code after # will not be seen # print( 'tgif' ) # return 'tgif' 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. --> do we know how to do this already? ''' # boolean accumulation result = True # assume the number is prime at first --> default result to True for i in range( 2, x ): # checking each of the number between 2 and x-1 (b/c we know 1 and x are factors of x) # if any one of these numbers is a factor of x --> x is not prime # if ( x % i == 0 ): # if x divides i evenly then i is a factor of x --> we've already done this if ( is_factor( x, i ) == True ): # using another function we've defined, is_factor( x, y ) # since is_prime() and is_factor() are in the same module, we don't have to say primal.the_function() # this function tells us whether y is a factor of x and returns a boolean value # we can use is_factor() in another function like this one to shorten our code # and check whether i is a factor of x # if i is a factor of x, re-assign the result variable to False because x is no longer a prime number # set the result to False and the result will keep being False while we are accumulating result = False # way 2 # ONCE we found the FIRST thing that's a factor of x, x is no longer prime # we want to exit the function immediately! # we have the answer right now, so we don't want to keep going in the for loop # return here, function will end immediately # return result # else: if i is not a factor of x, do nothing # result will be False if at least one of the i number is a factor of x # result will REMAIN True (from our default) if the if statement was never True so we never set result to False return result # with boolean accumulation, we tend to assume True or False for our result --> start the accumulator with # True or False # then we tend to use the if/else statements in some capacity to check whether our assumption was True/False # return the result # result remains the default value if our assumption was right # result will change if the conditional statements were met and we changed our result inside those statements 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. ''' # test if is_factor(x, y) == True --> False # if y is a factor of x, then x and y are not relative prime # elif is_factor( y, x ) == True --> False # if x is a factor of y, then x and y are also not relative prime # else: --> True # includes that y is NOT a factor of x, and x is NOT a factor of y, # then x and y are relative prime of each other ...