Ackermann Function - GeeksforGeeks

11 Jun, 2021
In computability theory, the Ackermann function, named after Wilhelm Ackermann, is one of the simplest and earliest-discovered examples of a total computable function that is not primitive recursive. All primitive recursive functions are total and computable, but the Ackermann function illustrates that not all total computable functions are primitive recursive. Refer this for more.It’s a function with two arguments each of which can be assigned any non-negative integer.Ackermann function is defined as: 
Ackermann algorithm:  
Ackermann(m, n) 
   {next and goal are arrays indexed from 0 to m, initialized so that next[O] 
    through next[m] are 0, goal[O] through goal[m - l] are 1, and goal[m] is -1} 
repeat
    value <-- next[O] + 1 
    transferring <-- true 
    current <-- O 
    while transferring do begin
       if next[current] = goal[current] then goal[current] <-- value
                                            else transferring <-- false
       next[current] <-- next[current]+l
       current <-- current + 1 
       end while
until next[m] = n + 1 
return value {the value of A(m, n)}
end Ackermann 
Here’s the explanation of the given Algorithm: Let me explain the algorithm by taking the example A(1, 2) where m = 1 and n = 2 So according to the algorithm initially the value of next, goal, value and current are: 
Though next[current] != goal[current], so else statement will execute and transferring become false. So now, the value of next, goal, value and current are: 
Similarly by tracing the algorithm until next[m] = 3 the value of next, goal, value and current are changing accordingly. Here’s the explanation how the values are changing, 
 
 
Finally returning the value e.g 4Analysis of this algorithm: 
The time complexity of this algorithm is: O(mA(m, n)) to compute A(m, n)  
The space complexity of this algorithm is: O(m) to compute A(m, n)  
Let’s understand the definition by solving a problem! 
Solve A(1, 2)?Answer:Given problem is A(1, 2) Here m = 1, n = 2 e.g m > 0 and n > 0 Hence applying third condition of Ackermann function A(1, 2) = A(0, A(1, 1)) ———- (1) Now, Let’s find A(1, 1) by applying third condition of Ackermann function A(1, 1) = A(0, A(1, 0)) ———- (2) Now, Let’s find A(1, 0) by applying second condition of Ackermann function A(1, 0) = A(0, 1) ———- (3) Now, Let’s find A(0, 1) by applying first condition of Ackermann function A(0, 1) = 1 + 1 = 2 Now put this value in equation 3 Hence A(1, 0) = 2 Now put this value in equation 2 A(1, 1) = A(0, 2) ———- (4) Now, Let’s find A(0, 2) by applying first condition of Ackermann function A(0, 2) = 2 + 1 = 3 Now put this value in equation 4 Hence A(1, 1) = 3 Now put this value in equation 1 A(1, 2) = A(0, 3) ———- (5) Now, Let’s find A(0, 3) by applying first condition of Ackermann function A(0, 3) = 3 + 1 = 4 Now put this value in equation 5 Hence A(1, 2) = 4So, A (1, 2) = 4 
 
Let’s solve another two questions on this by yourself!Question: Solve A(2, 1)? Answer: 5Question: Solve A(2, 2)? Answer: 7 
Here is the simplest c and python recursion function code for generating Ackermann function 
C++
C
Java
Python3
C#
Javascript
// C++ program to illustrate Ackermann function#include <iostream>using namespace std; int ack(int m, int n){    if (m == 0){        return n + 1;    }    else if((m > 0) && (n == 0)){        return ack(m - 1, 1);    }    else if((m > 0) && (n > 0)){        return ack(m - 1, ack(m, n - 1));    }} // Driver codeint main(){    int A;    A = ack(1, 2);    cout << A << endl;    return 0;} // This code is contributed by SHUBHAMSINGH10
// C program to illustrate Ackermann function #include <stdio.h>int ack(int m, int n){    if (m == 0){        return n+1;    }    else if((m > 0) && (n == 0)){        return ack(m-1, 1);    }    else if((m > 0) && (n > 0)){        return ack(m-1, ack(m, n-1));    }} int main(){    int A;    A = ack(1, 2);    printf("%d", A);    return 0;} // This code is contributed by Amiya Rout
// Java program to illustrate Ackermann function class GFG{     static int ack(int m, int n)    {        if (m == 0)        {            return n + 1;        }        else if((m > 0) && (n == 0))        {            return ack(m - 1, 1);        }        else if((m > 0) && (n > 0))        {            return ack(m - 1, ack(m, n - 1));        }else        return n + 1;    }     // Driver code    public static void main(String args[])    {        System.out.println(ack(1, 2));    }} // This code is contributed by AnkitRai01
# Python program to illustrate Ackermann function def A(m, n, s ="% s"):    print(s % ("A(% d, % d)" % (m, n)))    if m == 0:        return n + 1    if n == 0:        return A(m - 1, 1, s)    n2 = A(m, n - 1, s % ("A(% d, %% s)" % (m - 1)))    return A(m - 1, n2, s) print(A(1, 2)) # This code is contributed by Amiya Rout
// C# program to illustrate Ackermann functionusing System; class GFG{     static int ack(int m, int n)    {        if (m == 0)        {            return n + 1;        }        else if((m > 0) && (n == 0))        {            return ack(m - 1, 1);        }        else if((m > 0) && (n > 0))        {            return ack(m - 1, ack(m, n - 1));        }else        return n + 1;    }     // Driver code    public static void Main()    {                 Console.WriteLine(ack(1, 2));    }} // This code is contributed by mohit kumar 29
<script>// Javascript program to illustrate Ackermann function function ack(m,n){    if (m == 0)        {            return n + 1;        }        else if((m > 0) && (n == 0))        {            return ack(m - 1, 1);        }        else if((m > 0) && (n > 0))        {            return ack(m - 1, ack(m, n - 1));        }else        return n + 1;} // Driver codedocument.write(ack(1, 2));  // This code is contributed by unknown2108</script>
If you still wish to visualize how this result is arrived at, you can take a look at this page, which animates the calculation of every recursion step. 
mohit kumar 29
ankthon
SHUBHAMSINGH10
unknown2108
Technical Scripter 2019
Advanced Data Structure
GATE CS
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
2-3 Trees | (Search, Insert and Deletion)
Extendible Hashing (Dynamic approach to DBMS)
Quad Tree
Proof that Dominant Set of a Graph is NP-Complete
Suffix Array | Set 1 (Introduction)
Layers of OSI Model
ACID Properties in DBMS
TCP/IP Model
Page Replacement Algorithms in Operating Systems
Types of Operating Systems