R/number-of-partitions.R

Defines functions n_partitions

Documented in n_partitions

#' Number of equal sized partitions
#'
#' @param K How many partitions
#' @param N How many elements
#'
#' @return The number of partitions
#'
#' @export
#' 
#' @examples
#' n_partitions(20, 2)
#'
n_partitions <- function(N, K) {
  if (N %% K != 0) {
    stop("N = ", N, " cannot be evenly split into K = ", K, " sets.")
  }
  x <- rep(N / K, K)
  set_size <- c(sum(x), sum(x) - cumsum(x))
  result   <- 1
  for (i in 1:(length(x)-1)) {
    result <- result * choose(set_size[i], x[i])
  }
  result / factorial(K)
}
m-Py/anticlust documentation built on April 13, 2025, 11:17 p.m.