Ackermann function: Difference between revisions
Content added Content deleted
Line 5,350: | Line 5,350: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
<lang prolog>ack |
<lang prolog>:- table ack/3. % memoization reduces the execution time of ack(4,1,X) from several |
||
% minutes to about one second on a typical desktop computer. |
|||
ack(0, N, Ans) :- Ans is N+1. |
|||
ack(M, 0, Ans) :- M>0, X is M-1, ack(X, 1, Ans). |
ack(M, 0, Ans) :- M>0, X is M-1, ack(X, 1, Ans). |
||
ack(M, N, Ans) :- M>0, N>0, X is M-1, Y is N-1, ack(M, Y, Ans2), ack(X, Ans2, Ans).</lang> |
ack(M, N, Ans) :- M>0, N>0, X is M-1, Y is N-1, ack(M, Y, Ans2), ack(X, Ans2, Ans).</lang> |