Wednesday, July 3, 2024
HomeLanguagesPythonPython | 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

Ted Musemwa
As a software developer Iā€™m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments