[英]Least common multiple for more than two numbers
I want to find out Least Common Multiple(LCM) of more than two numbers.我想找出两个以上数字的最小公倍数(LCM)。 I know the formula of lcm(a,b) = (a * b) / gcd(a,b) .
我知道lcm(a,b) = (a * b) / gcd(a,b)的公式。 Let's say, I have an array of numbers: [2, 6, 8, 13] and the the lcm should be modulo M = 1000000007.
比方说,我有一个数字数组:[2,6,8,13] 并且 lcm 应该是模 M = 1000000007。
I have seen below code to calculate the LCM of multiple numbers but I am not understanding how the calculation is going on with both the loops.我已经看到下面的代码来计算多个数字的 LCM,但我不明白两个循环的计算是如何进行的。
int arr[] = {2, 6, 8, 13}, n = 4
long long int ans=1;
long long int M=1000000007;
for(int i=0;i<n;i++) // Calculating LCM
{
for(int j=i+1;j<n;j++)
{
arr[j]=arr[j]/__gcd(arr[i],arr[j]);
}
ans=((ans%M)*(arr[i]%M))%M;
}
return (ans)%M;
Can anyone please help me to understand the LCM calculation in the above code?谁能帮我理解上面代码中的 LCM 计算?
gcd(a, b)
represents the product of all the prime factors shared by a
and b
, what is the significance of a / gcd(a,b)
?gcd(a, b)
代表a
和b
共享的所有素因子的乘积,那么a / gcd(a,b)
的意义是什么? a / gcd(a, b)
is equal to the prime factors of a
that are not in b
. a / gcd(a, b)
等于a
中不在b
中的素数。
Therefore, when you multiple that quantity by b
, you get a product of all the prime factors of b
and all the prime factors of a
that are not in b
.因此,当您将该数量乘以
b
时,您会得到 b b
所有素因子和a
的所有不在b
中的素因子的乘积。 This is precisely lcm(a, b)
.这正是
lcm(a, b)
。
The lcm(a, b)
is a
times all the prime factors of b
not in a
or: lcm(a, b)
是a
乘以不在a
或中的b
的所有质因数:
a * (b / gcd(a, b)) = (a * b) / gcd(a, b)
Easy enough, you knew that already.很简单,你已经知道了。
But if we have a third number, lcm(a, b, c)
is a
times all the prime factors of b
not in a
times all the prime factors of c
in neither a
nor b
.但是,如果我们有第三个数,
lcm(a, b, c)
是a
乘以b
的所有素因数,而不是a
乘以c
的所有素因数,既不是a
也不是b
。 Well, the first part is straight forward, it's the same as above:嗯,第一部分是直截了当的,和上面一样:
lcm(a, b, c) = lcm(a, b) * (all the prime factors of c in neither a nor b)
How to calculate all the prime factors of c in neither a nor b
might not be obvious at first, but it's not overly complicated:如何计算
all the prime factors of c in neither a nor b
可能并不明显,但并不过分复杂:
all the prime factors of c in neither a nor b = c / (gcd(a, c) * gcd(b, c))
Which means that意思就是
lcm(a, b, c) = lcm(a, b) * c / (gcd(a, c) * gcd(b, c))
lcm(a, b, c) = (a * b * c) / (gcd(a, b) * gcd(a, c) * gcd(b, c))
And now, you can generalize easily:现在,您可以轻松概括:
lcm(a[0], ..., a[N]) = prod(a[0], ..., a[N]) / pairwise_gcd(a[0], ..., a[N])
But a more relevant formulation is the recursive version:但更相关的公式是递归版本:
lcm(a[0], ..., a[N]) = lcm(a[0], ..., a[N-1]) * a[N] / (gcd(a[0], a[N]) * ... * gcd(a[N-1], a[N]))
Or:或者:
lcm(a[0], ..., a[N]) = a[0] * lcm(a[1] / gcd(a[0], a[1]), ..., a[N] / gcd(a[0], a[N]))
Compare this to the last definition of lcm
on an array, I tried to make them appear similar.将此与数组上
lcm
的最后定义进行比较,我试图使它们看起来相似。
given int array = arrayOfNums
int product := 1
for number in arrayOfNums
remove all prime factors of number from all subsequent array elements
product = product * number
product is now the lcm of arrayOfNums
Hopefully, that wasn't too confusing;希望这不会太令人困惑。 I admit it may not be much of an explanation, but it is a starting point.
我承认这可能不是一个解释,但它是一个起点。 Please let me know if anything is still unclear.
如果还有什么不清楚的,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.