Python SymPy Module:
SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.
Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.
SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.
Installation:
pip install sympy
Python sympy.factorial() Method:
We can obtain the factorial of any number by using the factorial() method of the sympy module.
Syntax:
sympy.factorial()
Return Value:
The factorial of a given number is returned by the factorial() function.
Examples:
Example1:
Input:
Given Number = 4
Output:
The factorial of a given number { 4 } = 24
Example2:
Input:
Given Number = 6
Output:
The factorial of a given number { 6 } = 720
sympy.factorial() Method in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give the number as static input and store it in a variable.
- Pass the given number as an argument to the factorial() function to get the factorial of a given number.
- Store it in another variable.
- Print the factorial of a given number.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Give the number as static input and store it in a variable. gvn_num = 4 # Pass the given number as an argument to the factorial() function to # get the factorial of a given number. # Store it in another variable. rslt = factorial(gvn_num) # Print the factorial of a given number. print("The factorial of a given number {",gvn_num,"} = ", rslt)
Output:
The factorial of a given number { 4 } = 24
Method #2: Using Built-in Functions (User Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give the number as user input using the int(input()) function and store it in a variable.
- Pass the given number as an argument to the factorial() function to get the factorial of a given number.
- Store it in another variable.
- Print the factorial of a given number.
- The Exit of the Program.
Below is the implementation:
# Import all the functions from sympy module using the import keyword from sympy import * # Give the number as user input using the int(input()) function and # store it in a variable. gvn_num = int(input("Enter some random number = ")) # Pass the given number as an argument to the factorial() function to # get the factorial of a given number. # Store it in another variable. rslt = factorial(gvn_num) # Print the factorial of a given number. print("The factorial of a given number {",gvn_num,"} = ", rslt)
Output:
Enter some random number = 6 The factorial of a given number { 6 } = 720