Related Articles
Count of all possible pairs having sum of LCM and GCD equal to N
• Last Updated : 13 Apr, 2021

Given an integer N, the task is to find the count of all possible pairs of integers (A, B) such that GCD (A, B) + LCM (A, B) = N.

Examples:

Input: N = 14
Output: 7
Explanation:
All the possible pairs are {1, 13}, {2, 12}, {4, 6}, {6, 4}, {7, 7}, {12, 2}, {13, 1}

Input: N = 6
Output: 5

Approach:
Follow the steps below to solve the problem:

• Initialize a variable count, to store the count of all the possible pairs.
• Iterate over the range [1, N] to generate all possible pairs (i, j). Calculate the GCD of (i, j) using the __gcd() function and calculate LCM of (i, j).
• Now, check if the sum of LCM (i, j) and GCD (i, j) is equal to N or not. If so, increment count.
• Print the count value after the complete traversal of the range.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function to calculate and``// return LCM of two numbers``int` `lcm(``int` `a, ``int` `b)``{``    ``return` `(a * b) / __gcd(a, b);``}` `// Function to count pairs``// whose sum of GCD and LCM``// is equal to N``int` `countPair(``int` `N)``{``    ``int` `count = 0;``    ``for` `(``int` `i = 1;``        ``i <= N; i++) {` `        ``for` `(``int` `j = 1;``            ``j <= N; j++) {` `            ``if` `(__gcd(i, j)``                    ``+ lcm(i, j)``                ``== N) {` `                ``count++;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``int` `N = 14;``    ``cout << countPair(N);` `    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``class` `GFG{` `// Recursive function to return gcd of a and b``static` `int` `__gcd(``int` `a, ``int` `b)``{``    ``return` `b == ``0` `? a : __gcd(b, a % b);``}` `// Function to calculate and``// return LCM of two numbers``static` `int` `lcm(``int` `a, ``int` `b)``{``    ``return` `(a * b) / __gcd(a, b);``}` `// Function to count pairs``// whose sum of GCD and LCM``// is equal to N``static` `int` `countPair(``int` `N)``{``    ``int` `count = ``0``;``    ``for``(``int` `i = ``1``; i <= N; i++)``    ``{``        ``for``(``int` `j = ``1``; j <= N; j++)``        ``{``            ``if` `(__gcd(i, j) + lcm(i, j) == N)``            ``{``                ``count++;``            ``}``        ``}``    ``}``    ``return` `count;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``14``;``    ` `    ``System.out.print(countPair(N));``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 program to implement ``# the above approach ` `# Recursive function to return``# gcd of a and b ``def` `__gcd(a, b): ``    ` `    ``if` `b ``=``=` `0``:``        ``return` `a``    ``else``:``        ``return` `__gcd(b, a ``%` `b)``    ` `# Function to calculate and ``# return LCM of two numbers ``def` `lcm(a, b):` `    ``return` `(a ``*` `b) ``/``/` `__gcd(a, b) ` `# Function to count pairs ``# whose sum of GCD and LCM ``# is equal to N ``def` `countPair(N): ` `    ``count ``=` `0``    ` `    ``for` `i ``in` `range``(``1``, N ``+` `1``):``        ``for` `j ``in` `range``(``1``, N ``+` `1``):``            ``if` `(__gcd(i, j) ``+` `lcm(i, j) ``=``=` `N): ``                ``count ``+``=` `1` `            ` `    ``return` `count` `# Driver code``if` `__name__``=``=``"__main__"``:``    ` `    ``N ``=` `14` `      ` `    ``print``(countPair(N))` `# This code is contributed by rutvik_56`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG{` `// Recursive function to return gcd of a and b``static` `int` `__gcd(``int` `a, ``int` `b)``{``    ``return` `b == 0 ? a : __gcd(b, a % b);``}` `// Function to calculate and``// return LCM of two numbers``static` `int` `lcm(``int` `a, ``int` `b)``{``    ``return` `(a * b) / __gcd(a, b);``}` `// Function to count pairs``// whose sum of GCD and LCM``// is equal to N``static` `int` `countPair(``int` `N)``{``    ``int` `count = 0;``    ``for``(``int` `i = 1; i <= N; i++)``    ``{``        ``for``(``int` `j = 1; j <= N; j++)``        ``{``            ``if` `(__gcd(i, j) + lcm(i, j) == N)``            ``{``                ``count++;``            ``}``        ``}``    ``}``    ``return` `count;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 14;` `    ``Console.Write(countPair(N));``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``
Output:
`7`

Time Complexity: O(N3)
Auxiliary Space: O(1)

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