Maximum count of common divisors of A and B such that all are co | Javascript implementation of the approach ; Function to return the count of common factors of a and b such that all the elements are co - prime to one another ; GCD of a and b ; Include 1 initially ; Find all the prime factors of the gcd ; If gcd is prime ; Return the required answer ; Driver code
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function maxCommonFactors ( a , b ) { let gcd = GCD ( a , b ) ; let ans = 1 ; for ( let i = 2 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { ans ++ ; while ( gcd % i == 0 ) gcd = parseInt ( gcd / i ) ; } } if ( gcd != 1 ) ans ++ ; return ans ; } let a = 12 , b = 18 ; document . write ( maxCommonFactors ( a , b ) ) ;