Reference · Math

math.perm

signature
math.perm(int, int | null=) -> int

What it does

The number of ways to pick k items from n in order, n! / (n - k)!, as an int, as Python's math.perm: math.perm(5, 2) is 20. Without k, or with k of null, it is n!, all the orderings of n items.

Examples

print(math.perm(5, 2), math.perm(5), math.perm(5, null))print(math.perm(10, 3) == math.comb(10, 3) * math.factorial(3))
output
20 120 120true

Edge cases

print(math.perm(5, 6), math.perm(5, 0), math.perm(0), math.perm(0, 0))print(math.perm(20), math.perm(100, 2), math.perm(6.0, 2))
output
0 1 1 12432902008176640000 9900 30
this one fails
print(math.perm(21))
output
line 1: integer overflow

Errors

A negative n or k is a runtime error, not an error value, with a message that says so, as Python's ValueError. A fraction, text or null for n is a runtime error as well.

this one fails
print(math.perm(5, -1))
output
line 1: `math.perm` is not defined for negative numbers (-1)
this one fails
print(math.perm("5"))
output
line 1: `math.perm` expects an int as argument 1, got str

Chaining

In a pipeline n comes first, so n | math.perm(k):

print(10 | math.perm(3))print(range(1, 6) | map(math.perm))
output
720[1, 2, 6, 24, 120]

See also

math.comb, math.factorial, math.prod

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On math.perm(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo