title: Modified Ackermann Function in Python Segmentation fault
body: <p>So I tried to create a modified version of the basic Ackermann function that uses a dictionary to store the already computed values, so that next time when the program comes across a similar function call it can use the already computed value, and this seems to speed up the process quiet considerably. Here is the code:</p>
  
  <pre><code>import sys
  sys.setrecursionlimit(100000)
  
  resultset ={}
  
  def ackermann(m, n):
      """Computes the Ackermann function A(m, n)
  
      See http://en.wikipedia.org/wiki/Ackermann_function
  
      n, m: non-negative integers
      """
      if (m,n) in resultset:
          return resultset[(m,n)]
      if m == 0:
          ans = n+1;
      elif n == 0:
          ans = ackermann(m-1, 1)
      else:
          ans = ackermann(m-1, ackermann(m, n-1))
      if m != 0:
          resultset[(m,n)] = ans
      return ans;
  
  for i in range(0,10) :
      for j in range(0,10) :
          print("ackermann(%d, %d): " % (i, j) + str(ackermann(i,j)))
  </code></pre>
  
  <p>Now the problem I have is that it stops execution after a very short while. The output is like:</p>
  
  <pre><code>ackermann(0, 0): 1
  ackermann(0, 1): 2
  ackermann(0, 2): 3
  ackermann(0, 3): 4
  ackermann(0, 4): 5
  ackermann(0, 5): 6
  ...
  ...
  ...
  ackermann(3, 2): 29
  ackermann(3, 3): 61
  ackermann(3, 4): 125
  ackermann(3, 5): 253
  ackermann(3, 6): 509
  ackermann(3, 7): 1021
  ackermann(3, 8): 2045
  ackermann(3, 9): 4093
  ackermann(4, 0): 13
  ackermann(4, 1): 65533
  Segmentation fault
  </code></pre>
  
  <p>My system is a core i5 with 12 GB  of ram, and this program exits way before it reaches the mem limit, what could be the problem?</p>
  
  <p>I also tried using Shelves instead of dictionary so that it would store the data on disk. Here is the code for that:</p>
  
  <p>I also tried using shelves instead of dictionaries, so that I could see the usage on disk. Here is the code for that..</p>
  
  <pre><code>import sys
  sys.setrecursionlimit(100000)
  
  import shelve
  resultset = shelve.open("file.o")
  
  def ackermann(m, n):
      """Computes the Ackermann function A(m, n)
  
      See http://en.wikipedia.org/wiki/Ackermann_function
  
      n, m: non-negative integers
      """
      if str((m,n)) in resultset:
          return resultset[str((m,n))]
      if m == 0:
          ans = n+1;
      elif n == 0:
          ans = ackermann(m-1, 1)
      else:
          ans = ackermann(m-1, ackermann(m, n-1))
      if m != 0:
          resultset[str((m,n))] = ans
      return ans;
  
  for i in range(0,6) :
      for j in range(0,6) :
          print("ackermann(%d, %d): " % (i, j) + str(ackermann(i,j)))
  </code></pre>
  
  <p>The output file comes to exactly 6MB and python crashes. Does anybody have a clue why?</p>