gcd: Greatest Common Divisor and Least Common Multiple

Description Usage Arguments Details Value Examples

Description

gcd and lcm return the greatest common divisor and least common multiple of all the values present in its arguments.

pgcd and plcm return the parallel greatest common divisor and parallel least common multiple of the input values. They take any number of vectors as arguments, recycle them to common length and return a single vector.

Usage

1
2
3
4
5
6
7
8
gcd(..., na.rm = FALSE)
pgcd(..., na.rm = FALSE)

lcm(..., na.rm = FALSE)
plcm(..., na.rm = FALSE)

coprime(...)
pcoprime(...)

Arguments

...

integer arguments. NULL is accepted as equivalent to integer(0).

na.rm

a logical indicating whether missing values should be removed.

Details

The greatest common divisor d of two integers x and y (at least one of which is non-zero) is the greatest number for which x/d and y/d are both integers. When both x and y are zero, the greatest common divisor is also zero.

The least common multiple m of two non-zero integers x and y is the least number for which m/x and m/y are both integers. When one or both of x and y are zero, the least common multiple is also zero.

If x or y is NA, the greatest common divisor and least common multiple are also NA.

It is guaranteed that d * m == abs(x * y)

x is coprime with y if their greatest common divisor is 1.

Value

For gcd and lcm an integer vector of length 1. For coprime a logical vector of length 1.

For pgcd and plcm an integer vector. For pcoprime a logical vector. If any of the input values is a zero-length vector the result has length zero. Otherwise, the result has length equal to the length of the longest vector. The rules for determining the attributes of the result are rather complicated. Attributes are only copied from input values whose lengths are equal to the length of the result. If any such input values have a dim attribute, the first dim attribute is copied to the result. dimnames are copied in a similar manner (but only after the result has a dim attribute). If any such input values have a conformable dimnames attribute, the first conformable dimnames attribute is copied to the result. If a dim attribute has not been assigned to the result, then finally names are copied in a similar manner. If any such input values have a names attribute, the first names attribute is copied to the result. A result can have a dim attribute, a names attribute, neither, but cannot have both. dim has priority over names (similar to Arithmetic operators).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
y <- 105
x <- -y:y
d <- pgcd(x, y)
m <- plcm(x, y)


graphics::plot(
    x = x, y = d,
    ylab = substitute("Greatest Common Divisor of x and " ~ y, list(y = y))
)
graphics::plot(
    x = x, y = m,
    ylab = substitute("Least Common Multiple of x and " ~ y, list(y = y))
)
d * m == abs(x * y)


pcoprime(x, 105)

polynomial documentation built on Feb. 22, 2021, 5:08 p.m.