开发者

Ackermann function

开发者 https://www.devze.com 2023-01-01 06:19 出处:网络
I am writing a recursive program which calculates the Ackermann function. Here is the code: public class Ackermann{

I am writing a recursive program which calculates 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 it shows me mistakes:

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(Strin开发者_高级运维g[]args){
                                    ^
Ackermann.java:26: reached end of file while parsing
}
 ^
8 errors

How can this be fixed?


Your last ternary operation has no third operand.

(m>0 && n>0)? ackermann(m-1, ackermann(m,n-1));

Notice how there is a ? but no :.

Since you have covered all the cases, you might change this to return -1, or throw an Exception.

However, you might also implement this function more readably 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 is not necessarily bad, and code golf is not necessarily good. Write your code such that you could come back to it in a year and be able to easily figure out what it was intended to accomplish.


Simply 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 wasn't even complete (missing the else part).


Your third ternary if has no second option. And the construction itself might be valid after fixing that, it's ugly and a more verbose check would show you easily what you've done wrong. Rewrite your if's and realize you don't need three, the third option is just what remains when the two checks have failed.

Computing the Ackermann function isn't really useful by the way, it explodes for all m greater than 3. Your code will just overflow without producing any reasonable results.


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

So you should modify 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; 
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号