A Factorial is a way to find the total number of possible ways to arrange a certain number of things. Factorial is calculated by multiplying all the positive whole numbers together up to the selected number.
Factorial is commonly used in mathematics and probability to calculate permutations, combinations, and the probability of events. For example, if you have 5 different books and you want to arrange them on a shelf, the number of possible arrangements is 100! or 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000.
Today we are going to learn to write code to obtain the Factorial of a number in Python using an in-built function.
What is the In-built Factorial function?
Python has an inventory of various inbuilt mathematical functions. One such function is ‘factorial()’ which calculates the factorial of any given number. The function is present under the ‘math’ module within Python. We will obtain the Factorial of a Number in Python Using Function.
Python Program to Find the Factorial of a Number Using Function
# Factorial of a Number in Python Using In-built Function # import python math library import math num = 100 print("Factorial of", num, "is", math.factorial(num))
As mentioned below, a simple Python program to find the Factorial of a Number Using an in-built Function ‘factorial()’ to write our code.
- Import the Python math module
- We have to import the math module in order to use the factorial function. The math module allows the end user to perform various mathematical functions. One such function is to calculate the factorial of a number.
- Defining the value of the variable
- To define the value of the variable is to assign a numerical value to the argument. We define this by num and enter the number whose factorial we want to determine.
- Calculating the Factorial
- We use the factorial() function and feed the num (the variable number) as an argument, which gives us the factorial of num.
- Obtaining the result
- Finally, we obtain the result by the print statement: print(“Factorial of”, num, “is”, math.factorial(num)). Which gives us the result.
The output obtained as mentioned in the screenshot above.
Factorial of 100 is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The code outputs the factorial of a Number using the in-built Function.
The variable in the example is defined by ‘num’ with the value 100. Which reads ‘Factorial of 100 is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000’.
Advantages of using the factorial function in Python:
- Convenience: The factorial function is built-in and readily available in Python’s ‘math’ module, therefore the programmer does not have to define a logic for the process.
- Accuracy: The factorial function is implemented using an accurate algorithm that can handle very large numbers with precision, unlike some naive implementations that may produce inaccurate results or overflow errors for large inputs.
- Speed: The factorial function is optimized for performance, and it can calculate factorials much faster than some other algorithms that use loops or recursion.
Disadvantages of using the factorial function in Python:
- Memory consumption: Calculating the factorial of very large numbers can consume a lot of memory, and it may cause memory errors or performance issues if you’re working with limited resources or large datasets.
- Limited precision for floating point inputs: The factorial function can handle floating point inputs, but it may round off errors for very large inputs, due to its memory-heavy operation.
In this tutorial, we learned a simple Python program to find the Factorial of a Number Using an in-built Function. We also discussed the advantages and disadvantages of using the in-built function. The function ‘factorial()’ is extremely useful to obtain factorials of a number fast with minimal syntax.