计算两个整数的最小公倍数的最有效方法是什么?
计算两个整数的最小公倍数的最有效方法是什么?
我刚刚想出这个,但它确实还有一些不足之处。
int n=7, m=4, n1=n, m1=m;
while( m1 != n1 ){
if( m1 > n1 )
n1 += n;
else
m1 += m;
}
System.out.println( "lcm is " + m1 );
What is the most efficient way to calculate the least common multiple of two integers?
I just came up with this, but it definitely leaves something to be desired.
int n=7, m=4, n1=n, m1=m;
while( m1 != n1 ){
if( m1 > n1 )
n1 += n;
else
m1 += m;
}
System.out.println( "lcm is " + m1 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
a
和b
的最小公倍数 (lcm) 是它们的乘积除以其最大公约数 (gcd)(即lcm(a, b) = ab /gcd(a,b)
)。那么,问题就变成了,如何找到gcd? 欧几里得算法通常是计算 gcd 的方法。经典算法的直接实现是高效的,但是有一些变体利用二进制算术来做得更好一些。请参阅 Knuth 的“计算机编程艺术" 第 2 卷,“半数值算法”§ 4.5.2。
The least common multiple (lcm) of
a
andb
is their product divided by their greatest common divisor (gcd) ( i.e.lcm(a, b) = ab/gcd(a,b)
).So, the question becomes, how to find the gcd? The Euclidean algorithm is generally how the gcd is computed. The direct implementation of the classic algorithm is efficient, but there are variations that take advantage of binary arithmetic to do a little better. See Knuth's "The Art of Computer Programming" Volume 2, "Seminumerical Algorithms" § 4.5.2.
记住
最小公倍数是两个或多个数字中每个数字的倍数的最小整数。
如果您试图计算三个整数的最小公倍数,请按照下列步骤操作:
写出每个数字的素因数分解。 19 是质数。您不需要对 19 进行因式分解。
重复每个质因数,使其达到其在上述任何质因数分解中出现的最大次数。
2 × 3 × 7 × 19 = 798
21、42 和 19 的最小公倍数是 798。
Remember
The least common multiple is the least whole number that is a multiple of each of two or more numbers.
If you are trying to figure out the LCM of three integers, follow these steps:
Write the prime factorization for each number. 19 is a prime number. You do not need to factor 19.
Repeat each prime factor the greatest number of times it appears in any of the prime factorizations above.
2 × 3 × 7 × 19 = 798
The least common multiple of 21, 42, and 19 is 798.
下面的 C++ 最佳解决方案不会溢出
Best solution in C++ below without overflowing
我认为“按最大公约数减少”的方法应该更快。首先计算 GCD(例如使用欧几里得算法),然后除两个数字的乘积由GCD。
I think that the approach of "reduction by the greatest common divider" should be faster. Start by calculating the GCD (e.g. using Euclid's algorithm), then divide the product of the two numbers by the GCD.
我不知道它是否已优化,但可能是最简单的:
I don't know whether it is optimized or not, but probably the easiest one:
扩展 @John D. Cook 答案,该答案也标记为该问题的答案。 ( https://stackoverflow.com/a/3154503/13272795),我正在分享查找 n 个数字的 LCM 的算法,可能是 2 个数字或任意数字的 LCM。此代码的来源是此
Extending @John D. Cook answer that is also marked answer for this question. ( https://stackoverflow.com/a/3154503/13272795), I am sharing algorithm to find LCM of n numbers, it maybe LCM of 2 numbers or any numbers. Source for this code is this
连续取两个数中较大者的倍数,直到结果是较小者的倍数。
这可能有用..
Take successive multiples of the larger of the two numbers until the result is a multiple of the smaller.
this might work..
这是在 python 中查找两个数字的 LCM 的高效方法。
Here is a highly efficient approach to find the LCM of two numbers in python.
使用欧几里得算法找到 gcd,然后计算 lcm 除以 gcd 和 b 的乘积,这对我有用。
Using Euclidean algorithm to find gcd and then calculating the lcm dividing a by the product of gcd and b worked for me.
C++ 模板。编译时间
C++ template. Compile time
2 个数字的乘积等于 LCM * GCD 或 HCF。所以求 LCM 的最好方法就是求 GCD 并用 GCD 划分乘积。即,LCM(a,b) = (a*b)/GCD(a,b)。
Product of 2 numbers is equal to LCM * GCD or HCF. So best way to find LCM is to find GCD and divide the product with GCD. That is, LCM(a,b) = (a*b)/GCD(a,b).
首先,你必须找到最大公约数
之后,使用GCD你可以轻松找到最小公倍数,如下所示
First of all, you have to find the greatest common divisor
After that, using the GCD you can easily find the least common multiple like this
没有比使用内置函数更有效的方法了!
从 Python 3.8 开始,数学库中添加了
lcm()
函数。并且可以使用以下签名进行调用:返回指定整数参数的最小公倍数。如果所有参数均非零,则返回值是所有参数的倍数的最小正整数。如果任何参数为零,则返回值为 0。不带参数的 lcm() 返回 1。
There is no way more efficient than using a built-in function!
As of Python 3.8
lcm()
function has been added in math library. And can be called with folowing signature:Returns the least common multiple of the specified integer arguments. If all arguments are nonzero, then the returned value is the smallest positive integer that is a multiple of all arguments. If any of the arguments is zero, then the returned value is 0. lcm() without arguments returns 1.
因为我们知道数学属性,即“任意两个数字的 LCM 和 HCF 的乘积等于这两个数字的乘积”。
假设 X 和 Y 是两个整数,
然后
X * Y = HCF(X, Y) * LCM(X, Y)
现在我们可以通过知道 HCF 来找到 LCM,我们可以通过欧几里德算法找到 HCF。
希望这会很有效率。
Since we know the mathematic property which states that "product of LCM and HCF of any two numbers is equal to the product of the two numbers".
lets say X and Y are two integers,
then
X * Y = HCF(X, Y) * LCM(X, Y)
Now we can find LCM by knowing the HCF, which we can find through Euclidean Algorithm.
Hope this will be efficient.
是的,计算 LCM 的方法有很多种,例如使用 GCD (HCF)。
您可以应用素数分解,例如(优化/朴素)Sieve Eratosthenes 或查找素数因子来计算 GCD,这比直接计算 LCM 快得多。那么如上所述,LCM(X, Y) = (X * Y) / GCD(X, Y)
Yes, there are numerous way to calculate LCM such as using GCD (HCF).
You can apply prime decomposition such as (optimized/naive) Sieve Eratosthenes or find factor of prime number to compute GCD, which is way more faster than calculate LCM directly. Then as all said above, LCM(X, Y) = (X * Y) / GCD(X, Y)
我用谷歌搜索了同样的问题,并找到了这个 Stackoverflow 页面,
我想出了另一个使用 python 的简单解决方案
不过
数字 = [120,150,135,225]
它将返回
5400
I googled the same question, and found this Stackoverflow page,
however I come up with another simple solution using python
for
numbers = [120,150,135,225]
it will return
5400
欧几里得 GCD 代码片段
Euclidean GCD code snippet