Subfactorial

Version 1.0.0.0 (1.71 KB) by Yash
Calculate subfactorial of integers up to 170.
452 Downloads
Updated 26 Jun 2009

View License

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
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Logical in Help Center and MATLAB Answers
Version Published Release Notes
1.0.0.0