In this tutorial, we will discuss how to implement factorial in python by using recursion, for loop, while loop, maths library, numpy library. Let’s start this by discussing the maths definition of factorial.
Math definition of factorial :
factorial in maths donated by the symbol (!) for any integer (n) greater than equal to 1 can be defined as a product of all integers less than equal to n but greater than or equal to 1. factorial of zero is always 1 and factorial of negative integers does n’t exist.
n! = n(n-1)(n-2)(n-3)…..321
n! = n*(n-1)!
6! = 6*5*4*3*2*1 = 120
0! = 1
-1! = Does n’t exists
1. Python Program to find factorial In Python using recursion
Let’s write the python program to find factorial using recursion technique. If you have n’t heard of recursion before it’s the technique of solving the problems in computer science where the function calls itself repeatedly. learn more about recursion .
# program to find factorial in python def factorial(number): # IF number is less than or equal to 1 means it's factorial is 1 if number <=1: return 1 # Using formula n = n * (n-1)! return number * factorial(number-1) # Take input from user and convert it to int n = int(input("Enter Number : ")) f = factorial(n) # Find factorial print("Factorial of",n,":",f)
Run the above code you should see the following output.
2. Python program to find factorial in python using for loop
Now we are going to write code to find factorial using forloop of python.
# Function to find factorial def factorial(num): # set the f to 1 f = 1 # loop through 2 to n for i in range(2,num+1): f *= i # Multiply the each number # return the answer to the caller return f # Client code n = int(input("Enter the number : ")) f = factorial(n) print("Factorial of",n,":",f)
Enter the number : 6 Factorial of 6 : 720
3. Find factorial in python using while loop
Let's move forward to find factorial using while loop.
# Function to find factorial def factorial(num): # set the f to 1 f = 1 # Run loop until num is greater than 1 while num > 1: f *= num num -=1 # decrease the value of num # Client code n = int(input("Enter the number : ")) f = factorial(n) print("Factorial of",n,":",f)
4. Find factorial using maths library
Python builtin maths library provides us access to some commonly used math functions, which we can use in our code for more complex mathematical computations. it also has factorial so let's use it.
# import the factorial function from maths library from math import factorial n = int(input("Enter the number : ")) f = factorial(n) print("Factorial of",n,":",f)
5. Find factorial using numpy
Let's use a very popular scientific computing library in python named numpy. Note: numpy is not a builtin package so you have to install it manually using pip.
- Open cmd/Terminal.
- type the command "pip install numpy".
- Wait for the installation to complete.
# import the factorial function from numpy library import numpy factorial = numpy.math.factorial n = int(input("Enter the number : ")) f = factorial(n) print("Factorial of",n,":",f)
That's it for this tutorial. I hope you enjoyed it. If you have any doubts or want to give any suggestion please feel free to comment below.
Also read about Python Program To Multiply Two Matrices