Why does Runtimeerror: Maximum Recursion Limit Reached occur?

There are various reasons for Runtimeerror: Maximum Recursion Limit Reached in Python. Here we are explaining some common reasons for the occurrence of Runtimeerror: Maximum Recursion Limit Reached:

  • Missing Base Case
  • Infinite Recursion
  • Exceeding Maximum Recursion Depth

Missing Base Case

As we all know the recursive function should include a base case that defines when the recursion should stop. However when we do not specify a base case, the function may keep calling itself indefinitely, leading to a Runtimeerror: Maximum Recursion Limit Reached

Python3




def endless_recursion(n):
    """A recursive function without a proper base case"""
    return n * endless_recursion(n-1)
 
print(endless_recursion(5))


Output

 File "Solution.py", line 3, in endless_recursion
return n * endless_recursion(n-1)
File "Solution.py", line 3, in endless_recursion
return n * endless_recursion(n-1)
File "Solution.py", line 3, in endless_recursion
return n * endless_recursion(n-1)
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

Infinite Recursion

This occurs when we incorrectly define recursive logic and it fails to make progress towards the base case can result in infinite recursion. This exhausts the call stack and results into ‘RuntimeError: Maximum Recursion Limit Reached’

Python3




def countdown(n):
    if n > 0:
# Here we dont reduce n so it leads to infinite loop
        print(n)
        countdown(n)
# Example usage with n=5   
n=5
countdown(n)


Output

 File "Solution.py", line 5, in countdown
countdown(n)
File "Solution.py", line 5, in countdown
countdown(n)
[Previous line repeated 994 more times]
File "Solution.py", line 4, in countdown
print(n)
RecursionError: maximum recursion depth exceeded while getting the str of an object

Exceeding Maximum Recursion Depth

In Python, there is a limit on the maximum recursion depth to prevent stack overflow.If a recursive function exceeds this limit, Python raises a ‘RuntimeError: Maximum Recursion Limit Reached’.

Python3




def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
 
# Testing the factorial function
result = factorial(1001)
print(result)


Output

Here we are calling factorial with a large value of n, that why we encounter a RecursionError.

File "Solution.py", line 5, in factorial
return n * factorial(n-1)
File "Solution.py", line 5, in factorial
return n * factorial(n-1)
[Previous line repeated 995 more times]
File "Solution.py", line 2, in factorial
if n == 0:
RecursionError: maximum recursion depth exceeded

Runtimeerror: Maximum Recursion Limit Reached in Python

In this article, we will elucidate the Runtimeerror: Maximum Recursion Limit Reached In Python through examples, and we will also explore potential approaches to resolve this issue.

What is Runtimeerror: Maximum Recursion Limit Reached?

When you run a Python program you may see Runtimeerror: Maximum Recursion Limit Reached. It indicates that the execution of your program has surpassed the recursion limit of the Python interpreter. This typically occurs when a function calls itself recursively, and the recursion doesn’t have a proper stopping condition (base case).

Syntax :

RecursionError: maximum recursion depth exceeded

Similar Reads

Why does Runtimeerror: Maximum Recursion Limit Reached occur?

There are various reasons for Runtimeerror: Maximum Recursion Limit Reached in Python. Here we are explaining some common reasons for the occurrence of Runtimeerror: Maximum Recursion Limit Reached:...

Fix RuntimeError: Maximum Recursion Limit Reached

...

Contact Us