Reference · Math

math.factorial

signature
math.factorial(int) -> int

What it does

n!, the product of the ints from 1 to n, as an int, as Python's math.factorial. math.factorial(0) is 1. Quillo's ints are 64 bits and there is no bigint, so the largest factorial that fits is 20!; Python's grows without bound.

Examples

print(math.factorial(0), math.factorial(1), math.factorial(5))print(math.factorial(20))print(math.factorial(10) / (math.factorial(3) * math.factorial(7)))
output
1 1 1202432902008176640000120

Edge cases

print(math.factorial(5.0), math.gamma(21), math.lgamma(21))print(math.comb(30, 15))
output
120 2432902008176640000 42.335616460753485155117520
this one fails
print(math.factorial(21))
output
line 1: integer overflow

Errors

A negative number is a runtime error, not an error value, with a message that says so; Python raises ValueError there too. A fraction, text or null is a runtime error as well.

this one fails
print(math.factorial(-1))
output
line 1: `math.factorial` is not defined for negative numbers (-1)
this one fails
print(math.factorial(5.5))
output
line 1: `math.factorial` expects an int as argument 1, got float

Chaining

In a pipeline the number comes first:

print(5 | math.factorial)print(range(0, 6) | map(math.factorial))
output
120[1, 1, 2, 6, 24, 120]

See also

math.gamma, math.lgamma, math.comb, math.perm, math.prod

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

Thread 0 posts

On math.factorial(): 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