Maximum count of common divisors of A and B such that all are co-primes to one another
Given two integers A and B. The task is to find the count of maximum elements from the common divisors of A and B such that all selected elements are co-prime to one another.
Examples:
Input: A = 12, B = 18
Output: 3
Common divisors of A and B are 1, 2, 3 and 6.
Select 1, 2, and 3. All the pairs are co primes to
one another i.e. gcd(1, 2) = gcd(1, 3) = gcd(2, 3) = 1.
Input: A = 1, B = 3
Output: 1
Approach: It can be observed that all the common factors of A and B must be a factor of their gcd. And, in order for the factors of this gcd to be co-prime to one another, one element of the pair must be either 1 or both the elements must be prime. So the answer will be 1 more than the count of prime divisors of gcd(A, B). Note that 1 is added because 1 can also be a part of the chosen divisors as its gcd with the other pairs will always be 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCommonFactors( int a, int b)
{
int gcd = __gcd(a, b);
int ans = 1;
for ( int i = 2; i * i <= gcd; i++) {
if (gcd % i == 0) {
ans++;
while (gcd % i == 0)
gcd /= i;
}
}
if (gcd != 1)
ans++;
return ans;
}
int main()
{
int a = 12, b = 18;
cout << maxCommonFactors(a, b);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int maxCommonFactors( int a, int b)
{
int __gcd = gcd(a, b);
int ans = 1 ;
for ( int i = 2 ; i * i <= __gcd; i++)
{
if (__gcd % i == 0 )
{
ans++;
while (__gcd % i == 0 )
__gcd /= i;
}
}
if (__gcd != 1 )
ans++;
return ans;
}
public static void main (String[] args)
{
int a = 12 , b = 18 ;
System.out.println(maxCommonFactors(a, b));
}
}
|
Python3
import math
def maxCommonFactors(a, b):
gcd = math.gcd(a, b)
ans = 1 ;
i = 2
while (i * i < = gcd):
if (gcd % i = = 0 ):
ans + = 1
while (gcd % i = = 0 ):
gcd = gcd / / i
i + = 1
if (gcd ! = 1 ):
ans + = 1
return ans
a = 12
b = 18
print (maxCommonFactors(a, b))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int maxCommonFactors( int a, int b)
{
int __gcd = gcd(a, b);
int ans = 1;
for ( int i = 2; i * i <= __gcd; i++)
{
if (__gcd % i == 0)
{
ans++;
while (__gcd % i == 0)
__gcd /= i;
}
}
if (__gcd != 1)
ans++;
return ans;
}
public static void Main (String[] args)
{
int a = 12, b = 18;
Console.WriteLine(maxCommonFactors(a, b));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O((log(min(a, b)))2), where a and b are two parameters of gcd
Auxiliary Space: O(1)
Last Updated :
15 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...