Learn R Programming

primes (version 1.1.0)

gcd: Find the Greatest Common Divisor, Smallest Common Multiple, or Coprimality

Description

These functions provide vectorized computations for the greatest common divisor (gcd), smallest common multiple (scm), and coprimality. Coprime numbers are also called mutually prime or relatively prime numbers. The smallest common multiple is often called the least common multiple.

Usage

gcd(m, n)

scm(m, n)

coprime(m, n)

Value

A vector of the length of longest input vector. If one vector is shorter, it will be recycled. The gcd and scm functions return an integer vector while coprime returns a logical vector.

Arguments

m, n

integer vectors.

Author

Paul Egeler, MS

Details

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\).

Examples

Run this code
gcd(c(18, 22, 49, 13), 42)
## [1] 6 2 7 1

scm(60, 90)
## [1] 180

coprime(60, c(77, 90))
## [1]  TRUE FALSE

Run the code above in your browser using DataLab