Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: About the ackermann function...

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default About the ackermann function...

    i cant really understand this concept

    i have this code...
    public static int A(int m, int n){
     
    if(m == 0) return n+1;
    else if(m > 0 && n ==0) return A(m-1,1);
    else if(m > 0 && n > 0) return A(m-1, A(m, n-1);
    return 0;
    }
    my code works but i dont really understand what it does,
    see i called this method with 3,4 and it spits out 125 as it should
    but w/ 4,3 the machine runs out of stack space or memory..

    can somebody explain this concept? thanks..


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: About the ackermann function...

    Quote Originally Posted by gonfreecks View Post
    i cant really understand this concept

    i have this code...
    public static int A(int m, int n){
     
    if(m == 0) return n+1;
    else if(m > 0 && n ==0) return A(m-1,1);
    else if(m > 0 && n > 0) return A(m-1, A(m, n-1);
    return 0;
    }
    my code works but i dont really understand what it does,
    see i called this method with 3,4 and it spits out 125 as it should
    but w/ 4,3 the machine runs out of stack space or memory..

    can somebody explain this concept? thanks..
    Hello, gonfreecks.
    Ackerman function requires a lot of recursion.

    Once a function goes too deeply in its recursion a java.lang.StackOverflowError occurs.
    On my machine (Windows XP, 1 GB of memory) I tried a dummy recursive function and an StackOverflowError occured on recursion depths 6236 and 6260.


    With Ackerman function once there is a calculation of expression like A(m,n) and m and n are positive numbers,it is going to recurse at least as deep as n-1.

    For example if we have A(1,10000) it is going to recurse
    A(1,10000)=A(0,A(1,9999))=A(0,A(0,A(0,A(1,9998)))= A(0,A(0,A(0,A(0,A(1,9997))))...... A(0,...A(1,0)) and it is already at recursion depth of
    9999 which is greater than 6236 or 6260 on which the dummy function caused the exception on my computer.

    While calculating A(4,3) the recursive calls which follow involve calls with much greater n than 10000, which we maybe can think of as some recursion depth limit.
    For example, there must be a call to A(3,2^65536-3) while calculating A(4,3)
    (Ackermann function - Wikipedia, the free encyclopedia),
    which would require recursion depth of at least 2^65536-4 , and that is way greater than 10000.

    Even with A(4,1) there are calls A(m,n) with n greater than 4000 , and which cause exception on my computer.

    So, A(4,3) recurses way too deep.

    Hope it helped,
    Valentin
    Last edited by Valen007; March 25th, 2011 at 09:26 AM. Reason: mistyping

Similar Threads

  1. Javascript function returns NaN
    By jai in forum Other Programming Languages
    Replies: 2
    Last Post: April 19th, 2011, 02:31 AM
  2. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 03:32 PM
  3. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  4. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 11:29 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM