Tak (function)

In computer science, the Tak function is a recursive function, named after Ikuo Takeuchi (ja:竹内郁雄). It is defined as follows:

$$ \tau (x,y,z) = \begin{cases} \tau (\tau (x-1,y,z) ,\tau (y-1,z,x) ,\tau (z-1,x,y) ) & \text{if } y < x \\ z & \text{otherwise} \end{cases} $$

This function is often used as a benchmark for languages with optimization for recursion.

tak vs. tarai
The original definition by Takeuchi was as follows:

tarai is short for たらい回し tarai mawashi, "to pass around" in Japanese.

John McCarthy named this function tak after Takeuchi.

However, in certain later references, the y somehow got turned into the z. This is a small, but significant difference because the original version benefits significantly from lazy evaluation.

Though written in exactly the same manner as others, the Haskell code below runs much faster.

One can easily accelerate this function via memoization yet lazy evaluation still wins.

The best known way to optimize tarai is to use mutually recursive helper function as follows.

Here is an efficient implementation of tarai in C: Note the additional check for (x <= y) before z (the third argument) is evaluated, avoiding unnecessary recursive evaluation.