/** This implements the hyperoperation function, which is a generalization of several mathematical operators: * n = 0 : the unary successor function (returns b+1; a is ignored) * n = 1 : addition (a + b) (a+1+1+1+1... b times) * n = 2 : multiplication (a * b) (a+a+a+a... b times) * n = 3 : exponentiation (a^b) (a*a*a*a... b times) * n = 4 : tetration (a^a^a^a ... b times) * etc See: https://en.m.wikipedia.org/wiki/Hyperoperation */ /** Optimized version of hyperoperations. */ hyper[n, a, b] := { println["hyper[$n, $a, $b]"] if n == 0 return b+1 if n == 1 return a + b if n == 2 return a * b if b == 0 return 1 if n == 3 return a^b return hyper[n-1, a, hyper[n, a, b-1]] }