10,964 Pages

The subfactorial or left factorial, written $$!n$$, is the number of ways that n objects can be arranged where no object appears in its natural position (known as "derangements.") There are many formulas for $$!n$$:

\begin{eqnarray*} !n &=& n! \displaystyle\sum^{n}_{i = 0} \frac{(-1)^i}{i!}\\ &=& \displaystyle\sum^{n}_{i = 0} i! (-1)^{n - i} \binom{n}{i}\\ &=& \displaystyle\frac{\Gamma(n + 1, -1)}{e}\\ &=& \left[\frac{n!}{e}\right] \text{ (only for $n > 0$)} \end{eqnarray*}

In the last formula, [n] means the nearest integer to n. (It is a direct consequence of the first formula — the summation converges to $$1/e$$.)

The first few values of !n for n = 0, 1, 2, 3, 4, 5, etc. are 1, 0, 1, 2, 9, 44, 265, 1,854, 14,833, ...

In base 10, only one number is equal to the sum of the subfactorials of its digits: 148,349 = !1 + !4 + !8 + !3 + !4 + !9 = 109 × 1,361.

## Pseudocode

// Standard factorial function
function factorial(z):
result := 1
for i from 1 to z:
result := result * i
return result

// Subfactorial
function subfactorial(z):
return floor(factorial(z) / e + 0.5)