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)
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- What are the problems associated to Best First Sea
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
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)
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
As of C++17, you can use
std::lcm
.And here is a little program that shows how to specialize it for multiple parameters
Test it here: https://wandbox.org/permlink/25jVinGytpvPaS4v