View source code
Display the source code in std/numeric.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.numeric.gcd

Computes the greatest common divisor of a and b by using an efficient algorithm such as Euclid's or Stein's algorithm.

typeof(Unqual!T.init%Unqual!U.init) gcd(T, U) (
  T a,
  U b
if (isIntegral!T && isIntegral!U);

auto gcd(T) (
  T a,
  T b
if (!isIntegral!T && is(typeof(T.init % T.init)) && is(typeof(T.init == 0 || T.init > 0)));


a Integer value of any numerical type that supports the modulo operator %. If bit-shifting << and >> are also supported, Stein's algorithm will be used; otherwise, Euclid's algorithm is used as a fallback.
b Integer value of any equivalent numerical type.


The greatest common divisor of the given arguments.


writeln(gcd(2 * 5 * 7 * 7, 5 * 7 * 11)); // 5 * 7
const int a = 5 * 13 * 23 * 23, b = 13 * 59;
writeln(gcd(a, b)); // 13


Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko


Boost License 1.0.