permutations

permutations(n)

permutations(n, k)

Try it yourself:


permutations(): count ordered selections

permutations(n, k) counts ways to choose and arrange k distinct items from n. Order matters: choosing A then B differs from B then A.

Formula

The count is n!/(n-k)!. For example, choosing first and second place from five entrants produces permutations(5, 2) = 20.

Compare counting methods

Use combinations() when order does not matter, and combinationsWithRep() when repeats are allowed. factorial() supplies the underlying formula, while catalan() counts a different family of structures.

Input limits

n and k should be non-negative integers with k no greater than n. Counts grow quickly, so keep exact results when possible.

All functions