Skip to content
Related Articles

Related Articles

Print any pair of integers with sum of GCD and LCM equals to N
  • Last Updated : 13 Apr, 2021

Given an integer N, the task is to print any pair of integers that have the sum of GCD and LCM equal to N.
Examples:

Input: N = 14 
Output: 1, 13 
Explanation
For the given pair we have GCD(1, 13) = 1 and LCM (1, 13) = 13. Sum of GCD and LCM = 1 + 13 = 14.

Input:
Output: 1 1 
Explanation
For the given pair we have GCD(1, 1) = 1 and LCM (1, 1) = 1. Sum of GCD and LCM = 1 + 1 = 2.  

Approach: 
To solve the problem mentioned above let us consider the pair to be (1, n-1). GCD of (1, n-1) = 1 and LCM of (1, n-1) = n – 1. So the sum of GCD and LCM = 1 + (n – 1) = n. Hence the pair (1, n – 1) will be the pair which has the sum of GCD and LCM equal to N.
Below is the implementation of the above approach:  

C++




// C++ implementation to Print any pair of integers
// whose summation of GCD and LCM is equal to integer N
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the required pair
void printPair(int n)
{
    // print the pair
    cout << 1 << " " << n - 1;
}
 
// Driver code
int main()
{
    int n = 14;
 
    printPair(n);
 
    return 0;
}

Java




// Java implementation to print any pair of integers
// whose summation of GCD and LCM is equal to integer N
class GFG{
 
// Function to print the required pair
static void printPair(int n)
{
    // Print the pair
    System.out.print(1 + " " + (n - 1));
}
 
// Driver code
public static void main(String[] args)
{
    int n = 14;
    printPair(n);
}
}
 
// This code is contributed by gauravrajput1

Python3




# Python3 implementation to print any
# pair of integers whose summation of
# GCD and LCM is equal to integer N
 
# Function to print the required pair
def printPair(n):
 
    # Print the pair
    print("1", end = " ")
    print(n - 1)
 
# Driver code
n = 14
printPair(n)
 
# This code is contributed by PratikBasu

C#




// C# implementation to print any pair
// of integers whose summation of
// GCD and LCM is equal to integer N
using System;
 
public class GFG{
 
// Function to print the required pair
static void printPair(int n)
{
     
    // Print the pair
    Console.Write(1 + " " + (n - 1));
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 14;
    printPair(n);
}
}
 
// This code is contributed by Princi Singh

Javascript




<script>
 
// javascript implementation to prvar any pair of integers
// whose summation of GCD and LCM is equal to integer N
// Function to prvar the required pair
    function printPair(n)
    {
     
        // Prvar the pair
        document.write(1 + " " + (n - 1));
    }
 
    // Driver code
        var n = 14;
        printPair(n);
 
// This code is contributed by aashish1995
</script>
Output: 



1 13

 

Time Complexity:O(1)

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
Recommended Articles
Page :