Count of all possible pairs having sum of LCM and GCD equal to N

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement 
// the above approach 
#include <bits/stdc++.h> 
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; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

7

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, GauravRajput1