-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathackermann.erl
34 lines (29 loc) · 989 Bytes
/
ackermann.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-module(ackermann).
-export([ackermann/2]).
%https://en.wikipedia.org/wiki/Ackermann_function
%added memoization but that doesn't help, it's too monstrously large
ackermann(M, N) ->
case ets:info(acker_memo) of
undefined ->
ets:new(acker_memo, [named_table]);
_ -> ok
end,
ackermann(compute, M, N).
ackermann(compute, M, N) when M == 0 ->
N + 1;
ackermann(compute, M, N) when ((M > 0) and (N == 0)) ->
case ets:lookup(acker_memo, {M, N}) of
[] ->
AckerNum = ackermann(compute, M-1, 1),
ets:insert_new(acker_memo, {{M, N}, AckerNum}),
AckerNum;
[{_, Value}] -> Value
end;
ackermann(compute, M, N) when ((M > 0) and (N > 0)) ->
case ets:lookup(acker_memo, {M, N}) of
[] ->
AckerNum = ackermann(compute, M-1, ackermann(compute, M, N-1)),
ets:insert_new(acker_memo, {{M, N}, AckerNum}),
AckerNum;
[{_, Value}] -> Value
end.