Saturday, October 18, 2025
HomeLanguagesPython | sympy.subfactorial() method

Python | sympy.subfactorial() method

With the help of sympy.subfactorial() method, we can find the Subfactorial of a number in SymPy. Subfactorial of a number is given by –

 !n = \begin{cases} 1 & n = 0 \\ 0 & n = 1 \\ (n-1)(!(n-1) + !(n-2)) & n > 1 \end{cases}

Syntax: subfactorial(n)

Parameter:
n – It denotes the number whose subfactorial is to be calculated.

Returns: Returns the subfactorial of number, i. e., n.

Example #1:




# import sympy 
from sympy import * 
  
n = 4
print("Value of n = {}".format(n))
   
# Use sympy.subfactorial() method 
subfactorial_n = subfactorial(n)  
      
print("Subfactorial of n : {}".format(subfactorial_n))  


Output:

Value of n = 4
Subfactorial of n : 9

Example #2:




# import sympy 
from sympy import * 
  
n = 9
print("Value of n = {}".format(n))
   
# Use sympy.subfactorial() method 
subfactorial_n = subfactorial(n)  
      
print("Subfactorial of n : {}".format(subfactorial_n))  


Output:

Value of n = 9
Subfactorial of n : 133496
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS