' http://largeint.sourceforge.net/Ackerman.htm
' https://en.wikipedia.org/wiki/Ackermann_function

uses console

function A (int m, n) as quad
      select case m
      case 0
         function = n + 1
      case 1
         function = n + 2
      case 2
         function = 2 * n + 3
      case 3
         function = 2 ^ (n + 3) - 3
      case else
         if n = 0 then
            function = A(m - 1, 1)
         else
            function = A(m - 1, A(m, n - 1))
         end if
      end select
end function

int x, r, c
string line = string(64, "-")

cls
print "Ackermann function A(m,n)" + cr + cr
print  "   n|   "
printl "m   |   "
for x = 0 to 5 : print x tab : next : print x
printl line
for r = 0 to 5
   printl r "   |   "
   for c = 0 to 6
      if r=4 and c=2 then print "too big" : exit for
      if r=5 and c=1 then print "too big" : exit for 
      print A(r,c) tab
   next
next

printl
printl "Enter ..."
waitkey