Find two numbers with the given LCM and minimum possible difference

Given an integer X, the task is to find two integers A and B such that LCM(A, B) = X and the difference between the A and B is minimum possible.

Examples:

Input: X = 6
Output: 2 3
LCM(2, 3) = 6 and (3 – 2) = 1
which is the minimum possible.



Input X = 7
Output: 1 7

Approach: An approach to solve this problem is to find all the factors of the given number using the approach discussed in this article and then find the pair (A, B) that satisfies the given conditions and has the minimum possible differecne.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the LCM of a and b
int lcm(int a, int b)
{
    return (a / __gcd(a, b) * b);
}
  
// Function to find and print the two numbers
void findNums(int x)
{
  
    int ans;
  
    // To find the factors
    for (int i = 1; i <= sqrt(x); i++) {
  
        // To check if i is a factor of x and
        // the minimum possible number
        // satisfying the given conditions
        if (x % i == 0 && lcm(i, x / i) == x) {
  
            ans = i;
        }
    }
    cout << ans << " " << (x / ans);
}
  
// Driver code
int main()
{
    int x = 12;
  
    findNums(x);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
    // Function to return the LCM of a and b
    static int lcm(int a, int b)
    {
        return (a / __gcd(a, b) * b);
    }
  
    static int __gcd(int a, int b) 
    {
        return b == 0 ? a : __gcd(b, a % b);
    }
  
    // Function to find and print the two numbers
    static void findNums(int x)
    {
  
        int ans = -1;
  
        // To find the factors
        for (int i = 1; i <= Math.sqrt(x); i++)
        {
  
            // To check if i is a factor of x and
            // the minimum possible number
            // satisfying the given conditions
            if (x % i == 0 && lcm(i, x / i) == x)
            {
  
                ans = i;
            }
        }
        System.out.print(ans + " " + (x / ans));
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int x = 12;
  
        findNums(x);
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
from math import gcd as __gcd, sqrt, ceil
  
# Function to return the LCM of a and b
def lcm(a, b):
    return (a // __gcd(a, b) * b)
  
# Function to find and prthe two numbers
def findNums(x):
  
    ans = 0
  
    # To find the factors
    for i in range(1, ceil(sqrt(x))):
  
        # To check if i is a factor of x and
        # the minimum possible number
        # satisfying the given conditions
        if (x % i == 0 and lcm(i, x // i) == x):
  
            ans = i
  
    print(ans, (x//ans))
  
# Driver code
x = 12
  
findNums(x)
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
    // Function to return the LCM of a and b
    static int lcm(int a, int b)
    {
        return (a / __gcd(a, b) * b);
    }
  
    static int __gcd(int a, int b) 
    {
        return b == 0 ? a : __gcd(b, a % b);
    }
  
    // Function to find and print the two numbers
    static void findNums(int x)
    {
  
        int ans = -1;
  
        // To find the factors
        for (int i = 1; i <= Math.Sqrt(x); i++)
        {
  
            // To check if i is a factor of x and
            // the minimum possible number
            // satisfying the given conditions
            if (x % i == 0 && lcm(i, x / i) == x)
            {
  
                ans = i;
            }
        }
        Console.Write(ans + " " + (x / ans));
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int x = 12;
  
        findNums(x);
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

3 4



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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.