Number of co | Python3 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
from copy import deepcopy NEW_LINE import math NEW_LINE def coprime ( a , b ) : NEW_LINE INDENT return ( math . gcd ( a , b ) ) == 1 NEW_LINE DEDENT def numOfPairs ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , N - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( coprime ( int ( arr [ i ] ) , int ( arr [ j ] ) ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def noOfCoPrimePairs ( N , d1 , d2 ) : NEW_LINE INDENT l = [ ] NEW_LINE l . append ( str ( d1 ) ) NEW_LINE l . append ( str ( d2 ) ) NEW_LINE l . sort ( ) NEW_LINE if int ( N ) < int ( l [ 1 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT total = temp2 = deepcopy ( l ) NEW_LINE flag = 0 NEW_LINE temp3 = [ ] NEW_LINE while len ( l [ 0 ] ) < 10 : NEW_LINE INDENT for i in range ( len ( l ) ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT if int ( l [ i ] + temp2 [ j ] ) > int ( N ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT total . append ( l [ i ] + temp2 [ j ] ) NEW_LINE temp3 . append ( l [ i ] + temp2 [ j ] ) NEW_LINE DEDENT if flag == 1 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if flag == 1 : NEW_LINE INDENT break NEW_LINE DEDENT l = deepcopy ( temp3 ) NEW_LINE temp3 = [ ] NEW_LINE DEDENT lenOfTotal = len ( total ) NEW_LINE ans = numOfPairs ( total , lenOfTotal ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 30 NEW_LINE d1 = 2 NEW_LINE d2 = 3 NEW_LINE noOfCoPrimePairs ( N , d1 , d2 ) NEW_LINE DEDENT