R/permutations.R

Defines functions permutations

Documented in permutations

permutations <- function(n) {
    if (n == 1) {
        return(matrix(1))
    } else {
        sp <- permutations(n - 1)
        p <- nrow(sp)
        A <- matrix(nrow = n * p, ncol = n)
        for (i in 1:n) {
            A[(i - 1) * p + 1:p,] <- cbind(i, sp + (sp >= i))
        }
        return(A)
    }
}

Try the UNCLES package in your browser

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

UNCLES documentation built on May 2, 2019, 11:11 a.m.