Skip to main content

Mojo function

perm

perm(n: Int, k: Int = -1) -> Int

Computes the number of ways to arrange k items from n items without repetition (permutations).

Equivalent to Python's math.perm(n, k).

Examples:

from std.math import perm
print(perm(5, 2))  # 20
print(perm(5))     # 120  (same as factorial(5))
print(perm(5, 0))  # 1

Args:

  • n (Int): The total number of items. Must be non-negative.
  • k (Int): The number of items to arrange. Must be non-negative and at most n. If omitted (default), returns n! via factorial(n).

Returns:

Int: The number of permutations P(n, k) = n! / (n-k)!. Asserts if n is negative, k is negative, or k > n.

Was this page helpful?