gcd: Greatest Common Divisor and Least Common Multiple

gcdR Documentation

Greatest Common Divisor and Least Common Multiple

Description

gcd(a,b) computes the greatest common divisor of two positive integer numbers by Euclid's algorithm.

lcm(...) computes the least common multiple of an arbitrary number of integers, iteratively applying lcm(a,b) = (a * b) / gcd(a,b).

Usage

GCD(a, b)
LCM(n, ...)

Arguments

a, b

two integer numbers.

n, ...

an integer (vector or number) and possibly more; the ... argument is for convenience, allowing e.g., LCM(2,3,4).

Value

a positive integer.

Note

Very simple, but too useful to spend time on, if you need it.

Author(s)

Martin Maechler

See Also

primes, and factorize.

Examples

GCD(12, 18)
GCD(15, 105)
GCD(84, 64)

LCM(1,2,3,4,5,6) # 60
LCM(2,3,5,7) == print(2*3*5*7) # true, of course
LCM(1:8) # 840

## the LCMs needed to get integer coefficients / N  in Taylor polynomial for log(1+x):
vapply(1:24, function(n) LCM(1:n), 1)

sfsmisc documentation built on Nov. 21, 2025, 9:06 a.m.