Count pairs in an array whose product is composite number | Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code
function isComposite ( N ) { var i ; for ( i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return true ; } } return false ; } function compositePair ( arr , N ) { var res = 0 ; var i , j ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { var prod = arr [ i ] * arr [ j ] ; if ( isComposite ( prod ) ) { res ++ ; } } } return res ; } var arr = [ 1 , 1 , 2 , 2 , 8 ] ; var N = arr . length ; document . write ( compositePair ( arr , N ) ) ;