Search for a tool
Subfactorial

Tool to compute subfactorial. Subfactorial !n is the number of derangements of n objects, i.e. the number of permutations of n objects in order that no object stands in its original position.

Results

Subfactorial -

Tag(s) : Arithmetics

Share
Share
dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!


Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!


Feedback and suggestions are welcome so that dCode offers the best 'Subfactorial' tool for free! Thank you!

Subfactorial

SubFactorial Calculator !N


Derangements Generator

⮞ Go to: Derangements

Answers to Questions (FAQ)

What is a subfactorial? (Definition)

The subfactorial, denoted $ !n $, is a mathematical function that counts the number of derangements of a set of $ n $ elements.

Derangements are permutations from which the fixed points (i.e., no element is in its original position) are removed.

How to calculate a subfactorial?

The subfactorial of $ n $ is calculated by this formula: $$ !n = n! \sum_{k=0}^n \frac{(-1)^k}{k!} $$

Example: $$ \begin{align} !4 &= 4! ( \frac{(-1)^0}{0!} + \frac{(-1)^1}{1!} + \frac{(-1)^2}{2!} + \frac{(-1)^3}{3!} + \frac{(-1)^4}{4!} ) \\ &= 4! \times ( 1/1 - 1/1 + 1/2 - 1/6 + 1/24 ) \\ &= 24 \times 9/24 \\ &= 9 \end{align} $$

The subfactorial can be defined by recurrence: $$ !n = (n−1) (!(n−1)+!(n−2)) $$ or via another equivalent recurrent relation: $$ !n = n \times !(n-1) + (-1)^n $$

This formula is also used: $$ !n = \left [ \frac {n!}{e} \right ] $$ where brackets [] stands for rounding to the closest integer.

Example: $ 4! / e \approx 24/2.718 \approx 8.829 \Rightarrow !4 = 9 $

How to calculate derangements?

The number of derangements for $ n $ elements is subfactorial of $ n $: $ !n $.

Example: How many ways can the digits in 1234 be rearranged without any digit remaining in its original position? The $ !4 = 9 $ derangements of {1,2,3,4} are {2,1,4,3}, {2,3,4,1}, {2,4,1,3}, {3,1,4,2}, {3,4,1,2}, {3,4,2,1}, {4,1,2,3}, {4,3,1,2}, and {4,3,2,1}.

What are the first values of the subfactorial function?

The first values for the first natural numbers are:

!1 = 0
!2 = 1
!3 = 2
!4 = 9
!5 = 44
!6 = 265
!7 = 1854
!8 = 14833
!9 = 133496
!10 = 1334961
see OEIS here

What are the typical exercises for the subfactorial?

— A secretary places 100 letters in 100 envelopes completely randomly. The probability that no letter is in the correct envelope is given by: $ \frac{100!}{!100} \approx \frac{1}{e} \approx 0.36788 \approx 36.7\% $

— A banquet planner assigns $ n $ guests to $ n $ numbered seats, but the guests ignore their assigned seats and each sits randomly. What is the probability that no guest is seated in their correct seat? $ \frac{n!}{!n} \approx \frac{1}{e} \approx 36.7\% $

— $ k $ people place their hats in a box. Then, each person takes a hat at random. What is the probability that no person retrieves their own hat? $ \frac{k!}{!k} \approx \frac{1}{e} \approx 36.7\% $

How to write a subfactorial?

The subfactorial as the factorial, uses the exclamation mark as symbol but it is written to the left of the number: $ !n $

Sometimes the form $ D_n $ is used in some sources in combinatorics (D for derangements).

What is the precedence of the operator subfactorial (order of operations)?

By convention, postfixed operators have priority (the calculation goes first) over prefixed, so factorial (postfixed) has priority over subfactorial (prefixed)

Example: $ !3! = !(3!) $

What is the relationship between subfactorial and factorial?

The proportion of derangements among all permutations tends to $ \frac{1}{e} $ when $ n $ becomes large, that is: $$ !n \approx n! \times e \\ n! \approx \frac{!n}{e} $$

How to program subfactorial in a programming language?

The subfactorial algorithm is given by the following pseudo-code:
function subfactorial(n) {
memo = [1,0]
for (i = 2 .. n) {
memo[i] = (i - 1) * (memo[i - 1] + memo[i - 2])
}
return memo[n]
}

Time and space complexity: O(n)

If the language already has the factorial function, then it may be faster to use another algorithm.

Example: In Python: from math import factorial
def subfactorial(n):
return round(factorial(n) / 2.71828)

Source code

dCode retains ownership of the "Subfactorial" source code. Any algorithm for the "Subfactorial" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Subfactorial" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Subfactorial" or any other element are not public (except explicit open source licence like Creative Commons). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.

Cite dCode

The content of the page "Subfactorial" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source. Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link: https://www.dcode.fr/subfactorial
In a scientific article or book, the recommended bibliographic citation is: Subfactorial on dCode.fr [online website], retrieved on 2025-04-15, https://www.dcode.fr/subfactorial

Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Questions / Comments

Feedback and suggestions are welcome so that dCode offers the best 'Subfactorial' tool for free! Thank you!


https://www.dcode.fr/subfactorial
© 2025 dCode — The ultimate collection of tools for games, math, and puzzles.
 
Feedback