- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# How to write recursive Python Function to find factorial?

Factorial of a number is the product of all the positive integers from 1 to that number. For example, the factorial of 4 is 4*3*2*1 = 24.

To find the factorial of a number using recursive Python function, we can define a function that calls itself with a smaller input until it reaches the base case, which is the factorial of 1, which is 1.

Here is the code to find the factorial of a number using recursive Python function:

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1)

In this code, the function factorial(n) takes a number n as input and returns the factorial of that number. The first if statement checks if the input number n is 1. If n is 1, the function returns 1 as the base case. If n is not 1, the function returns the product of n and the factorial of n-1. This is where the recursive function call happens, where the function calls itself with the input n-1.

To use this function, you can simply call it with the number whose factorial you want to find, like this:

### Example

Finding the factorial of 8

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1) print(factorial(8))

### Output

40320

### Example

The following code calculates the factorial for n = 6 and n = 15

def factorial(n): if n == 1: return 1 else: res = n * factorial(n-1) return res print ("factorial(6) = %d" %factorial(6)) print ("factorial(15) = %d" %factorial(15))

### Output

factorial(6) = 720 factorial(15) = 1307674368000

Here is an example of the code in action:

### Example

def factorial(n): if n==0 or n == 1: return 1 else: return n * factorial(n-1) print(factorial(5))

When you run this code, it will output 120, which is the factorial of 5.

### Output

120

To understand how the function works, let's see how it would find the factorial of 5:

The function is called with the input 5.

Since 5 is not 1, the else block is executed.

The function returns 5 * factorial(4).

To find the factorial of 4, the function is called again with the input 4.

Since 4 is not 1, the else block is executed.

The function returns 4 * factorial(3).

To find the factorial of 3, the function is called again with the input 3.

Since 3 is not 1, the else block is executed.

The function returns 3 * factorial(2).

To find the factorial of 2, the function is called again with the input 2.

Since 2 is not 1, the else block is executed.

The function returns 2 * factorial(1).

To find the factorial of 1, the function is called again with the input 1.

Since 1 is equal to 1, the if block is executed.

The function returns 1 as the base case.

The previous function calls continue to be resolved.

2 * factorial(1) is resolved to 2 * 1 = 2.

3 * factorial(2) is resolved to 3 * 2 = 6.

4 * factorial(3) is resolved to 4 * 6 = 24.

5 * factorial(4) is resolved to 5 * 24 = 120.

The final result, 120, is returned.

### Output

120

This process shows how the function uses recursion to repeatedly call itself with smaller inputs until it reaches the base case of 1, and then returns the factorial of each input by multiplying it with the factorial of the next smaller input.

Some more examples of using the recursive function to find the factorial of different numbers are here as under:

### Example

Find the factorial of 7

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1) print(factorial(7))

### Output

5040

### Example

Find the factorial of 10

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1) print(factorial(10))

### Output

3628800

### Example

Find the factorial of 1

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1) print(factorial(1))

### Output

1

### Example

Find the factorial of 0 (note that the factorial of 0 is defined as 1)

def factorial(n): if n == 1 or n == 0: return 1 else: return n * factorial(n-1) print(factorial(0))

### Output

1

These examples show how the function can be used to find the factorial of different numbers, including the edge cases of 0 and 1.

Here's one more example to illustrate how the function handles large inputs:

### Example

Find the factorial of 20

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1) print(factorial(20))

### Output

2432902008176640000

This example shows that the function can handle large inputs without running into errors or performance issues. However, it's worth noting that for very large inputs, the recursive function may run into issues with stack overflow or take a very long time to execute, so in those cases, it may be better to use an iterative implementation of the factorial function.

Here's an example of an iterative implementation of the factorial function in Python:

def factorial(n): result = 1 for i in range(1, n+1): result *= i return result

In this implementation, the function iterates over all the numbers from 1 to n, multiplying them together to compute the factorial. The result is stored in the variable result, which is initialized to 1.

To use this function, you can call it with the number whose factorial you want to find, like this:

### Example

def factorial(n): result = 1 for i in range(1, n+1): result *= i return result print(factorial(9))

This will output 362880, which is the factorial of 9.

### Output

362880

This iterative implementation has a few advantages over the recursive implementation for very large inputs. First, it doesn't run into stack overflow issues, since it doesn't use function calls to compute the factorial. Second, it's often faster than the recursive implementation, since it doesn't have the overhead of function calls and stack manipulation. However, for smaller inputs, the recursive implementation can be more concise and easier to understand.

- Related Articles
- How to write recursive Python Function to find factorial?
- How to write a recursive function in Python?
- How to write the factorial function with reduce and range in JavaScript?
- Comparing the performance of recursive and looped factorial function in JavaScript
- Recursive factorial method in Java
- How to Find Factorial of Number Using Recursion in Python?
- How to Find the Factorial of a Number using Python?
- C program to find GCD of numbers using recursive function
- How to write an empty function in Python?
- How to create a simple Recursive Function in Golang?
- Python program to find factorial of a large number
- C program to find GCD of numbers using non-recursive function
- C++ Program to Find Factorial
- How to write a function to get the time spent in each function in Python?
- Recursive function to do substring search in C++
- Haskell Program to create a simple recursive function