Check if all the pairs of an array are coprime with each other | Function to store and check the factors ; Check if factors are equal ; Check if the factor is already present ; Insert the factor in set ; Check if the factor is already present ; Insert the factors in set ; Function to check if all the pairs of array elements are coprime with each other ; Check if factors of A [ i ] haven 't occurred previously ; Driver code
def findFactor ( value , factors ) : NEW_LINE INDENT factors . add ( value ) NEW_LINE i = 2 NEW_LINE while ( i * i <= value ) : NEW_LINE if value % i == 0 : NEW_LINE INDENT if value // i == i : NEW_LINE if i in factors : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT factors . add ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( i in factors ) or NEW_LINE ( ( value // i ) in factors ) : NEW_LINE return bool ( True ) NEW_LINE else : NEW_LINE factors . add ( i ) NEW_LINE factors . add ( value // i ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE return bool ( False ) NEW_LINE DEDENT def allCoprime ( A , n ) : NEW_LINE INDENT all_coprime = bool ( True ) NEW_LINE factors = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if A [ i ] == 1 : NEW_LINE INDENT continue NEW_LINE DEDENT if findFactor ( A [ i ] , factors ) : NEW_LINE all_coprime = bool ( False ) NEW_LINE break NEW_LINE DEDENT return bool ( all_coprime ) NEW_LINE DEDENT A = [ 3 , 5 , 11 , 7 , 19 ] NEW_LINE arr_size = len ( A ) NEW_LINE if ( allCoprime ( A , arr_size ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT