Usage :
r = subfactorial(n) calculates the subfactorial of a non -negative integer (n) using the inclusion-exclusion principle.
The subfactorial can be written as,
!n = n! sum( (-1)^k / k! ), for k = 0 to n
!n : subfactorial(n)
n! : factorial(n) = 1*2*3*...*n
Remark :
This function is limited by largest floating point number that can be
defined, as given by the realmax. Since,
subfactorial(170) < realmax < subfactorial(171)
Maximum value of input (n) cannot exceed 170.
Higher value of (n) will return NaN.
Example:
a = subfactorial(12);
returns -> a = 176214841
See Also:
factorial
Cite As
Yash (2026). Subfactorial (https://www.mathworks.com/matlabcentral/fileexchange/24566-subfactorial), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.0.0.0 |
