6

I am at early stage of learning Python. I tried calculating Ackerman function for smaller values. It worked just fine until the values are (3,7). Any value higher than that (say 3,8) throws this error. [Process finished with exit code -1073741571 (0xC00000FD)]

At first I checked whether recursion limit is reached, but the process stopped well below the set Recursion limit (in this case it is set to maximum)

import sys
sys.setrecursionlimit(999999999)
count = 0
def cf():
    global count
    count+=1
cf()
def Ack(m,n):
    if  m==0:
        x=n+1
        cf()
        return x
    elif m>0 and n==0:
        x=Ack(m-1,1)
        cf()
        return x
    elif m>0 and n>0:
        x=Ack(m-1,Ack(m,n-1))
        cf()
        return x
a,b=map(int,input("Enter values").split())
print(a,b)
result=Ack(a,b)
print(result)
print(count)
Suresh Kumar
  • 119
  • 2
  • 10
  • Does this answer your question? [Process finished with exit code -1073741571](https://stackoverflow.com/questions/20629027/process-finished-with-exit-code-1073741571) – Tomerikoo May 31 '21 at 16:23

1 Answers1

9

Simple as that, you are getting a stack overflow.

Recursion limit only dictates how deep the recursion calls can go, but it doesn't change the stack size. Each recursive call adds frames to the stack and eventually you are reaching the limit.

If you really want to go so deep with recursion, you have to change stack size with threading.stack_size() and create a new thread.

related question: Process finished with exit code -1073741571