-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathackermann_loop.java
52 lines (45 loc) · 1.48 KB
/
ackermann_loop.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.jon;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
System.out.println(ackerman_recurse(3, 3));
System.out.println(ackerman_loop(3, 3));
System.out.println(ackerman_recurse(3, 1));
System.out.println(ackerman_loop(3, 1));
System.out.println(ackerman_recurse(2, 4));
System.out.println(ackerman_loop(2, 4));
}
public static long ackerman_recurse(long m, long n) {
if (!(m >= 0 && n >= 0)) throw new IllegalArgumentException("m and n must be nonnegative");
if (m == 0) return n + 1;
else {
// m > 0
if (n == 0) return ackerman_recurse(m-1, 1);
else return ackerman_recurse(m-1, ackerman_recurse(m, n-1)); // n > 0
}
}
public static long ackerman_loop(long m, long n) {
if (!(m >= 0 && n >= 0)) throw new IllegalArgumentException("m and n must be nonnegative");
Stack<Long> ms = new Stack();
while (true) {
if (m == 0) {
if (ms.isEmpty()) return n + 1;
else {
m = ms.pop();
++n;
}
} else {
// m > 0
if (n == 0) {
// tail call
--m;
n = 1;
} else {
// n > 1
ms.push(m-1);
--n;
}
}
}
}
}