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)Wikipedia
DerangementSubfactorialMathWorldSubfactorialWikidataQ2361661OEISA000166Wolfram LanguageSubfactorial✓Subfactorial(n)the number of derangements of n objects.SageMath
subfactorial(n)SymPysubfactorial(n)Juliaenumeratio_subfactorial(n)Typed signatures
| Type | Package | Applies |
|---|---|---|
(integer) -> integer | compute-engine | always |
(any) -> any | number-theoryreplaces compute-engine | always |
Details
- Closed form , so is n! divided by e and rounded to the nearest integer.
- Recurrence: , with .
- Also satisfies the two-term recurrence .
- The classic hat-check problem: 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 , complex-valued;
N(x, d)past a double's digits sums with (DLMF 8.7.1).
Examples
Applications #
The hat-check problem: the number of ways 6 people can have their hats returned so nobody gets their own
live ↗InOut
Properties #
Only the swap is a derangement of two items
live ↗InOut
Known value · Wolfram MathematicalFunctionData, Subfactorial
Two 3-cycles are the derangements
live ↗InOut
Known value · Wolfram MathematicalFunctionData, Subfactorial
About
| Examples | 26 |
|---|---|
| Checked against | Wolfram Language 24 agree2 unevaluatedSageMath 15 agree2 unevaluated6 errorSymPy 18 agree4 differ2 unevaluatedmpmath 18 agree1 differmathlib4 9 agree7 errorJulia 18 agree1 error |
| Crosswalk | Wikipedia ×2MathWorldWikidataOEISWolfram LanguageSageMathSymPyJulia |
Implementation
mapped
sympypackages/oracle/src/mappings.tssubfactorial(n) is the number of derangements, !n.
mapped
mpmathpackages/oracle/src/mappings.tsΓ(n + 1, −1)/e, Wolfram's closed form.
mapped
sagepackages/oracle/src/mappings.tsmapped
juliapackages/oracle/src/mappings.tsmapped
mathlib4packages/oracle/src/mappings.tsDerangements.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