📌  相关文章
📜  找到最小LCM的对(a,b),使它们的总和等于N

📅  最后修改于: 2021-04-26 09:06:26             🧑  作者: Mango

给定数字N ,任务是找到两个数字a和b,使得a + b = NLCM(a,b)最小。

例子:

方法:想法是使用GCD和LCM的概念。步骤如下:

  • 如果N是素数,则答案为1N – 1,因为在任何其他情况下, a + b> NLCM(a,b)> N – 1 。这是因为如果N为质数,则意味着N为奇数。因此,a和b,它们中的任何一个都必须是奇数而其他偶数。因此,LCM(a,b)必须大于N (如果不是1且N – 1),因为2始终是一个因数。
  • 如果N不是质数,则选择a,b,使它们的GCD最大,因为公式LCM(a,b)= a * b / GCD(a,b)因此,为了最小化LCM(a,b),我们必须 最大化GCD(a,b)。
  • 如果xN的除数,则通过简单的数学运算 a和b可以分别表示为N / xN / x *(x – 1) 。现在,由于a = N / xb = N / x *(x – 1) ,因此它们的GCD表示为N / x 。为了最大限度地提高本GCD,取可能的最小x或N个最小可能除数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if number is
// prime or not
bool prime(int n)
{
    // As 1 is neither prime
    // nor composite return false
    if (n == 1)
        return false;
 
    // Check if it is divided by any
    // number then it is not prime,
    // return false
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false;
    }
 
    // Check if n is not divided
    // by any number then it is
    // prime and hence return true
    return true;
}
 
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
void minDivisior(int n)
{
 
    // Check if the number is prime
    if (prime(n)) {
        cout << 1 << " " << n - 1;
    }
 
    // Now, if it is not prime then
    // find the least divisior
    else {
        for (int i = 2; i * i <= n; i++) {
 
            // Check if divides n then
            // it is a factor
            if (n % i == 0) {
 
                // Required output is
                // a = n/i & b = n/i*(n-1)
                cout << n / i << " "
                     << n / i * (i - 1);
                break;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int N = 4;
 
    // Function call
    minDivisior(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if number is
// prime or not
static boolean prime(int n)
{
    // As 1 is neither prime
    // nor composite return false
    if (n == 1)
        return false;
 
    // Check if it is divided by any
    // number then it is not prime,
    // return false
    for (int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
 
    // Check if n is not divided
    // by any number then it is
    // prime and hence return true
    return true;
}
 
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
static void minDivisior(int n)
{
 
    // Check if the number is prime
    if (prime(n))
    {
        System.out.print(1 + " " +  (n - 1));
    }
 
    // Now, if it is not prime then
    // find the least divisior
    else
    {
        for (int i = 2; i * i <= n; i++)
        {
 
            // Check if divides n then
            // it is a factor
            if (n % i == 0)
            {
 
                // Required output is
                // a = n/i & b = n/i*(n-1)
                System.out.print(n / i + " " +
                                (n / i * (i - 1)));
                break;
            }
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4;
 
    // Function call
    minDivisior(N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Function to check if number is
# prime or not
def prime(n):
     
    # As 1 is neither prime
    # nor composite return false
    if (n == 1):
        return False
 
    # Check if it is divided by any
    # number then it is not prime,
    # return false
    for i in range(2, n + 1):
        if i * i > n:
            break
        if (n % i == 0):
            return False
 
    # Check if n is not divided
    # by any number then it is
    # prime and hence return true
    return True
 
# Function to find the pair (a, b)
# such that sum is N & LCM is minimum
def minDivisior(n):
 
    # Check if the number is prime
    if (prime(n)):
        print(1, n - 1)
 
    # Now, if it is not prime then
    # find the least divisior
    else:
        for i in range(2, n + 1):
            if i * i > n:
                break
 
            # Check if divides n then
            # it is a factor
            if (n % i == 0):
 
                # Required output is
                # a = n/i & b = n/i*(n-1)
                print(n // i, n // i * (i - 1))
                break
 
# Driver Code
N = 4
 
# Function call
minDivisior(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if number is
// prime or not
static bool prime(int n)
{
     
    // As 1 is neither prime
    // nor composite return false
    if (n == 1)
        return false;
 
    // Check if it is divided by any
    // number then it is not prime,
    // return false
    for(int i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }
 
    // Check if n is not divided
    // by any number then it is
    // prime and hence return true
    return true;
}
 
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
static void minDivisior(int n)
{
 
    // Check if the number is prime
    if (prime(n))
    {
        Console.Write(1 + " " + (n - 1));
    }
 
    // Now, if it is not prime then
    // find the least divisior
    else
    {
        for(int i = 2; i * i <= n; i++)
        {
             
            // Check if divides n then
            // it is a factor
            if (n % i == 0)
            {
                 
                // Required output is
                // a = n/i & b = n/i*(n-1)
                Console.Write(n / i + " " +
                             (n / i * (i - 1)));
                break;
            }
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 4;
 
    // Function call
    minDivisior(N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2 2

时间复杂度: O(平方(N))
辅助空间: O(1)