给定一个n个数字的数组,找到它的LCM。
Input : {1, 2, 8, 3}
Output : 24
Input : {2, 7, 3, 9, 4}
Output : 252
我们知道,
上述关系仅适用于两个数字,
这里的想法是将我们的关系扩展到两个以上的数字。假设我们有一个数组arr [],其中包含需要计算其LCM的n个元素。
我们算法的主要步骤是:
- 初始化ans = arr [0]。
- 遍历数组的所有元素,即从i = 1到i = n-1
在第i次迭代中,ans = LCM(arr [0],arr [1],…..,arr [i-1])。当LCM(arr [0],arr [1],…。,arr [i])= LCM(ans,arr [i])时,可以很容易地做到这一点。因此,在第i次迭代中,我们只需要做ans = LCM(ans,arr [i])= ans x arr [i] / gcd(ans,arr [i])
下面是上述算法的实现:
C++
// C++ program to find LCM of n elements
#include
using namespace std;
typedef long long int ll;
// Utility function to find
// GCD of 'a' and 'b'
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Returns LCM of array elements
ll findlcm(int arr[], int n)
{
// Initialize result
ll ans = arr[0];
// ans contains LCM of arr[0], ..arr[i]
// after i'th iteration,
for (int i = 1; i < n; i++)
ans = (((arr[i] * ans)) /
(gcd(arr[i], ans)));
return ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 7, 3, 9, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%lld", findlcm(arr, n));
return 0;
}
Java
// Java Program to find LCM of n elements
public class GFG {
public static long lcm_of_array_elements(int[] element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
while (true) {
int counter = 0;
boolean divisible = false;
for (int i = 0; i < element_array.length; i++) {
// lcm_of_array_elements (n1, n2, ... 0) = 0.
// For negative number we convert into
// positive and calculate lcm_of_array_elements.
if (element_array[i] == 0) {
return 0;
}
else if (element_array[i] < 0) {
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1) {
counter++;
}
// Divide element_array by devisor if complete
// division i.e. without remainder then replace
// number with quotient; used for find next factor
if (element_array[i] % divisor == 0) {
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}
// If divisor able to completely divide any number
// from array multiply with lcm_of_array_elements
// and store into lcm_of_array_elements and continue
// to same divisor for next factor finding.
// else increment divisor
if (divisible) {
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else {
divisor++;
}
// Check if all element_array is 1 indicate
// we found all factors and terminate while loop.
if (counter == element_array.length) {
return lcm_of_array_elements;
}
}
}
// Driver Code
public static void main(String[] args)
{
int[] element_array = { 2, 7, 3, 9, 4 };
System.out.println(lcm_of_array_elements(element_array));
}
}
// Code contributed by Mohit Gupta_OMG
Python
# Python Program to find LCM of n elements
def find_lcm(num1, num2):
if(num1>num2):
num = num1
den = num2
else:
num = num2
den = num1
rem = num % den
while(rem != 0):
num = den
den = rem
rem = num % den
gcd = den
lcm = int(int(num1 * num2)/int(gcd))
return lcm
l = [2, 7, 3, 9, 4]
num1 = l[0]
num2 = l[1]
lcm = find_lcm(num1, num2)
for i in range(2, len(l)):
lcm = find_lcm(lcm, l[i])
print(lcm)
# Code contributed by Mohit Gupta_OMG
C#
// C# Program to find LCM of n elements
using System;
public class GFG {
public static long lcm_of_array_elements(int[] element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
while (true) {
int counter = 0;
bool divisible = false;
for (int i = 0; i < element_array.Length; i++) {
// lcm_of_array_elements (n1, n2, ... 0) = 0.
// For negative number we convert into
// positive and calculate lcm_of_array_elements.
if (element_array[i] == 0) {
return 0;
}
else if (element_array[i] < 0) {
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1) {
counter++;
}
// Divide element_array by devisor if complete
// division i.e. without remainder then replace
// number with quotient; used for find next factor
if (element_array[i] % divisor == 0) {
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}
// If divisor able to completely divide any number
// from array multiply with lcm_of_array_elements
// and store into lcm_of_array_elements and continue
// to same divisor for next factor finding.
// else increment divisor
if (divisible) {
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else {
divisor++;
}
// Check if all element_array is 1 indicate
// we found all factors and terminate while loop.
if (counter == element_array.Length) {
return lcm_of_array_elements;
}
}
}
// Driver Code
public static void Main()
{
int[] element_array = { 2, 7, 3, 9, 4 };
Console.Write(lcm_of_array_elements(element_array));
}
}
// This Code is contributed by nitin mittal
PHP
Javascript
输出 :
252
相关文章:
- 在不使用GCD的情况下查找两个以上(或数组)数字的LCM
- 用于在C++中计算LCM的内置函数