Number of Co | Javascript program to count the pairs whose sum of digits is co - prime ; Function to find the elements after doing the sum of digits ; Traverse from a to b ; Find the sum of the digits of the elements in the given range one by one ; Function to count the co - prime pairs ; Function to make the pairs by doing the sum of digits ; Count pairs that are co - primes ; Driver code ; Function to count the pairs
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function makePairs ( pairs , a , b ) { for ( i = a ; i <= b ; i ++ ) { var sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k = parseInt ( k / 10 ) ; } if ( sumOfDigits <= 162 ) pairs . push ( sumOfDigits ) ; } } function countCoPrime ( a , b ) { var pairs = [ ] ; makePairs ( pairs , a , b ) ; var count = 0 ; for ( i = 0 ; i < pairs . length ; i ++ ) for ( j = i + 1 ; j < pairs . length ; j ++ ) if ( GCD ( pairs [ i ] , pairs [ j ] ) == 1 ) count ++ ; return count ; } var a = 12 , b = 15 ; document . write ( countCoPrime ( a , b ) ) ;