Hallo,
in the example shown I can see for acker(4,1) the awaited 777 but then it crashes
probably in acker(4,2) and because of really confusion in stack and/or memory lack
How can I clear that memory and kill all the recursive death bodies I guess?
The optimal would be, it writes also acker(4,2)=777 and so on.
This is a example for a more complex program, that should stay alive also after
a possible stack overflow and save some results and try again recursion with lower deep.
For nmax=3 and mmax=14 it worked so the function seems to be ok. Well and the strings
are only to provoke stack overflow to come some earlier (normally acker(4,1) is done but
to wait for the exception is to long)
program ackermann;
{$mode objfpc}
const nmax = 4;
mmax = 4{15};
var a,n,m:int64;
function acker(n,m: int64):int64;
var provoke: array[1..4] of string;
begin
if n = 0
then acker:= m + 1
else if m = 0
then acker:= acker( n-1, 1)
else acker:= acker( n-1, acker( n, m-1))
end;
begin
n:=0; m:=0;
while (n <= nmax) do begin
while (m <= mmax) do begin
try
begin
a:= acker( n, m );
end;
except
begin
a:= 777;
end;
end;
writeln('n=',n,' m=',m,' acker(',n,',',m,')=',a);
inc(m);
end;
inc(n); m:=0;
end;
end.