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.

T gcd(T) (
  T a,
  T b
)
if (isIntegral!T);

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

NameDescription
T 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.

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

Boost License 1.0.