Greatest common divisor and least common multiple

gcd(a, b, extended = FALSE)
Lcm(a, b)

Arguments

a, b

vectors of integers.

extended

logical; if TRUE the extended Euclidean algorithm will be applied.

Details

Computation based on the extended Euclidean algorithm.

If both a and b are vectors of the same length, the greatest common divisor/lowest common multiple will be computed elementwise. If one is a vektor, the other a scalar, the scalar will be replicated to the same length.

Value

A numeric (integer) value or vector of integers. Or a list of three vectors named c, d, g, g containing the greatest common divisors, such that

g = c * a + d * b.

Note

The following relation is always true:

n * m = gcd(n, m) * lcm(n, m)

See also

numbers::extGCD

Examples

gcd(12, 1:24)
#>  [1]  1  2  3  4  1  6  1  4  3  2  1 12  1  2  3  4  1  6  1  4  3  2  1 12
gcd(46368, 75025)  # Fibonacci numbers are relatively prime to each other
#> [1] 1
Lcm(12, 1:24)
#>  [1]  12  12  12  12  60  12  84  24  36  60 132  12 156  84  60  48 204  36 228
#> [20]  60  84 132 276  24
Lcm(46368, 75025)  # = 46368 * 75025
#> [1] 3478759200