Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 823465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:57:07+00:00 2026-05-15T02:57:07+00:00

I am writing a recursive program which calculates the Ackermann function . Here is

  • 0

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(String[]args){
                                    ^
Ackermann.java:26: reached end of file while parsing
}
 ^
8 errors

How can this be fixed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:57:07+00:00Added an answer on May 15, 2026 at 2:57 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble writing a recursive function in C: void func(int n) which for
I'm writing a recursive function that takes a char array, which represents a number,
I'm writing a recursive enumeration function, and I'm having a simple error somewhere. Here's
I'm writing a recursive function in Ocaml that's supposed to count the number of
I am writing this code that one function calls itself recursively. But I am
EDIT I had a problem a while ago writing a program in C which
I am writing a program to crawl the websites. The crawl function is a
I'm writing a program to solve the result of primitive recursive functions: 1 --Basic
How can I break long lines when writing c++ code in vim? For example,
I am writing a grading program for an assignment in which students are implementing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.