I'm using Ruby v1.9.1 to write a program with the Ackermann-function for my class in University. The code is the following:
def ackermann(n,m)
if n == 0 && m > 0
return m+1
elsif n > 0 && m == 0
return ackermann(n-1,1)
elsif n > 0 && m > 00
return ackermann(n-1,ackermann(n,m-1))
else
puts "Wrong input, m and n must be higher than 0"
end
end
puts ackermann(5,5)
This is a highly recursive function. So I get the error "stack level too deep (SystemStackError)". Is there any way or workaround to fix this error?