gcd

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

  1. T gcd(T a, T b)
    T
    gcd
    (
    T
    )
    (
    T a
    ,
    T b
    )
  2. T gcd(T a, T b)

Parameters

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.

Return Value

Type: T

The greatest common divisor of the given arguments.

Examples

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

Meta

Suggestion Box / Bug Report