### Ackermann function

Posted:

**Fri Aug 02, 2019 8:17 am**https://en.wikipedia.org/wiki/Ackermann_function

My JavaScript implementations:

A faster version without recursion:

Even with small inputs, it churns though a massive number of computational steps, and requires a large amount of memory, on the way towards reaching the output.

My JavaScript implementations:

Code: Select all

```
function ackermann(m, n) => {
if (m === 0) return n + 1;
if (m > 0 && n === 0) return ap(m - 1, 1);
if (m > 0 && n > 0) return ap(m - 1, ap(m, n - 1));
}
```

Code: Select all

```
function ackermann(m, n) => {
let cm = [];
while (true) {
if (m === 0) {
if (!cm.length) return n + 1;
m = cm.pop();
++n;
} else if (m > 0 && n === 0) {
--m;
n = 1;
} else if (m > 0 && n > 0) {
--n;
cm.push(m - 1);
}
}
}
```