“Discover the Different Ways to Find the Factorial of a Number in Python”
In mathematics, the factorial of a number is the product of all the integers from 1 to that number. The factorial of a number is represented by an exclamation mark (!) following the number. For example, the factorial of 4 is written as 4! and is equal to 1*2*3*4 = 24. In this article, we will explore various ways to find the factorial of a given number using Python programming language. From simple mathematical operations to more complex functions, we will demonstrate different methods to calculate factorials and help you choose the best approach for your specific needs. So, let’s dive in and learn how to find factorials in Python.
Factorial of a number in python using loops
# factorialnum.py # The function returns the factorial of given number. def factorial(num): if num < 1: return -1 fact = 1 # multiply all numbers from 2 to N. for i in range(2, num+1): fact = i * fact return fact # prints the factorial of 4. print(factorial(4))
The provided program is a python script that calculates the factorial of a given number using a function. The function is defined as “factorial(num)” which takes an argument ‘num’ and returns the factorial of that number.
The function starts by checking if the given number is less than 1, if it is, it returns -1. This is done to handle cases where the number passed as an argument is less than 1, as the factorial of a number less than 1 is not defined.
Then it declares a variable ‘fact’ and initializes it to 1. This variable will be used to store the factorial of the given number.
It then uses a for loop to iterate through the range of numbers from 2 to the given number +1. In each iteration, the current number is multiplied with the value of ‘fact’ and the result is stored back in ‘fact’. This is done to keep multiplying the current number with the previous product and thus get the factorial of the given number.
Finally, the function returns the value of ‘fact’ which is the factorial of the given number.
The program then calls the factorial function with an argument of 4 and prints the result. In this case, it will print “24” as 4! = 1*2*3*4 = 24
This is a simple and clear way to find the factorial of a number using a for loop, with a check for negative numbers.
# Output $python factorialnum.py 24
We can even find the factorial numbers in python using a recursion function.
# Recursion function for factorial. F(N) = N * F(N-1)
Let’s write the python code to find factorial using recursion.
# factorialrecur.py # The function returns factorial using recursion. def factorialRecur(num): if(num < 2): return 1 return num * factorialRecur(num-1) # prints the factorial of 4. print(factorialRecur(4))
The output of the above code is as follows.
# Output $ python factorialrecur.py 24
The provided program is a python script that calculates the factorial of a given number using a recursive function. The function is defined as “factorialRecur(num)” which takes an argument ‘num’ and returns the factorial of that number.
The function starts by checking if the given number is less than 2, if it is, it returns 1. This is done to handle the base case, where the factorial of 0 or 1 is defined as 1.
Then it calls the same function “factorialRecur(num-1)” by decrementing the value of num by 1, this process is repeated until the base case is reached and the function starts returning the values. Each returned value is then multiplied by the value of ‘num’ that was passed as an argument, this way each returned value is the factorial of one less number and when it is multiplied by the current number passed in the argument it gives the factorial of that number.
Finally, the function returns the value, which is the factorial of the given number.
This is a simple and clear way to find the factorial of a number using recursion, it uses the function call stack to keep track of the state of the function and to return the final value.
These are the two methods to identify the factorial of any given number using python programming. Hope this article helps you to understand the factorial in python. Happy learning!!.