📜  用给定的LCM查找两个数字,并求出最小可能差

📅  最后修改于: 2021-04-22 03:40:58             🧑  作者: Mango

给定一个整数X ,任务是找到两个整数AB ,使得LCM(A,B)= X,并且AB之间的差最小。

例子:

方法:一种解决此问题的方法是使用本文讨论的方法找到给定数量的所有因素,然后找到满足给定条件并具有最小可能差异的对(A,B)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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


Python3
# 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 print the 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


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


输出:
3 4