📜  要最小化两个给定数字的LCM的最小数量

📅  最后修改于: 2021-04-21 22:33:48             🧑  作者: Mango

给定两个数字AB ,任务是找到需要添加到AB的最小数字,以使它们的LCM最小。

例子:

方法:这个想法是基于广义公式,即(A + x)(B + x)的LCM等于(A + x)*(B + x)/ GCD (A + x,B + x) 。可以看出, (A + x)(B + x)的GCD等于(B – A)(A + x)的GCD。因此,gcd是(B − A)的除数。

因此,迭代(B − A)的所有除数,如果A%M = B%M (如果M是除数之一),则X的值(必须加上最小值)等于M − A%M

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function for finding all divisors
// of a given number
vector getDivisors(int diff)
{
    // Stores the divisors of the
    // number diff
    vector divisor;
 
    for (int i = 1; i * i <= diff; i++) {
 
        // If diff is a perfect square
        if (i == diff / i) {
            divisor.push_back(i);
            continue;
        }
 
        // If i divides diff then
        // diff / i also a divisor
        if (diff % i == 0) {
            divisor.push_back(i);
            divisor.push_back(diff / i);
        }
    }
 
    // Return the divisors stored
    return divisor;
}
 
// Function to find smallest integer x
// such that LCM of (A + X) and (B + X)
// is minimized
int findTheSmallestX(int a, int b)
{
    int diff = b - a;
 
    // Find all the divisors of diff
    vector divisor
        = getDivisors(diff);
 
    // Find LCM of a and b
    int lcm = (a * b) / __gcd(a, b);
 
    int ans = 0;
 
    for (int i = 0;
         i < (int)divisor.size(); i++) {
 
        // From equation x = M - a % M
        // here M = divisor[i]
        int x = (divisor[i]
                 - (a % divisor[i]));
 
        // If already checked for x == 0
        if (!x)
            continue;
 
        // Find the product
        int product = (b + x) * (a + x);
 
        // Find the GCD
        int tempGCD = __gcd(a + x, b + x);
        int tempLCM = product / tempGCD;
 
        // If current lcm is minimum
        // than previous lcm, update ans
        if (lcm > tempLCM) {
            ans = x;
            lcm = tempLCM;
        }
    }
 
    // Print the number added
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given A & B
    int A = 6, B = 10;
 
    // Function Call
    findTheSmallestX(A, B);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
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 for finding all
// divisors of a given number
static int[] getDivisors(int diff)
{
  // Stores the divisors of
  // the number diff
  Vector divisor =
         new Vector<>() ;
 
  for (int i = 1;
           i * i <= diff; i++)
  {
    // If diff is a perfect
    // square
    if (i == diff / i)
    {
      divisor.add(i);
      continue;
    }
 
    // If i divides diff then
    // diff / i also a divisor
    if (diff % i == 0)
    {
      divisor.add(i);
      divisor.add(diff / i);
    }
  }
   
  int []ans = new int[divisor.size()];
  int j = 0;
   
  for(int i: divisor)
    ans[j] = i;
   
  // Return the divisors
  // stored
  return ans;
}
 
// Function to find smallest integer
// x such that LCM of (A + X) and
// (B + X) is minimized
static void findTheSmallestX(int a,
                             int b)
{
  int diff = b - a;
 
  // Find all the divisors
  // of diff
  int[] divisor =
        getDivisors(diff);
 
  // Find LCM of a and b
  int lcm = (a * b) /
             __gcd(a, b);
 
  int ans = 0;
 
  for (int i = 0;
           i  tempLCM)
    {
      ans = x;
      lcm = tempLCM;
    }
  }
 
  // Print the number
  // added
  System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
  // Given A & B
  int A = 6, B = 10;
 
  // Function Call
  findTheSmallestX(A, B);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
from math import gcd
 
# Function for finding all divisors
# of a given number
def getDivisors(diff):
     
    # Stores the divisors of the
    # number diff
    divisor = []
 
    for i in range(1, diff):
        if i * i > diff:
            break
 
        # If diff is a perfect square
        if (i == diff // i):
            divisor.append(i)
            continue
 
        # If i divides diff then
        # diff / i also a divisor
        if (diff % i == 0):
            divisor.append(i)
            divisor.append(diff // i)
 
    # Return the divisors stored
    return divisor
 
# Function to find smallest integer x
# such that LCM of (A + X) and (B + X)
# is minimized
def findTheSmallestX(a, b):
     
    diff = b - a
 
    # Find all the divisors of diff
    divisor = getDivisors(diff)
 
    # Find LCM of a and b
    lcm = (a * b) // gcd(a, b)
 
    ans = 0
 
    for i in range(len(divisor)):
 
        # From equation x = M - a % M
        # here M = divisor[i]
        x = (divisor[i] - (a % divisor[i]))
 
        # If already checked for x == 0
        if (not x):
            continue
 
        # Find the product
        product = (b + x) * (a + x)
 
        # Find the GCD
        tempGCD = gcd(a + x, b + x)
        tempLCM = product // tempGCD
 
        # If current lcm is minimum
        # than previous lcm, update ans
        if (lcm > tempLCM):
            ans = x
            lcm = tempLCM
 
    # Print the number added
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    # Given A & B
    A = 6
    B = 10
 
    # Function Call
    findTheSmallestX(A, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
 
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 for finding all
// divisors of a given number
static int[] getDivisors(int diff)
{
   
  // Stores the divisors of
  // the number diff
  List divisor = new List();
 
  for(int i = 1; i * i <= diff; i++)
  {
     
    // If diff is a perfect
    // square
    if (i == diff / i)
    {
      divisor.Add(i);
      continue;
    }
 
    // If i divides diff then
    // diff / i also a divisor
    if (diff % i == 0)
    {
      divisor.Add(i);
      divisor.Add(diff / i);
    }
  }
   
  int []ans = new int[divisor.Count];
  int j = 0;
   
  foreach(int i in divisor)
    ans[j] = i;
   
  // Return the divisors
  // stored
  return ans;
}
 
// Function to find smallest integer
// x such that LCM of (A + X) and
// (B + X) is minimized
static void findTheSmallestX(int a,
                             int b)
{
  int diff = b - a;
 
  // Find all the divisors
  // of diff
  int[] divisor = getDivisors(diff);
 
  // Find LCM of a and b
  int lcm = (a * b) / __gcd(a, b);
 
  int ans = 0;
 
  for(int i = 0;
          i < divisor.Length; i++)
  {
     
    // From equation x = M - a % M
    // here M = divisor[i]
    int x = 0;
     
    if (divisor[i] != 0)
      x = (divisor[i] -
      (a % divisor[i]));
 
    // If already checked for
    // x == 0
    if (x == 0)
      continue;
 
    // Find the product
    int product = (b + x) *
                  (a + x);
 
    // Find the GCD
    int tempGCD = __gcd(a + x,
                        b + x);
    int tempLCM = product /
                  tempGCD;
 
    // If current lcm is
    // minimum than previous
    // lcm, update ans
    if (lcm > tempLCM)
    {
      ans = x;
      lcm = tempLCM;
    }
  }
   
  // Print the number
  // added
  Console.Write(ans);
}
 
// Driver Code
public static void Main(String[] args)
{
   
  // Given A & B
  int A = 6, B = 10;
 
  // Function Call
  findTheSmallestX(A, B);
}
}
 
// This code is contributed by 29AjayKumar


输出:
2











时间复杂度: O(sqrt(B – A))
辅助空间: O(max(A,B))