Count of non co | Recursive function to return gcd of two numbers ; Function to count the number of non co - prime pairs for each query ; Traverse the array arr [ ] ; Stores the count of non co - prime pairs ; Iterate over the range [ 1 , x ] ; Iterate over the range [ x , y ] ; If gcd of current pair is greater than 1 ; Given Input ; Function Call
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function countPairs ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) { var count = 0 ; for ( var x = 1 ; x <= arr [ i ] ; x ++ ) { for ( var y = x ; y <= arr [ i ] ; y ++ ) { if ( gcd ( x , y ) > 1 ) count ++ ; } } document . write ( count + " " ) ; } } var arr = [ 5 , 10 , 20 ] ; var N = 3 ; countPairs ( arr , N ) ;