Check if all array elements are pairwise co | Function to calculate GCD ; Function to calculate LCM ; Function to check if all elements in the array are pairwise coprime ; Initialize variables ; Iterate over the array ; Calculate product of array elements ; Calculate LCM of array elements ; If the product of array elements is equal to LCM of the array ; Driver Code ; Function call
function GCD ( a , b ) { if ( a == 0 ) return b ; return GCD ( b % a , a ) ; } function LCM ( a , b ) { return ( a * b ) / GCD ( a , b ) ; } function checkPairwiseCoPrime ( A , n ) { var prod = 1 ; var lcm = 1 ; for ( i = 0 ; i < n ; i ++ ) { prod *= A [ i ] ; lcm = LCM ( A [ i ] , lcm ) ; } if ( prod == lcm ) document . write ( " " ) ; else document . write ( " " ) ; } var A = [ 2 , 3 , 5 ] ; var n = A . length ; checkPairwiseCoPrime ( A , n ) ;