Number of co | JavaScript program for the above approach ; Function to check whether given integers are co - prime or not ; Utility function to count number of co - prime pairs ; Traverse the array ; If co - prime ; Increment count ; Return count ; Function to count number of co - prime pairs ; Stores digits in string form ; Sort the list ; Keep two copies of list l ; Generate 2 digit numbers using d1 and d2 ; If current number does not exceed N ; Stores length of list ; Stores number of co - prime pairs ; Print number of co - prime pairs ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs
function GCD ( a , b ) { return b == 0 ? a : GCD ( b , a % b ) ; } function coprime ( a , b ) { if ( GCD ( a , b ) == 1 ) return true ; return false ; } function numOfPairs ( arr , N ) { let count = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( coprime ( parseInt ( arr [ i ] ) , parseInt ( arr [ j ] ) ) ) { count = count + 1 ; } } } return count ; } function noOfCoPrimePairs ( N , d1 , d2 ) { let l = [ ] ; l . push ( d1 . toString ( ) ) ; l . push ( d2 . toString ( ) ) ; l . sort ( ) ; if ( N < parseInt ( l [ 1 ] ) ) return ; let total = [ ... l ] ; let temp2 = [ ... l ] ; let flag = 0 ; let temp3 = [ ] ; while ( l [ 0 ] . length < 10 ) { for ( let i = 0 ; i < l . length ; i ++ ) { for ( let j = 0 ; j < 2 ; j ++ ) { if ( parseInt ( l [ i ] + temp2 [ j ] ) > N ) { flag = 1 ; break ; } total . push ( l [ i ] + temp2 [ j ] ) ; temp3 . push ( l [ i ] + temp2 [ j ] ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = [ ... temp3 ] ; temp3 = [ ] ; } let lenOfTotal = total . length ; let ans = numOfPairs ( total , lenOfTotal ) ; document . write ( ans ) ; } let N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ;