Maximum count of pairwise co-prime and common divisors of two given numbers
Last Updated :
11 May, 2021
Given an array of pairs arr[] of two numbers {N, M}, the task is to find the maximum count of common divisors for each pair N and M such that every pair between the common divisor are co-prime.
A number x is a common divisor of N and M if, N%x = 0 and M%x = 0.
Two numbers are co-prime if their Greatest Common Divisor is 1.
Examples:
Input: arr[][] = {{12, 18}, {420, 660}}
Output: 3 4
Explanation:
For pair (12, 18):
{1, 2, 3} are common divisors of both 12 and 18, and are pairwise co-prime.
For pair (420, 660):
{1, 2, 3, 5} are common divisors of both 12 and 18, and are pairwise co-prime.
Input: arr[][] = {{8, 18}, {20, 66}}
Output: 2 2
Approach: The maximum count of common divisors of N and M such that the GCD of all the pairs between them is always 1 is 1 and all the common prime divisors of N and M. To count all the common prime divisors the idea is to find the GCD(say G) of the given two numbers and then count the number of prime divisors of the number G.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int x, int y)
{
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
int countPairwiseCoprime( int N, int M)
{
int answer = 1;
int g = gcd(N, M);
int temp = g;
for ( int i = 2; i * i <= g; i++) {
if (temp % i == 0) {
answer++;
while (temp % i == 0)
temp /= i;
}
}
if (temp != 1)
answer++;
return answer;
}
void countCoprimePair( int arr[][2], int N)
{
for ( int i = 0; i < N; i++) {
cout << countPairwiseCoprime(arr[i][0],
arr[i][1])
<< ' ' ;
}
}
int main()
{
int arr[][2] = { { 12, 18 }, { 420, 660 } };
int N = sizeof (arr) / sizeof (arr[0]);
countCoprimePair(arr, N);
return 0;
}
|
Java
class GFG{
static int gcd( int x, int y)
{
if (x % y == 0 )
return y;
else
return gcd(y, x % y);
}
static int countPairwiseCoprime( int N, int M)
{
int answer = 1 ;
int g = gcd(N, M);
int temp = g;
for ( int i = 2 ; i * i <= g; i++)
{
if (temp % i == 0 )
{
answer++;
while (temp % i == 0 )
temp /= i;
}
}
if (temp != 1 )
answer++;
return answer;
}
static void countCoprimePair( int arr[][], int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(countPairwiseCoprime(arr[i][ 0 ],
arr[i][ 1 ]) + " " );
}
}
public static void main(String[] args)
{
int arr[][] = { { 12 , 18 }, { 420 , 660 } };
int N = arr.length;
countCoprimePair(arr, N);
}
}
|
Python3
def gcd(x, y):
if (x % y = = 0 ):
return y
else :
return gcd(y, x % y)
def countPairwiseCoprime(N, M):
answer = 1
g = gcd(N, M)
temp = g
for i in range ( 2 , g + 1 ):
if i * i > g:
break
if (temp % i = = 0 ) :
answer + = 1
while (temp % i = = 0 ):
temp / / = i
if (temp ! = 1 ):
answer + = 1
return answer
def countCoprimePair(arr, N):
for i in range (N):
print (countPairwiseCoprime(arr[i][ 0 ],
arr[i][ 1 ]),
end = " " )
if __name__ = = '__main__' :
arr = [ [ 12 , 18 ], [ 420 , 660 ] ]
N = len (arr)
countCoprimePair(arr, N)
|
C#
using System;
class GFG{
static int gcd( int x, int y)
{
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
static int countPairwiseCoprime( int N, int M)
{
int answer = 1;
int g = gcd(N, M);
int temp = g;
for ( int i = 2; i * i <= g; i++)
{
if (temp % i == 0)
{
answer++;
while (temp % i == 0)
temp /= i;
}
}
if (temp != 1)
answer++;
return answer;
}
static void countCoprimePair( int [,]arr, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(countPairwiseCoprime(arr[i, 0],
arr[i, 1]) + " " );
}
}
public static void Main(String[] args)
{
int [,]arr = { { 12, 18 }, { 420, 660 } };
int N = arr.GetLength(0);
countCoprimePair(arr, N);
}
}
|
Javascript
<script>
function gcd(x, y)
{
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
function countPairwiseCoprime(N, M)
{
let answer = 1;
let g = gcd(N, M);
let temp = g;
for (let i = 2; i * i <= g; i++)
{
if (temp % i == 0)
{
answer++;
while (temp % i == 0)
temp /= i;
}
}
if (temp != 1)
answer++;
return answer;
}
function countCoprimePair(arr, N)
{
for (let i = 0; i < N; i++)
{
document.write(countPairwiseCoprime(arr[i][0],
arr[i][1]) + " " );
}
}
let arr = [[ 12, 18 ], [ 420, 660 ]];
let N = arr.length;
countCoprimePair(arr, N);
</script>
|
Time Complexity: O(X*(sqrt(N) + sqrt(M))), where X is the number of pairs and N & M are two pairs in arr[].
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...