Ackerman function

I am writing a recursive program that evaluates the Ackermann function .

Here is the code:

public class Ackermann{ public static long ackermann( long m,long n) { return (m==0)? n+1: (m>0 && n==0)? ackermann(m-1,1): (m>0 && n>0)? ackermann(m-1, ackermann(m,n-1)); } public static void main(String[]args) { long m=4; long n=2; System.out.println(ackermann(m,n)); } } 

But this shows me errors:

 Ackermann.java:7: : expected (m>0 && n>0)? ackermann(m-1, ackermann(m,n-1)); ^ Ackermann.java:7: ';' expected (m>0 && n>0)? ackermann(m-1, ackermann(m,n-1)); ^ Ackermann.java:18: illegal start of expression public static void main(String[]args){ ^ Ackermann.java:18: ';' expected public static void main(String[]args){ ^ Ackermann.java:18: illegal start of expression public static void main(String[]args){ ^ Ackermann.java:18: ';' expected public static void main(String[]args){ ^ Ackermann.java:18: ';' expected public static void main(String[]args){ ^ Ackermann.java:26: reached end of file while parsing } ^ 8 errors 

How can this be fixed?

+4
source share
5 answers

Just use

 public static long ackermann(long m, long n) { return (m==0)? n+1: (m>0 && n==0)? ackermann(m-1,1): ackermann(m-1, ackermann(m,n-1)); } 

Your last ternary operator is useless and not even complete (the else part is missing).

+4
source

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)); } // Something went wrong System.out.println("Invalid inputs: m and n cannot be negative"); return -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.

+9
source

Your third three, if she does not have a second option. And the design itself may be valid after it is fixed, this ugly and more detailed check will easily show you what you did wrong. Rewrite your if and understand that you do not need three, the third option is what remains when the two checks failed.

The calculation of the Ackerman function is not very useful, by the way, it explodes for all m greater than 3. Your code will simply overflow without any reasonable results.

+4
source

Java only supports this exact syntax conditional?true-statement:false-statement there is no syntax like conditional?true-statement .

So you should change your code to something like this:

 public static long ackermann( long m,long n){ return (m==0)? n+1: (m>0 && n==0)? ackermann(m-1,1): (m>0 && n>0)? ackermann(m-1, ackermann(m,n-1)):0; } 
+2
source

There is no expression for the case when (m>0 && n>0) false

+1
source

Source: https://habr.com/ru/post/1311305/


All Articles