Ackermann Function
Algorithm:
The following procedure computes a mathematical function called the Ackermann function (Exercise 1.10 from SICP):
f(x,y) =
0wheny = 02ywhenx = 02wheny = 1f(x-1, f(x, y-1))in all other cases
For small x, the Ackermann function is equivalent to the following functions:
functionAckermann(0, n) <=> 2*nfunctionAckermann(1, n) <=> 2^nfunctionAckermann(2, n) <=> 2^2^...^2 (n times)
The Ackermann function is known for being very fast to compute.
Code:
def functionAckermann(x: Int, y: Int): Int =
if y == 0 then 0
else if x == 0 then 2 * y
else if y == 1 then 2
else functionAckermann(x - 1, functionAckermann(x, y - 1))
functionAckermann(3, 3)
// 65536
References: