educative.io

Educative

Error with numbers grater than 997

Hi, with numbers grater than 997 the Function throws errors, why!?

def rec_count(number):
print(number)
# Base case
if number == 0:
    return 0
rec_count(number - 1)  # A recursive call with a different argument
print(number)
rec_count(998)

Traceback (most recent call last):
File “main.py”, line 10, in
rec_count(998)
File “main.py”, line 6, in rec_count
rec_count(number - 1) # A recursive call with a different argument
File “main.py”, line 6, in rec_count
rec_count(number - 1) # A recursive call with a different argument
File “main.py”, line 6, in rec_count
rec_count(number - 1) # A recursive call with a different argument
File “main.py”, line 6, in rec_count
rec_count(number - 1) # A recursive call with a different argument
File “main.py”, line 6, in rec_count

Hey @ek1

This problem is related to Python stack. We can increase the capacity of the stack using the following :

import sys
sys.setrecursionlimit(10000)

Set the recursion limit according to your requirement, you won’t get an error now.

Happy learning,