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)
}

Try the fracture package in your browser

Any scripts or data that you put into this service are public.

fracture documentation built on May 21, 2022, 9:05 a.m.