There is no third operand in your last triple operation.
(m>0 && n>0)? ackermann(m-1, ackermann(m,n-1));
Note what exists ? but not :
Since you have covered all cases, you can change this to return -1, or throw an exception.
However, you can also implement this function more easily without using the ternary operator:
public static long ackermann(long m, long n) { if (m == 0) { return n+1; } if (m > 0 && n == 0) { return ackermann(m-1, 1); } if (m > 0 && n > 0) { return ackermann(m-1, ackermann(m, n-1)); }
More lines of code are not necessarily bad, and golf code is not necessarily good. Write your code so that you can return to it in a year and you can easily understand what it had to execute.
source share