C++计算多个数字的最小公倍数的算法
是否有 C++ 算法可以计算多个数字的最小公倍数,例如 lcm(3,6,12)
或 lcm(5,7,9,12)
?
Is there a C++ algorithm to calculate the least common multiple for multiple numbers, like lcm(3,6,12)
or lcm(5,7,9,12)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您可以使用 std::accumulate 和一些辅助函数:
You can use std::accumulate and some helper functions:
boost 提供了计算 2 个数字的 lcm 的函数(请参见此处)
事实
然后利用您也可以轻松计算多个数字的 lcm 的
boost provides functions for calculation lcm of 2 numbers (see here)
Then using the fact that
You can easily calculate lcm for multiple numbers as well
从 C++17 开始,您可以使用
std::lcm
。这里有一个小程序,展示了如何将其专门用于多个参数,
请在此处查看其实际操作:https://wandbox。 org/permlink/25jVinGytpvPaS4v
As of C++17, you can use
std::lcm
.And here is a little program that shows how to specialize it for multiple parameters
See it in action here: https://wandbox.org/permlink/25jVinGytpvPaS4v
该算法并非特定于 C++。 AFAIK,没有标准库函数。
要计算 LCM,首先使用欧几里得算法计算 GCD(最大公约数)。
http://en.wikipedia.org/wiki/Greatest_common_divisor
GCD 算法通常给出两个参数,但是...
要计算 LCM,请使用...
其逻辑基于素因数分解。更一般的形式(超过两个变量)是...
编辑-实际上,我认为最后一点可能是错误的。不过,第一个 LCM(对于两个参数)是正确的。
The algorithm isn't specific to C++. AFAIK, there's no standard library function.
To calculate the LCM, you first calculate the GCD (Greatest Common Divisor) using Euclids algorithm.
http://en.wikipedia.org/wiki/Greatest_common_divisor
The GCD algorithm is normally given for two parameters, but...
To calculate the LCM, use...
The logic for that is based on prime factorization. The more general form (more than two variables) is...
EDIT - actually, I think that last bit may be wrong. The first LCM (for two parameters) is right, though.
使用 GCC 和 C++14 以下代码对我有用:
在 C++17 中,有 std::lcm 函数 (http://en.cppreference.com/w/cpp/numeric/lcm),可以直接用于累加。
Using GCC with C++14 following code worked for me:
In C++17 there is std::lcm function (http://en.cppreference.com/w/cpp/numeric/lcm) that could be used in accumulate directly.
未内置于标准库中。您需要自己构建它,或者获得一个可以完成它的库。我敢打赌 Boost 有一个...
Not built in to the standard library. You need to either build it yourself or get a library that did it. I bet Boost has one...
我刚刚为多个数字创建了 gcd:
dbd - gcd。
n - 数字的数量。
I just created gcd for multiple numbers:
dbd - gcd.
n - number of numbers.
源代码参考
Source code reference
您可以在 boost 中计算 LCM 和/或 GCM,如下所示:(
示例取自 http://www.boost.org/doc/libs/1_31_0/libs/math/doc/common_factor.html)
You can calculate LCM and or GCM in boost like this:
(Example taken from http://www.boost.org/doc/libs/1_31_0/libs/math/doc/common_factor.html)
上面给出的代码仅讨论了如何评估多个数字的 LCM,但是很可能发生在执行乘法时溢出数据类型存储的整数限制< /strong>
*极端情况 :- *
例如
如果在评估时达到这样的情况,如果 LCM_till_now=1000000000000000 next_number_in_list=99999999999999 且因此 GCD=1 (因为它们彼此相对互质)
所以如果你执行操作 (LCM_till_now*next_number_in_list) 甚至不适合“ unsigned long long int”
补救措施:-
1.使用大整数类
2.或者如果问题要求 LCM%MOD------------> 然后应用模运算的属性。
The Codes given above only discusses about evaluating LCM for multiple numbers however it is very likely to happen that while performing multiplications we may overflow integer limit for data type storage
*A Corner Case :- *
e.g.
if while evaluating you reach situation such that if LCM_till_now=1000000000000000 next_number_in_list=99999999999999 and Hence GCD=1 (as both of them are relatively co-prime to each other)
So if u perform operation (LCM_till_now*next_number_in_list) will not even fit in "unsigned long long int"
Remedy :-
1.Use Big Integer Class
2.Or if the problem is asking for LCM%MOD----------->then apply properties of modular arithmetic.
利用 lcm 应该可整除的事实
由列表中的所有数字组成。这里的列表是一个包含数字的向量
Using the fact that lcm should be divisible
by all the numbers in list. Here the list is a vector containing numbers
我在搜索类似问题时发现了这一点,并想贡献我对两个数字的想法。
I found this while searching a similar problem and wanted to contribute what I came up with for two numbers.
如果您查看此页面,您可以看到一个相当简单的您可以使用的算法。 :-)
我并不是说它很高效或者什么,但它在概念上确实可以扩展到多个数字。您只需要空间来跟踪原始数字和您操作的克隆集,直到找到 LCM。
If you look at this page, you can see a fairly simple algorithm you could use. :-)
I'm not saying it's efficient or anything, mind, but it does conceptually scale to multiple numbers. You only need space for keeping track of your original numbers and a cloned set that you manipulate until you find the LCM.