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
using System ; using System . Collections . Generic ; class GFG { static int GCD ( int a , int b ) { return b == 0 ? a : GCD ( b , a % b ) ; } static bool coprime ( int a , int b ) { if ( GCD ( a , b ) == 1 ) return true ; return false ; } static int numOfPairs ( List < 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 ( Int32 . Parse ( arr [ i ] ) , Int32 . Parse ( arr [ j ] ) ) ) { count = count + 1 ; } } } return count ; } static void noOfCoPrimePairs ( int N , int d1 , int d2 ) { List < string > l = new List < string > ( ) ; l . Add ( d1 . ToString ( ) ) ; l . Add ( d2 . ToString ( ) ) ; l . Sort ( ) ; if ( N < Int32 . Parse ( l [ 1 ] ) ) return ; List < string > total = new List < string > ( l ) ; List < string > temp2 = new List < string > ( l ) ; int flag = 0 ; List < string > temp3 = new List < string > ( ) ; while ( l [ 0 ] . Length < 10 ) { for ( int i = 0 ; i < l . Count ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( Int32 . Parse ( l [ i ] + temp2 [ j ] ) > N ) { flag = 1 ; break ; } total . Add ( l [ i ] + temp2 [ j ] ) ; temp3 . Add ( l [ i ] + temp2 [ j ] ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = temp3 ; temp3 . Clear ( ) ; } int lenOfTotal = total . Count ; int ans = numOfPairs ( total , lenOfTotal ) ; Console . WriteLine ( ans ) ; } public static void Main ( ) { int N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ; } }