Least common element in given two Arithmetic sequences | Function to find the smallest element common in both the subsequences ; If a is equal to c ; If a exceeds c ; Check for the satisfying equation ; Least value of possible_y satisfying the given equation will yield True in the below if and break the loop ; If the value of possible_y satisfying the given equation lies in range [ 0 , b ] ; If no value of possible_y satisfies the given equation ; Driver Code
def smallestCommon ( a , b , c , d ) : NEW_LINE INDENT if ( a == c ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT if ( a > c ) : NEW_LINE INDENT swap ( a , c ) ; NEW_LINE swap ( b , d ) ; NEW_LINE DEDENT first_term_diff = ( c - a ) ; NEW_LINE possible_y = 0 ; NEW_LINE for possible_y in range ( b ) : NEW_LINE INDENT if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( possible_y != b ) : NEW_LINE INDENT return c + possible_y * d ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT def swap ( x , y ) : NEW_LINE INDENT temp = x ; NEW_LINE x = y ; NEW_LINE y = temp ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 ; B = 20 ; C = 19 ; D = 9 ; NEW_LINE print ( smallestCommon ( A , B , C , D ) ) ; NEW_LINE DEDENT