How to Solve RecursionError: Maximum Recursion Depth Exceeded

Python RecursionError is raised while working with recursive functions. The recursive function is similar to a normal function. It is a function calling itself, again and again, known as the recursive function. There is some recursive depth up to which the function calls itself. If this depth is exceeded, then the RecursionError is raised.

What is RecursionError in Python

Python RecursionError exception is raised when your program’s execution exceeds the Python interpreter’s recursion limit. The program will raise an error if the recursion depth exceeds this limit.

def fib(n):
 if n <= 1:
   return n
 else:
   return(fib(n-1) + fib(n-2))

print(fib(10))

Output

55

This is a program for finding the Fibonacci of the nth number. If we execute this program, the output will be displayed. This is because we 55. We used recursion to find the Fibonacci number. In the statement, we can see that inside the function, the same function is called the recursion.

If we change the n value, this program may create an error. If the number is smaller, it will work great, but if we give a big number, there is a chance of getting an error.

def fib(n):
 if n <= 1:
   return n
 else:
   return(fib(n-1) + fib(n-2))


print(fib(100))

This program creates an error known as the RecursionError: maximum recursion depth exceeded in comparison. 

One way to solve this is by changing the recursion depth limit.

import sys

print( sys.setrecursionlimit(2000))

But this is not a good solution. We should not change this because it slows down the processor. When this error was raised, we didn’t fix the boundary condition.

def fib(n):
  return(fib (n-1) + fib (n-2))
print(fib(5))

Output

RecursionError: maximum recursion depth exceeded

This program creates an error known as the maximum recursion depth reached. This RecursionError can be solved by fixing the boundary condition. The boundary is the limit that the program should not execute below or above.

def fib(n):
  if n <= 1:
    return n
  else:
    return(fib(n-1) + fib(n-2))

print(fib(5))

Output

5

In this case, if condition is the boundary condition. Another way of doing this is by using the try and except clause.

def fib(n):
 try:
   return(fib(n-1) + fib(n-2))
 except RecursionError as e:
   print(e)


print(fib(5))

Output

maximum recursion depth exceeded while calling a Python object

RecursionError: Maximum Recursion Depth Exceeded

To solve RecursionError: Maximum Recursion Depth Exceeded in Python, use the memorization. Memorization is the process of memorizing the old performed tasks. This is the best practice for solving this error. Moreover, it can perform a big number of operations at high speed.

def fib(n, memo={}):
 if(n in memo):
   return memo[n]
 if(n <= 2):
   return 1
 else:
   memo[n] = fib(n-1, memo) + fib(n-2, memo)
   return memo[n]


print(fib(10))

Output

55

This is the best solution for the Fibonacci series. In this example, we used a memo dictionary to store all the values. So if the process has already been executed will not be executed again. This is known as Memoization.

Conclusion

Maximum Recursion Depth Exceeded error occurs when the recursion depth has exceeded its maximum limit. This error can be used to find using the try and except clause. This error can be solved by using boundary conditions and using memoization.

That’s it for this tutorial.

Leave a Comment