Number of co | C ++ 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 ; Driver Code ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int coprime ( int a , int b ) { return ( __gcd ( a , b ) == 1 ) ; } int numOfPairs ( vector < string > arr , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( coprime ( stoi ( arr [ i ] ) , stoi ( arr [ j ] ) ) ) { count = count + 1 ; } } } return count ; } int noOfCoPrimePairs ( int N , int d1 , int d2 ) { vector < string > l ; l . push_back ( to_string ( d1 ) ) ; l . push_back ( to_string ( d2 ) ) ; sort ( l . begin ( ) , l . end ( ) ) ; if ( N < stoi ( l [ 1 ] ) ) return 0 ; vector < string > total = l ; vector < string > temp2 = l ; int flag = 0 ; vector < string > temp3 ; while ( l [ 0 ] . length ( ) < 10 ) { for ( int i = 0 ; i < l . size ( ) ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( stoi ( l [ i ] + temp2 [ j ] ) > N ) { flag = 1 ; break ; } total . push_back ( l [ i ] + temp2 [ j ] ) ; temp3 . push_back ( l [ i ] + temp2 [ j ] ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = temp3 ; vector < string > temp3 ; } int lenOfTotal = total . size ( ) ; int ans = numOfPairs ( total , lenOfTotal ) ; cout << ( ans ) ; } int main ( ) { int N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ; }