IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b1
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

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.