Factorial: n!n!

The factorial of a number is specified by an exclamation mark. It is the number of different ways you can order n elements.

It is the product of the first n positive natural numbers, so it is an integer.

n!=123nn! = 1 \cdot 2 \cdot 3 \cdot \dots \cdot n

Using the product symbol as seen in another section we can also write it as:

n!=i=1ni n! = \prod\limits_{i=1}^n i

From this we also get 0!=10! = 1. It is not defined for negative values.

Attention: The factorial increases very rapidly. If you use a 32 bit signed integer, then 13!13! will already overflow. Even for a 64 bit signed integer, 21!21! is too much. So if you see a formula involving factorials, try to cancel out terms as much as possible. For example, if k<nk<n: n!k!=(k+1)(k+2)(n1)n\frac{n!}{k!} = (k+1)(k+2) \cdots (n-1) n

Code: