View source code
Display the source code in std/numeric.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla 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 usinglocal 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)));
Parameters
Name | Description |
---|---|
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. |
Returns
The greatest common divisor of the given arguments.
Example
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
Authors
Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko
License
Copyright © 1999-2025 by the D Language Foundation | Page generated by ddox.