Number of Co | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int makePairs ( vector < int > & pairs , int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { int sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k /= 10 ; } if ( sumOfDigits <= 162 ) pairs . push_back ( sumOfDigits ) ; } } int countCoPrime ( int a , int b ) { vector < int > pairs ; makePairs ( pairs , a , b ) ; int count = 0 ; for ( int i = 0 ; i < pairs . size ( ) ; i ++ ) for ( int j = i + 1 ; j < pairs . size ( ) ; j ++ ) if ( __gcd ( pairs [ i ] , pairs [ j ] ) == 1 ) count ++ ; return count ; } int main ( ) { int a = 12 , b = 15 ; cout << countCoPrime ( a , b ) ; return 0 ; }