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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))  

chevron_right


Output:

Value of n = 4
Subfactorial of n : 9

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))  

chevron_right


Output:

Value of n = 9
Subfactorial of n : 133496


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.