Skip to content

Subfactorial ​

The number of derangements of n objects: permutations that leave no element in its original position.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaDerangementSubfactorialMathWorldSubfactorialWikidataQ2361661OEISA000166Wolfram LanguageSubfactorial✓
Subfactorial(n)the number of derangements DnD_n of n objects.
SageMathsubfactorial(n)SymPysubfactorial(n)Juliaenumeratio_subfactorial(n)
Typed signatures
TypePackageApplies
(integer) -> integercompute-enginealways
(any) -> anynumber-theoryreplaces compute-enginealways

Domain: Combinatorics

Details
  • Closed form Dn=n!∑k=0n(−1)kk!D_n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}, so DnD_n is n! divided by e and rounded to the nearest integer.
  • Recurrence: Dn=n Dn−1+(−1)nD_n = n\,D_{n-1} + (-1)^n, with D0=1D_0 = 1.
  • Also satisfies the two-term recurrence Dn+1=n (Dn+Dn−1)D_{n+1} = n\,(D_n + D_{n-1}).
  • The classic hat-check problem: DnD_n counts the ways n people can have their hats returned so nobody gets their own.
  • compute-engine leaves negative arguments unevaluated rather than extending Subfactorial analytically.
  • A real, non-integer n continues as Dn=Γ(n+1,−1)/eD_n = \Gamma(n+1, -1)/e, complex-valued; N(x, d) past a double's digits sums Γ(a)−eiπa∑k≥01/(k! (a+k))\Gamma(a) - e^{i\pi a}\sum_{k\ge 0} 1/(k!\,(a+k)) with a=n+1a = n+1 (DLMF 8.7.1).

Examples

Basic #
live ↗
InSubfactorial(0)\mathrm{Subfactorial}(0)Out11

A single object can't be deranged

live ↗
InSubfactorial(1)\mathrm{Subfactorial}(1)Out00
live ↗
InSubfactorial(4)\mathrm{Subfactorial}(4)Out99
live ↗
InSubfactorial(5)\mathrm{Subfactorial}(5)Out4444
live ↗
InSubfactorial(20)\mathrm{Subfactorial}(20)Out895 014 631 192 902 121895\,014\,631\,192\,902\,121
live ↗
InFullSimplify(Normal⁡(Series⁡(Subfactorial(x),x,0,2)))\mathrm{FullSimplify}(\operatorname{Normal}(\operatorname{Series}(\mathrm{Subfactorial}(x), x, 0, 2)))Out12Subfactorial′′(0)x2+xSubfactorial′(0)+1\frac{1}{2}\mathrm{Subfactorial}^{\prime\prime}(0)x^2+x\mathrm{Subfactorial}^{\prime}(0)+1
live ↗
InFullSimplify(Normal⁡(Series⁡(Subfactorial(x),x,∞,2)))\mathrm{FullSimplify}(\operatorname{Normal}(\operatorname{Series}(\mathrm{Subfactorial}(x), x, ∞, 2)))OutSeries⁡(Subfactorial(x),x,∞,2)\operatorname{Series}(\mathrm{Subfactorial}(x), x, ∞, 2)
Scope #

Threads element-wise over a list, as Wolfram's Listable heads do

live ↗
InSubfactorial([1,2,3,4])\mathrm{Subfactorial}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out[0,1,2,9]\bigl\lbrack0, 1, 2, 9\bigr\rbrack

Exact for large n

live ↗
InSubfactorial(48)\mathrm{Subfactorial}(48)Out4 566 824 330 931 624 695 767 452 273 778 667 071 025 042 534 230 906 772 538 9134\,566\,824\,330\,931\,624\,695\,767\,452\,273\,778\,667\,071\,025\,042\,534\,230\,906\,772\,538\,913

Real arguments evaluate as Γ(n+1,−1)e\frac{\Gamma(n+1, -1)}{e} (complex off the integers)

live ↗
InSubfactorial(4.5)\mathrm{Subfactorial}(4.5)Out19.255 831 840 742 534+0.157 179 122 964 615 25i19.255\,831\,840\,742\,534+0.157\,179\,122\,964\,615\,25i

Complex arguments evaluate as Γ(n+1,−1)e\frac{\Gamma(n+1, -1)}{e} too

live ↗
InSubfactorial(1.6+i)\mathrm{Subfactorial}(1.6+i)Out0.296 830 458 177 711 8+0.284 208 547 251 969 9i0.296\,830\,458\,177\,711\,8+0.284\,208\,547\,251\,969\,9i

Threads over a matrix element-wise

live ↗
InSubfactorial([[2,0],[0,2]])\mathrm{Subfactorial}(\bigl\lbrack\bigl\lbrack2, 0\bigr\rbrack, \bigl\lbrack0, 2\bigr\rbrack\bigr\rbrack)Out[[1,1],[1,1]]\bigl\lbrack\bigl\lbrack1, 1\bigr\rbrack, \bigl\lbrack1, 1\bigr\rbrack\bigr\rbrack

Threads over a list; exact derangement counts up to 2020

live ↗
InSubfactorial([5,10,20])\mathrm{Subfactorial}(\bigl\lbrack5, 10, 20\bigr\rbrack)Out[44,1 334 961,895 014 631 192 902 121]\bigl\lbrack44, 1\,334\,961, 895\,014\,631\,192\,902\,121\bigr\rbrack

On a scalar matrix 2I2I the matrix function gives !2⋅I!2\cdot I

live ↗
InMatrixFunction(_1⁡↦Subfactorial(_1⁡),[[2,0],[0,2]])\mathrm{MatrixFunction}(\operatorname{\_1}↦\mathrm{Subfactorial}(\operatorname{\_1}), \bigl\lbrack\bigl\lbrack2, 0\bigr\rbrack, \bigl\lbrack0, 2\bigr\rbrack\bigr\rbrack)Out[[1,0],[0,1]]\bigl\lbrack\bigl\lbrack1, 0\bigr\rbrack, \bigl\lbrack0, 1\bigr\rbrack\bigr\rbrack
live ↗
InN(Subfactorial(13),50)N(\mathrm{Subfactorial}(\frac{1}{3}), 50)Out0.581 679 279 998 131 546 308 233 168 239 509 695 786 964 644 882 07+0.438 504 127 935 786 850 035 306 159 942 752 654 110 750 012 538 63i0.581\,679\,279\,998\,131\,546\,308\,233\,168\,239\,509\,695\,786\,964\,644\,882\,07+0.438\,504\,127\,935\,786\,850\,035\,306\,159\,942\,752\,654\,110\,750\,012\,538\,63i
live ↗
InFullSimplify(Normal⁡(Series⁡(Subfactorial(x),x,∞,1)))\mathrm{FullSimplify}(\operatorname{Normal}(\operatorname{Series}(\mathrm{Subfactorial}(x), x, ∞, 1)))OutSeries⁡(Subfactorial(x),x,∞,1)\operatorname{Series}(\mathrm{Subfactorial}(x), x, ∞, 1)
live ↗
InFullSimplify(Normal⁡(Series⁡(Subfactorial(x),x,x0,2)))\mathrm{FullSimplify}(\operatorname{Normal}(\operatorname{Series}(\mathrm{Subfactorial}(x), x, \mathrm{x0}, 2)))Out12Subfactorial′′(x0)(x−x0)2+(x−x0)Subfactorial′(x0)+Subfactorial(x0)\frac{1}{2}\mathrm{Subfactorial}^{\prime\prime}(\mathrm{x0})(x-\mathrm{x0})^2+(x-\mathrm{x0})\mathrm{Subfactorial}^{\prime}(\mathrm{x0})+\mathrm{Subfactorial}(\mathrm{x0})
Applications #

The hat-check problem: the number of ways 6 people can have their hats returned so nobody gets their own

live ↗
InSubfactorial(6)\mathrm{Subfactorial}(6)Out265265
Properties #

Recurrence: Dn=nDn−1+(−1)nD_n = n D_{n-1} + (-1)^n

live ↗
InSubfactorial(4)=4Subfactorial(3)+(−1)4\mathrm{Subfactorial}(4)=4\mathrm{Subfactorial}(3)+(-1)^4OutTrue\mathrm{True}

DnD_n is n!e\frac{n!}{e} rounded to the nearest integer

live ↗
InSubfactorial(10)=round(10!e)\mathrm{Subfactorial}(10)=\mathrm{round}(\frac{10!}{e})OutTrue\mathrm{True}

Two-term recurrence: Dn+1=n (Dn+Dn−1)D_{n+1} = n\,(D_n + D_{n-1})

live ↗
InSubfactorial(6)=5(Subfactorial(5)+Subfactorial(4))\mathrm{Subfactorial}(6)=5(\mathrm{Subfactorial}(5)+\mathrm{Subfactorial}(4))OutTrue\mathrm{True}

Exponential generating function e−x1−x\frac{e^{-x}}{1-x}

live ↗
InExponentialGeneratingFunction(Subfactorial(n),n,x)\mathrm{ExponentialGeneratingFunction}(\mathrm{Subfactorial}(n), n, x)Oute−x1−x\frac{e^{-x}}{1-x}

Only the swap is a derangement of two items

live ↗
InSubfactorial(2)\mathrm{Subfactorial}(2)Out11

Known value · Wolfram MathematicalFunctionData, Subfactorial

Two 3-cycles are the derangements

live ↗
InSubfactorial(3)\mathrm{Subfactorial}(3)Out22

Known value · Wolfram MathematicalFunctionData, Subfactorial

Possible issues #

A negative argument is left unevaluated

live ↗
InSubfactorial(−1)\mathrm{Subfactorial}(-1)OutSubfactorial(−1)\mathrm{Subfactorial}(-1)
Neat examples #

Digit-wise derangement sums spell out the number

live ↗
In148 349=∑Subfactorial([1,4,8,3,4,9])148\,349=\sum\mathrm{Subfactorial}(\bigl\lbrack1, 4, 8, 3, 4, 9\bigr\rbrack)OutTrue\mathrm{True}
About
Examples26
Checked againstWolfram Language 24 agree2 unevaluatedSageMath 15 agree2 unevaluated6 errorSymPy 18 agree4 differ2 unevaluatedmpmath 18 agree1 differmathlib4 9 agree7 errorJulia 18 agree1 error
CrosswalkWikipedia ×2MathWorldWikidataOEISWolfram LanguageSageMathSymPyJulia

Implementation

mappedsympypackages/oracle/src/mappings.ts

subfactorial(n) is the number of derangements, !n.

mappedmpmathpackages/oracle/src/mappings.ts

Γ(n + 1, −1)/e, Wolfram's closed form.

mappedsagepackages/oracle/src/mappings.ts
mappedjuliapackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts

Derangements.numDerangements, the same a(0) = 1, a(1) = 0 recurrence; natural numbers only, so a negative or non-integer n fails to typecheck rather than taking Wolfram's Gamma closed form.

See also: Factorial