R/frac_lcm.R

Defines functions frac_gcd frac_lcm

Documented in frac_gcd frac_lcm

#' Least common multiple and greatest common divisor
#'
#' @param ... Integer vectors or vectors that can be coerced to integer.
#' @param max If the least common multiple is greater than `max`, `max` is
#'   returned instead.
#'
#' @return An integer.
#' @export
#'
#' @example examples/frac_lcm.R

frac_lcm <- function(..., max = 1e7) {
  x <- c(list(...), recursive = TRUE)

  if (any(x != as.integer(x))) {
    stop("The least common multiple can only be calculated for integers.")
  }

  lcm(x, max)
}

#' @rdname frac_lcm
#' @export

frac_gcd <- function(...) {
  x <- c(list(...), recursive = TRUE)

  if (any(x != as.integer(x))) {
    stop("The greatest common divisor can only be calculated for integers.")
  }

  gcd(x)
}
rossellhayes/fracture documentation built on May 21, 2022, 11:23 p.m.