I am wondring about the reason for the high setrecursionlimit overhead at Google Colab.
I am trying to make a basic power recursive function basicpower
as a part of my university assignment shown in the screenshot below. I tried to set the recursion limit to 100 using sys.setrecursionlimit(100)
locally on my laptop. When I make a call to basicpower with any value between 98 and 100 if shows me a RecursionError: maximum recursion depth exceeded in comparison error
. However, if I make a call to basicpower
with any value between 1 and 97 it works perfectly fine. Furthermore, Why does Colab prevent me from using 39 frames not only the three mentioned above? What causes this overhead in the call stack?
Code of basic power function:
import sys
sys.setrecursionlimit(100)
def basicpower(x, n):
'''Compute the value x**n for integer n.'''
if n == 0:
return 1 # base case without base case our recursive function will run forever
else:
return x * basicpower(x, n-1) # our recursive call so we are calling the function on itself on a smaller problem space
print(basicpower(2,98))