Check if all the pairs of an array are coprime with each other | JavaScript implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ; Driver Program
function gcd ( a , b ) { if ( ! b ) { return a ; } return gcd ( b , a % b ) ; } function allCoprime ( A , n ) { var all_coprime = true ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( gcd ( A [ i ] , A [ j ] ) != 1 ) { all_coprime = false ; break ; } } } return all_coprime ; } var A = [ 3 , 5 , 11 , 7 , 19 ] ; var arr_size = A . length ; if ( allCoprime ( A , arr_size ) ) { console . log ( " " ) ; } else { console . log ( " " ) ; }