In C++17, std::gcd() returns the greatest common divisor of two integers. This is the biggest whole number that divides both of them without a remainder. For example, the gcd of 12 and 18 is 6.
Before C++17 you had to write the Euclid loop yourself. Now one call does it. It is used to simplify fractions, to find the repeat period of two cycles, and to build std::lcm(). 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::gcd(...), not with a dot.
Syntax
Give two integers. The result has their common type.
C++
auto g = std::gcd(a, b);
int g1 = std::gcd(12, 18); // 6
Call
What it does
gcd(a, b)
Returns the greatest common divisor of |a| and |b|. The result is never negative.
If one number is 0, the answer is the absolute value of the other. gcd(0, 0) is 0.
Library declarations
C++
template<class M, class N>
constexpr std::common_type_t<M, N> gcd(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 greatest common divisor, with the common type of the two arguments. It is 0 only if both arguments are 0.
C++ Examples
Example 1 — GCD of Two Numbers
A few small calls: a normal pair, a zero, a negative number, and two numbers that share nothing.
12 and 18 are both divisible by 1, 2, 3 and 6. The biggest of these is 6.
When one number is 0, every number divides it, so the answer is the other number. The sign is ignored, so -12 behaves like 12.
7 and 13 are both prime and different, so they only share 1. Numbers with gcd 1 are called coprime.
Example 2 — GCD of Many Numbers with accumulate()
gcd() takes two numbers. To use it on a whole vector, fold it with accumulate().
C++
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {12, 18, 30};
int g = std::accumulate(v.begin(), v.end(), 0,
[](int a, int b) { return std::gcd(a, b); });
std::cout << "gcd of all = " << g << '\n';
return 0;
}
Output
gcd of all = 6
How It Works
std::gcd is a template, so it is wrapped in a small lambda that accumulate() can call.
The start value is 0, which works well because gcd(0, x) is x. The steps are gcd(0,12) = 12, then gcd(12,18) = 6, then gcd(6,30) = 6.
The final value 6 divides all three numbers, so it is the gcd of the whole vector.
Example 3 — Simplify a Fraction
Divide the top and bottom of a fraction by their gcd to reduce it to the lowest terms.
C++
#include <iostream>
#include <numeric>
int main() {
int top = 18;
int bottom = 24;
int g = std::gcd(top, bottom);
std::cout << top << "/" << bottom << " = "
<< top / g << "/" << bottom / g << '\n';
return 0;
}
Output
18/24 = 3/4
How It Works
The fraction is 18/24. We want the biggest number that divides both 18 and 24.
std::gcd(18, 24) is 6. Dividing the top and the bottom by 6 keeps the value of the fraction the same.
18/6 = 3 and 24/6 = 4, so the fraction in lowest terms is 3/4.
Frequently Asked Questions
std::gcd(a, b) returns the greatest common divisor of two integers: the biggest number that divides both without a remainder. It needs <numeric> and C++17.
std::gcd(0, n) returns the absolute value of n, because every number divides 0. std::gcd(0, 0) returns 0.
Yes. The signs are ignored and the result is never negative. For example std::gcd(-12, 18) returns 6.
Fold gcd() over the numbers. With a vector, call std::accumulate(v.begin(), v.end(), 0, [](int a, int b) { return std::gcd(a, b); }).
No. gcd() accepts integer types only. A call with a double or a bool does not compile, so cast to an integer type first if that makes sense for your data.