Skip to content

Height of call graph for TAK function

An answer to this question on Stack Overflow.

Question

How would I go about computing the height of the call graph for the TAK function:

enter image description here

in terms of the three parameters?

Answer

You could run the function passing an additional fourth and fifth parameter to store the height of the current call stack and the maximum height observed in the call graph thus far.

Note that this will require unpackaging the function. Also, assume that maxcalls is passed by reference.

t1=t(x-1,y,z,calls,maxcalls)
t2=t(y-1,z,x,calls,maxcalls)
t3=t(z-1,x,y,calls,maxcalls)
if y<x:
  t=t(t1,t2,t3,call+1,maxcalls)
else:
  if calls>maxcalls:
    maxcalls=calls
  t=z

Given the complexity of this function and its use as a good benchmark, it's unlikely that there's a way (or at least an obvious one) to calculate this without actually running the function.