📜  [L, R] 范围内所有对 (i, j) 中的最小和最大 LCM

📅  最后修改于: 2022-05-13 01:56:05.431000             🧑  作者: Mango

[L, R] 范围内所有对 (i, j) 中的最小和最大 LCM

给定两个正整数LR代表一个范围。任务是在[L, R]范围内找到任何对(i, j)的最小和最大可能 LCM,使得L ≤ i < j ≤ R

例子:

方法:这个问题可以用简单的数学来解决。请按照以下步骤解决给定的问题。

对于最小 LCM,

  • 一个数字肯定是[L, R]范围内的最小数字。
  • 以这样一种方式选择数字,即一个是另一个的因素。
  • L 给出最小 LCM 的唯一数字是2*L
  • 检查2*L <= R
    • 如果是,最小 LCM 将为2*L
    • 否则,最小 LCM 将为L*(L+1)

对于最大 LCM,

  • 一个数字肯定是[L, R]范围内的最大数字,即R
  • 选择第二个数,使其与 R 互质并且两者的乘积最大。
  • 如果R!=2RR-1将始终互质。
  • 因此, R*(R-1)将给出最大 LCM。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum LCM in range [L, R]
int minLCM(int L, int R)
{
 
    // If 2*L is within the the range
    // then minimum LCM would be 2*L
    if (2 * L <= R)
        return 2 * L;
 
    // Otherwise L * (L+1) would give
    // the minimum LCM
    else
        return L * (L + 1);
}
 
// Function to find maximum LCM in range [L, R]
int maxLCM(int L, int R)
{
 
    // The only possible equation that will give
    // the maximum LCM is R * (R-1)
    return R * (R - 1);
}
 
// Driver Code
int main()
{
    int L = 2;
    int R = 6;
 
    cout << minLCM(L, R) << ' ';
    cout << maxLCM(L, R);
}


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find minimum LCM in range [L, R]
    public static int minLCM(int L, int R) {
 
        // If 2*L is within the the range
        // then minimum LCM would be 2*L
        if (2 * L <= R)
            return 2 * L;
 
        // Otherwise L * (L+1) would give
        // the minimum LCM
        else
            return L * (L + 1);
    }
 
    // Function to find maximum LCM in range [L, R]
    public static int maxLCM(int L, int R) {
 
        // The only possible equation that will give
        // the maximum LCM is R * (R-1)
        return R * (R - 1);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int L = 2;
        int R = 6;
 
        System.out.print(minLCM(L, R) + " ");
        System.out.print(maxLCM(L, R));
    }
}


Python3
# Python program for the above approach
 
# Function to find minimum LCM in range [L, R]
def minLCM(L, R):
 
  # If 2*L is within the the range
  # then minimum LCM would be 2*L
  if (2 * L <= R):
    return 2 * L
 
  # Otherwise L * (L+1) would give
  # the minimum LCM
  else:
    return L * (L + 1)
 
# Function to find maximum LCM in range [L, R]
def maxLCM(L, R):
 
  # The only possible equation that will give
  # the maximum LCM is R * (R-1)
  return R * (R - 1);
 
# Driver Code
if __name__=="__main__":
   
  L = 2;
  R = 6;
 
  print(minLCM(L, R),end=' ')
  print(maxLCM(L, R))
 
#This code is contributed by Akash Jha


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to find minimum LCM in range [L, R]
    public static int minLCM(int L, int R)
    {
 
        // If 2*L is within the the range
        // then minimum LCM would be 2*L
        if (2 * L <= R)
            return 2 * L;
 
        // Otherwise L * (L+1) would give
        // the minimum LCM
        else
            return L * (L + 1);
    }
 
    // Function to find maximum LCM in range [L, R]
    public static int maxLCM(int L, int R)
    {
 
        // The only possible equation that will give
        // the maximum LCM is R * (R-1)
        return R * (R - 1);
    }
 
    // Driver Code
    public static void Main()
    {
        int L = 2;
        int R = 6;
 
        Console.Write(minLCM(L, R) + " ");
        Console.Write(maxLCM(L, R));
    }
}


Javascript


输出
4 30

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