gcd {cheapr} | R Documentation |
Greatest common divisor and smallest common multiple
Description
Fast greatest common divisor and smallest common multiple using the Euclidean algorithm.
gcd()
returns the greatest common divisor.
scm()
returns the smallest common multiple.
gcd2()
is a vectorised binary version of gcd
.
scm2()
is a vectorised binary version of scm
.
Usage
gcd(
x,
tol = sqrt(.Machine$double.eps),
na_rm = TRUE,
round = TRUE,
break_early = TRUE
)
scm(x, tol = sqrt(.Machine$double.eps), na_rm = TRUE)
gcd2(x, y, tol = sqrt(.Machine$double.eps), na_rm = TRUE)
scm2(x, y, tol = sqrt(.Machine$double.eps), na_rm = TRUE)
Arguments
x |
A numeric vector. |
tol |
Tolerance. This must be a single positive number strictly less than 1. |
na_rm |
If |
round |
If |
break_early |
This is experimental and
applies only to floating-point numbers.
When |
y |
A numeric vector. |
Details
Method
GCD
The GCD is calculated using a binary function that takes input
GCD(gcd, x[i + 1])
where the output of this function is passed as input
back into the same function iteratively along the length of x
.
The first gcd value is x[1]
.
Zeroes are handled in the following way:
GCD(0, 0) = 0
GCD(a, 0) = a
This has the nice property that zeroes are essentially ignored.
SCM
This is calculated using the GCD and the formula is:
SCM(x, y) = (abs(x) / GCD(x, y) ) * abs(y)
If you want to calculate the gcd & lcm for 2 values
or across 2 vectors of values, use gcd2
and scm2
.
Value
A number representing the GCD or SCM.
Examples
library(cheapr)
library(bench)
# Binary versions
gcd2(15, 25)
gcd2(15, seq(5, 25, 5))
scm2(15, seq(5, 25, 5))
scm2(15, 25)
# GCD across a vector
gcd(c(0, 5, 25))
mark(gcd(c(0, 5, 25)))
x <- rnorm(10^5)
gcd(x)
gcd(x, round = FALSE)
mark(gcd(x))