In C++17, std::lcm() returns the least common multiple of two integers. This is the smallest positive number that both numbers divide evenly. For example, the lcm of 4 and 6 is 12.
It is useful to find a common denominator for fractions, or to find when two repeating events happen together again. It is the partner of std::gcd(), and it takes integer types only. Include <numeric> and use C++17.
This function is declared in the <numeric> header. It needs C++17 (-std=c++17). It is a free function, called as std::lcm(...), not with a dot.
Syntax
Give two integers. The result has their common type.
C++
auto l = std::lcm(a, b);
int l1 = std::lcm(4, 6); // 12
Call
What it does
lcm(a, b)
Returns the smallest positive number that is a multiple of both |a| and |b|.
If either number is 0, the result is 0. Internally the result is based on |a| / gcd(a, b) * |b|.
Library declarations
C++
template<class M, class N>
constexpr std::common_type_t<M, N> lcm(M m, N n);
// Integer types only. bool is not allowed.
Parameters
Parameter
Description
a
Required. The first integer. Negative numbers are allowed.
b
Required. The second integer. Negative numbers are allowed.
Return Value
Return Value
Description
Integer
The least common multiple, with the common type of the two arguments. It is 0 if either argument is 0.
C++ Examples
Example 1 — LCM of Two Numbers
A normal pair, a zero, a negative number, and two coprime numbers.
The multiples of 4 are 4, 8, 12, ... and the multiples of 6 are 6, 12, .... The first one they share is 12.
If one number is 0, the only common multiple is 0, so the result is 0. The sign is ignored, so -4 acts like 4.
7 and 13 are coprime, so their lcm is simply the product 7 * 13 = 91.
Example 2 — LCM of Many Numbers with accumulate()
Find the smallest number that every element of a vector divides.
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {2, 3, 4, 5};
int l = std::accumulate(v.begin(), v.end(), 1,
[](int a, int b) { return std::lcm(a, b); });
std::cout << "lcm of all = " << l << '\n';
return 0;
}
Output
lcm of all = 60
How It Works
A lambda wraps std::lcm so accumulate() can call it with the running value and the next element.
The start value is 1, because lcm(1, x) is x. The steps are lcm(1,2) = 2, lcm(2,3) = 6, lcm(6,4) = 12, and lcm(12,5) = 60.
The number 60 is the smallest number divisible by 2, 3, 4 and 5.
Example 3 — Add Two Fractions with a Common Denominator
Use lcm() to find a shared bottom number, then add the tops.
C++
#include <iostream>
#include <numeric>
int main() {
// 1/6 + 1/4
int d1 = 6, n1 = 1;
int d2 = 4, n2 = 1;
int common = std::lcm(d1, d2);
int top = n1 * (common / d1) + n2 * (common / d2);
std::cout << n1 << "/" << d1 << " + " << n2 << "/" << d2
<< " = " << top << "/" << common << '\n';
return 0;
}
Output
1/6 + 1/4 = 5/12
How It Works
To add 1/6 and 1/4 the bottom numbers must match. std::lcm(6, 4) gives the smallest shared bottom number, 12.
Each top is scaled: 1 * (12/6) = 2 and 1 * (12/4) = 3. In other words, 1/6 = 2/12 and 1/4 = 3/12.
The tops add up to 2 + 3 = 5, so the answer is 5/12. It is already in lowest terms.
Frequently Asked Questions
std::lcm(a, b) returns the least common multiple of two integers: the smallest positive number that both a and b divide evenly. It needs <numeric> and C++17.
gcd() gives the biggest number that divides both values, and lcm() gives the smallest number that both values divide. They are linked: gcd(a, b) * lcm(a, b) equals |a * b|.
It returns 0. There is no positive common multiple with 0, so the standard defines the result as 0.
Yes. The lcm of two ints can be larger than INT_MAX, and then the behaviour is undefined. Use long long arguments when the numbers can be big.
Fold lcm() over the vector with accumulate(), starting from 1: std::accumulate(v.begin(), v.end(), 1, [](int a, int b) { return std::lcm(a, b); }).