The greatest common divisor uses Euclid's algorithm, a fast and widely
used method. The smallest common multiple and coprimality are computed using
the gcd, where \(scm = \frac{a}{gcd(a, b)} \times b\)
and two numbers are coprime when \(gcd = 1\).
The gcd
, scm
, and coprime
functions perform element-wise computation.
The Rgcd
and Rscm
functions perform gcd
and scm
over multiple values
using reduction. That is, they compute the greatest common divisor and least
common multiple for an arbitrary number of integers based on the properties
\(gcd(a_1, a_2, ..., a_n) = gcd(gcd(a_1, a_2, ...), a_n)\) and
\(scm(a_1, a_2, ..., a_n) = scm(scm(a_1, a_2, ...), a_n)\). The binary
operation is applied to two elements; then the result is used as the first
operand in a call with the next element. This is done iteratively until all
elements are used. It is idiomatically equivalent to Reduce(gcd, x)
or
Reduce(scm, x)
, where x
is a vector of integers, but much faster.