Reference · Math
math.factorial
math.factorial(int) -> intWhat 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)))1 1 1202432902008176640000120Edge cases
21!does not fit in an int: it is a runtime error,integer overflow, not a float and not a wrapped value, in the browser as on the server, where20!is exact too, past 2^53. For the size of a large factorial use math.lgamma (lgamma(n + 1)islog(n!)), or math.gamma up to170!as a float.- A float that is a whole number is accepted:
math.factorial(5.0)is120. A float with a fraction is not. math.combandmath.permcount without computing the full factorials, so they reach further thann!itself does.
print(math.factorial(5.0), math.gamma(21), math.lgamma(21))print(math.comb(30, 15))120 2432902008176640000 42.335616460753485155117520print(math.factorial(21))line 1: integer overflowErrors
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.
print(math.factorial(-1))line 1: `math.factorial` is not defined for negative numbers (-1)print(math.factorial(5.5))line 1: `math.factorial` expects an int as argument 1, got floatChaining
In a pipeline the number comes first:
print(5 | math.factorial)print(range(0, 6) | map(math.factorial))120[1, 1, 2, 6, 24, 120]See also
math.gamma, math.lgamma, math.comb, math.perm, math.prod
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