Count ordered pairs of numbers with a given LCM - GeeksforGeeks

10 May, 2021
Given an integer N, the task is to count the total number of ordered pairs such that the LCM of each pair is equal to N.
Examples:
Input: N = 6Output: 9 Explanation: Pairs with LCM equal to N(= 6) are {(1, 6), (2, 6), (2, 3), (3, 6), (6, 6), (6, 3), (3, 2), (6, 2), (6, 1)} Therefore, the output is 9.
Input: N = 36Output: 25
Approach: The problem can be solved based on the following observations:
Consider an ordered pair(X, Y). X = P1a1 * P2a2 * P3a3 *.....* Pnan Y = P1b1 * P2b2 * P3b3 *.....* PnbnHere, P1, P2, ....., Pn are prime factors of X and Y. LCM(X, Y) = P1max(a1, b1)  * P2max(a2, b2) *..........*Pnmax(an, bn)Therefore, LCM(X, Y) = N = P1m1 * P2m2 * P3m3 *.....* Pnmn
Therefore, total number of ordered pairs (X, Y) = [{(m1 + 1)2 – m12} * {(m2 + 1)2 – m22} * ......* {(mn + 1)2 – mn2} ]= (2*m1+1) * (2*m2+1) * (2*m3+1) * ........* (2*mn+1).
Follow the steps below to solve the problem:
Initialize a variable, say, countPower, to store the power of all prime factors of N.Calculate the power of all prime factors of N.Finally, print the count of ordered pairs(X, Y) using the aforementioned formula.
Initialize a variable, say, countPower, to store the power of all prime factors of N.
Calculate the power of all prime factors of N.
Finally, print the count of ordered pairs(X, Y) using the aforementioned formula.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to count the number of// ordered pairs with given LCMint CtOrderedPairs(int N){    // Stores count of    // ordered pairs    int res = 1;     // Calculate power of all    // prime factors of N    for (int i = 2; i * i <= N; i++) {         // Store the power of        // prime factors        int countPower = 0;        while (N % i == 0) {            countPower++;            N /= i;        }         res = res * (2 * countPower                     + 1);    }     if (N > 1) {        res = res * (2 * 1 + 1);    }    return res;} // Driver Codeint main(){    int N = 36;    cout << CtOrderedPairs(N);}
// Java program to implement// the above approach class GFG{   // Function to count the number of// ordered pairs with given LCMstatic int CtOrderedPairs(int N){         // Stores count of    // ordered pairs    int res = 1;     // Calculate power of all    // prime factors of N    for(int i = 2; i * i <= N; i++)    {                 // Store the power of        // prime factors        int countPower = 0;                 while (N % i == 0)        {            countPower++;            N /= i;        }        res = res * (2 * countPower + 1);    }     if (N > 1)    {        res = res * (2 * 1 + 1);    }    return res;} // Driver Codepublic static void main(String[] args){    int N = 36;         System.out.println(CtOrderedPairs(N));}} // This code is contributed by aimformohan
# Python3 program to implement# the above approach  # Function to count the number of# ordered pairs with given LCMdef CtOrderedPairs(N):     # Stores count of    # ordered pairs    res = 1      # Calculate power of all    # prime factors of N    i = 2    while(i * i <= N):          # Store the power of        # prime factors        countPower = 0        while (N % i == 0):            countPower += 1            N //= i          res = res * (2 * countPower + 1)        i += 1         if (N > 1):        res = res * (2 * 1 + 1)         return res # Driver CodeN = 36 print(CtOrderedPairs(N)) # This code is contributed by code_hunt
// C# program to implement// the above approachusing System;  class GFG{    // Function to count the number of// ordered pairs with given LCMstatic int CtOrderedPairs(int N){         // Stores count of    // ordered pairs    int res = 1;      // Calculate power of all    // prime factors of N    for(int i = 2; i * i <= N; i++)    {                  // Store the power of        // prime factors        int countPower = 0;                  while (N % i == 0)        {            countPower++;            N /= i;        }        res = res * (2 * countPower + 1);    }      if (N > 1)    {        res = res * (2 * 1 + 1);    }    return res;}  // Driver Codepublic static void Main(){    int N = 36;          Console.WriteLine(CtOrderedPairs(N));}} // This code is contributed by code_hunt
<script> // Javascript program to implement// the above approach // Function to count the number of// ordered pairs with given LCMfunction CtOrderedPairs(N){           // Stores count of    // ordered pairs    let res = 1;       // Calculate power of all    // prime factors of N    for(let i = 2; i * i <= N; i++)    {                   // Store the power of        // prime factors        let countPower = 0;                   while (N % i == 0)        {            countPower++;            N /= i;        }        res = res * (2 * countPower + 1);    }       if (N > 1)    {        res = res * (2 * 1 + 1);    }    return res;} // Driver Code     let N = 36;           document.write(CtOrderedPairs(N));           </script>
25
 
Time Complexity: O(√N) Auxiliary Space: O(1)
aimformohan
code_hunt
avijitmondal1998
LCM
maths-power
prime-factor
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Sieve of Eratosthenes
Program for Decimal to Binary Conversion
Euclidean algorithms (Basic and Extended)