Skip to content
Related Articles

Related Articles

Improve Article

Given GCD G and LCM L, find number of possible pairs (a, b)

Geek Week

We need to find number of possible pairs (a, b) such that GCD(a, b) is equal to given G and LCM (a, b) such that LCM(a, b) is equal to given L.
Examples: 
 

Input : G = 2, L = 12
Output : 4
Explanation : There are 4 possible pairs :
 (2, 12), (4, 6), (6, 4), (12, 2)

 

Input : G = 3, L = 6
Output : 2
Explanation : There are 2 possible pairs :
(3, 6), (6, 3)

 

 

Solution 1 (Simple):



Since a and b both will be less than or equal to lcm(a, b) L, so we try all possible pairs that have product equal to L * G. Note that product of a and b is same as product of gcd(a, b) and lcm(a, b), a*b = G*L.
Here is our algorithm 
 

p = G*L
count = 0
for a = 1 to L
    if p%a == 0 and gcd(a, p/a) = G
        count++
    end if
end for
return count

 

C++




// C++ program to find all pairs
// with given GCD and LCM.
#include <iostream>
using namespace std;
 
// C++ function to calculate GCD
// of two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b%a, a);
}
 
// C++ function to count number
// of pairs with given GCD and LCM
int countPairs(int G, int L)
{
    // To store count
    int count = 0;
 
    // To store product a*b = G*L
    int p = G*L;
 
    // p/a will be b if a divides p
    for (int a=1; a<=L; a++)
        if (!(p%a) && gcd(a, p/a)==G)
            count++;
 
    return count;
}
 
// Driver code to test above functions
int main()
{
    int G = 2, L = 12;
    cout << "Total possible pair with GCD " << G;
    cout << " & LCM " << L;
    cout <<" = " << countPairs(G, L);
    return 0;
}

Java




// Java program to find all pairs
// with given GCD and LCM.
public class GCD
{
    // Java function to calculate GCD
    // of two numbers
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b%a , a);
    }
     
    // Java function to count number
    // of pairs with given GCD and LCM
    static int countPairs(int G, int L)
    {
        // To store count
        int count = 0;
         
        // To store product a*b = G*L
        int p = G*L;
         
        // p/a will be b if a divides p
        for (int a = 1; a<=L; a++)
            if ((p%a == 0) && gcd(a, p/a) == G)
                count++;
                 
       return count;
    }
     
    public static void main (String[] args)
    {
        int G = 2, L = 12;
        System.out.print("Total possible pair with GCD " + G);
        System.out.print(" & LCM " + L);
        System.out.print(" = " + countPairs(G, L));
         
    }
     
}
 
// This code is contributed by Saket Kumar

Python3




# Python3 program to find all pairs
# with given GCD and LCM.
import math
 
# Function to calculate GCD
# of two numbers
def gcd(a, b):
 
    if (a == 0):
        return b
    return math.gcd(b % a, a)
 
# Function to count number of
# pairs with given GCD and LCM
def countPairs(G, L):
 
    # To store count
    count = 0
 
    # To store product a*b = G*L
    p = G * L
 
    # p/a will be b if a divides p
    for a in range(1, L + 1):
        if (not (p % a) and
            math.gcd(a, p // a) == G):
            count += 1
 
    return count
 
# Driver Code
if __name__ == "__main__":
 
    G = 2
    L = 12
    print ("Total possible pair with GCD ",
                               G, end = "")
    print (" & LCM ", L, end = "")
    print (" = ", countPairs(G, L))
 
# This code is contributed by ita_c

C#




// C# program to find all pairs
// with given GCD and LCM.
using System;
 
class GCD
{
     
    // function to calculate
    // GCD of two numbers
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a , a);
    }
     
    // Function to count number of 
    // pairs with given GCD and LCM
    static int countPairs(int G, int L)
    {
         
        // To store count
        int count = 0;
         
        // To store product a *
        // b = G * L
        int p = G * L;
         
        // p / a will be b if
        // a divides p
        for (int a = 1; a <= L; a++)
            if ((p % a == 0) &&
                 gcd(a, p / a) == G)
                count++;
                 
    return count;
    }
     
    // Driver Code
    public static void Main ()
    {
        int G = 2, L = 12;
        Console.Write("Total possible pair with GCD " + G);
        Console.Write(" & LCM " + L);
        Console.Write(" = " + countPairs(G, L));
         
    }
     
}
 
// This code is contributed by Nitin Mittal.

PHP




<?php
// PHP program to find all pairs
// with given GCD and LCM.
 
// function to calculate GCD
// of two numbers
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
 
// function to count number
// of pairs with given GCD and LCM
function countPairs( $G, $L)
{
     
    // To store count
    $count = 0;
 
    // To store product a*b = G*L
    $p = $G * $L;
 
    // p/a will be b if a divides p
    for ($a = 1; $a <= $L; $a++)
        if (!($p % $a) and
           gcd($a, $p / $a) == $G)
            $count++;
 
    return $count;
}
 
    // Driver Code
    $G = 2;
    $L = 12;
    echo "Total possible pair with GCD " , $G;
    echo " & LCM " , $L;
    echo " = " , countPairs($G, $L);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
// javascript program to find all pairs
// with given GCD and LCM.
 
// javascript function to calculate GCD
    // of two numbers
    function gcd(a , b) {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
 
// javascript function to count number
    // of pairs with given GCD and LCM
    function countPairs(G , L) {
        // To store count
        var count = 0;
 
        // To store product a*b = G*L
        var p = G * L;
 
        // p/a will be b if a divides p
        for (let a = 1; a <= L; a++)
            if ((p % a == 0) && gcd(a, p / a) == G)
                count++;
 
        return count;
    }
 
        var G = 2, L = 12;
        document.write("Total possible pair with GCD " + G);
        document.write(" & LCM " + L);
        document.write(" = " + countPairs(G, L));
 
// This code is contributed by todaysgaurav
</script>
Output
Total possible pair with GCD 2 & LCM 12 = 4

Auxiliary Space : O(1) 
Time Complexity: O( L * log(L) ). We have one for loop which iterates L times and in each iteration in the worst case, gcd will be called so O(log L) in the worst case for that call.
 

Solution 2 (Efficient):

We know that G * L = a * b
Since G is gcd(a, b), both a and b will have G as its factor
Let A = a/G
Let B = b/G

From above definitions of A and B, GCD of A and B must be 1.
We can write, a = G * A, b = G * B

G * L = G * A * G * B
A * B = L / G
Now, we need to find all possible pairs of (A, B)
such that gcd(A, B) = 1 and A*B = L/G
Let say p1, p2, ..., pk are prime factors of L/G.
Then if p1 is present in prime factorization of A then p1
can't be present in prime factorization of B because 
gcd(A, B) = 1.
Therefore each prime factor pi will be present in either
A or B. Hence total possible ways to divide all prime 
factors among A and B is 2^k, where L/G has k distinct 
prime factors.

Below is implementation of above steps.
 

C++




// Efficient C++ program to count all         
// pairs with GCD and LCM.         
#include <iostream>         
using namespace std;         
// A function to find number of distinct         
// prime factors of a given number n         
int totalPrimeFactors(int n)         
{         
 // To keep track of count         
 int count = 0;         
 // 2s that divide n         
 if (!(n%2))         
 {         
 count++;         
 while (!(n%2))         
 n /= 2;         
 }         
 // n must be odd at this point. So we can skip         
 // one element (Note i = i +2)         
 for (int i = 3; i*i <= n; i = i+2)         
 {         
 // i divides n         
 if (!(n%i))         
 {         
 count++;         
 while (!(n%i))         
 n /= i;         
 }         
 }         
 // This condition is to handle the case when n         
 // is a prime number greater than 2         
 if (n>2)         
 count++;         
 return count;         
}         
// C++ function to count number         
// of pair with given GCD and LCM         
int countPairs(int G, int L)         
{         
 if (L % G != 0)         
 return 0;         
 int div = L/G;         
 // answer is 2^totalPrimeFactors(L/G)         
 return (1<<totalPrimeFactors(div));         
}         
// Driver code to test above functions         
int main()         
{         
 int G = 2, L = 12;         
 cout << "Total possible pair with GCD" << G;         
 cout << " &LCM " << L;         
 cout << " = " << countPairs(G, L);         
 return 0;         
}

Java




// Efficient Java program to count all
// pairs with GCD and LCM.
public class GCD
{
    // A function to find number of distinct
    // prime factors of a given number n
    static int totalPrimeFactors(int n)
    {
        // To keep track of count
        int count = 0;
         
        // 2s that divide n
        if ((n%2 == 0))
        {
            count++;
            while ((n%2 == 0))
               n /= 2;
        }
        // n must be odd at this point. So we can skip
        // one element (Note i = i +2)
        for (int i = 3; i*i <= n; i = i+2)
        {
            // i divides n
            if ((n%i == 0))
                count++;
                while ((n%i == 0))
                    n /= 2;
        }
        // This condition is to handle the case when n
        // is a prime number greater than 2
        if (n > 2)
            count++;
        
       return count;
    }
    // Java function to count number
    // of pair with given GCD and LCM
    static int countPairs(int G, int L)
    {
        if (L % G != 0)
            return 0;
         
        int div = L/G;
         
        // answer is 2^totalPrimeFactors(L/G)
        return (1 << totalPrimeFactors(div));
    }
     
    // Driver function
    public static void main (String[] args)
    {
        int G = 2, L = 12;
        System.out.print("Total possible pair with GCD " + G);
        System.out.print(" & LCM " + L);
        System.out.print(" = " + countPairs(G, L));
    }
}
 
// This code is contributed by Saket Kumar

Python3




# Efficient python3 program to count
# all pairs with GCD and LCM.
 
# A function to find number of distinct
# prime factors of a given number n
def totalPrimeFactors(n):
     
    # To keep track of count
    count = 0;
 
    # 2s that divide n
    if ((n % 2) == 0):
        count += 1;
        while ((n % 2) == 0):
            n //= 2;
 
    # n must be odd at this point.
    # So we can skip one element
    # (Note i = i +2)
    i = 3;
    while (i * i <= n):
         
        # i divides n
        if ((n % i) == 0):
            count += 1;
            while ((n % i) == 0):
                n //= i;
        i += 2;
 
    # This condition is to handle the
    # case when n is a prime number
    # greater than 2
    if (n > 2):
        count += 1;
 
    return count;
 
# function to count number
# of pair with given GCD and LCM
def countPairs(G, L):
 
    if (L % G != 0):
        return 0;
 
    div = int(L / G);
 
    # answer is 2^totalPrimeFactors(L/G)
    return (1 << totalPrimeFactors(div));
 
# Driver Code
G = 2;
L = 12;
print("Total possible pair with GCD",
           G, "& LCM", L, end = "");
print(" =", countPairs(G, L));
 
# This code is contributed by mits

C#




// Efficient C# program to count all
// pairs with GCD and LCM.
using System;
 
class GFG {
     
    // A function to find number
    // of distinct prime factors
    // of a given number n
    static int totalPrimeFactors(int n)
    {
         
        // To keep track of count
        int count = 0;
         
        // 2s that divide n
        if ((n % 2 == 0))
        {
            count++;
            while ((n % 2 == 0))
            n /= 2;
        }
         
        // n must be odd at this
        // point. So we can skip
        // one element (Note i = i+2)
        for (int i = 3; i * i <= n;
                        i = i + 2)
        {
             
            // i divides n
            if ((n % i == 0))
                count++;
                while ((n % i == 0))
                    n /= 2;
        }
         
        // This condition is to
        // handle the case when n
        // is a prime number
        // greater than 2
        if (n > 2)
            count++;
         
    return count;
    }
     
    // function to count number
    // of pair with given GCD and LCM
    static int countPairs(int G, int L)
    {
        if (L % G != 0)
            return 0;
         
        int div = L/G;
         
        // answer is 2^totalPrimeFactors(L/G)
        return (1 << totalPrimeFactors(div));
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
        int G = 2, L = 12;
        Console.Write("Total possible pair with GCD " + G);
        Console.Write(" & LCM " + L);
        Console.Write(" = " + countPairs(G, L));
    }
}
 
// This code is contributed by Anshul Aggarwal.

PHP




<?php
// Efficient php program to count all
// pairs with GCD and LCM.
 
// A function to find number of distinct
// prime factors of a given number n
function totalPrimeFactors($n)
{
     
    // To keep track of count
    $count = 0;
 
    // 2s that divide n
    if (!($n % 2))
    {
        $count++;
        while (!($n % 2))
            $n /= 2;
    }
 
    // n must be odd at this point.
    // So we can skip one element
    // (Note i = i +2)
    for ($i = 3; $i * $i <= $n; $i = $i + 2)
    {
         
        // i divides n
        if (!($n % $i))
        {
            $count++;
            while (!($n % $i))
                $n /= $i;
        }
    }
 
    // This condition is to
    // handle the case when n
    // is a prime number greater
    // than 2
    if ($n > 2)
        $count++;
 
    return $count;
}
 
// function to count number
// of pair with given GCD and LCM
function countPairs($G, $L)
{
    if ($L % $G != 0)
    return 0;
 
    $div = $L/$G;
 
    // answer is 2^totalPrimeFactors(L/G)
    return (1 << totalPrimeFactors($div));
}
 
    // Driver Code
    $G = 2;
    $L = 12;
    echo "Total possible pair with GCD " , $G;
    echo " & LCM " , $L;
    echo " = " ,countPairs($G, $L);
    return 0;
 
// This code is contributed by nitin mittal.
?>

Javascript




<script>
 
// Efficient javascript program to count all
// pairs with GCD and LCM.
 
    // A function to find number of distinct
    // prime factors of a given number n
    function totalPrimeFactors(n) {
        // To keep track of count
        var count = 0;
 
        // 2s that divide n
        if ((n % 2 == 0)) {
            count++;
            while ((n % 2 == 0))
                n = parseInt(n/2);
        }
        // n must be odd at this point. So we can skip
        // one element (Note i = i +2)
        for (i = 3; i * i <= n; i = i + 2) {
            // i divides n
            if ((n % i == 0))
                count++;
            while ((n % i == 0))
                n = parseInt(n/2);
        }
        // This condition is to handle the case when n
        // is a prime number greater than 2
        if (n > 2)
            count++;
 
        return count;
    }
 
// javascript function to count number
    // of pair with given GCD and LCM
    function countPairs(G , L) {
        if (L % G != 0)
            return 0;
 
        var div = L / G;
 
        // answer is 2^totalPrimeFactors(L/G)
        return (1 << totalPrimeFactors(div));
    }
 
    // Driver function
     
        var G = 2, L = 12;
        document.write("Total possible pair with GCD " + G);
        document.write(" & LCM " + L);
        document.write(" = " + countPairs(G, L));
 
// This code is contributed by aashish1995
 
</script>

Output: 

Total possible pair with GCD 2 & LCM 12 = 4

Analysis of above algorithm 
Auxiliary Space: O(1) 
Time Complexity : O(sqrt(L/G) * log(L/G)). For time complexity to find number of distinct prime factors we need O(sqrt(L/G) * log (L/G)) time, Here sqrt(L) iterations are there in the worst case and in each iteration O(log L/G) iterations again.
This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :