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
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!

Thanks to your feedback and relevant comments, dCode has developed the best 'Subfactorial' tool, so feel free to write! Thank you!

# Subfactorial

## SubFactorial Calculator !N

### How to calculate a subfactorial?

SubFactorial $n$ is calculated using 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}

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$

And a recurrence relationship : $$!n = n \times !(n-1) + (-1)^n$$

### 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

### 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$

### 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!)$

### How to calculate derangements

Derangements (or Rencontres) are permutations without the one with fixed points (no item is in its original place). The number of derangements for $n$ elements is subfactorial of $n$: $!n$.

Example: 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}.

## Source code

dCode retains ownership of the online 'Subfactorial' tool source code. Except explicit open source licence (indicated CC / Creative Commons / free), any 'Subfactorial' algorithm, applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, translator), or any 'Subfactorial' function (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and no data download, script, copy-paste, or API access for 'Subfactorial' will be for free, same for offline use on PC, tablet, iPhone or Android ! dCode is free and online.

## Need Help ?

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