Ackermann function with C++


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 notprimitive recursive. All primitive recursive functions are total and computable, but the Ackermann function illustrates that not all total computable functions are primitive recursive.

After Ackermann’s publication[1] of his function (which had three nonnegative integer arguments), many authors modified it to suit various purposes, so that today “the Ackermann function” may refer to any of numerous variants of the original function. One common version, the two-argument Ackermann–Péter function, is defined as follows for nonnegative integers m and n:

Its value grows rapidly, even for small inputs. For example A(4,2) is an integer of 19,729 decimal digits.

#include <stdio.h>
/* The three conditions can be found here:
   http://en.wikipedia.org/wiki/Ackermann%27s_function
*/
int acker(int m, int n){
    if (m == 0) return (n + 1);
    else if ((m > 0) && (n == 0)){
         return acker(m-1, 1);
    }
    else if ((m > 0) && (n > 0)){
         return acker(m-1, acker(m, n-1));
    }
}
/*sample usage*/
int main(){
    /*iterate through the first 4 "pairs"
              1 3 7 61
    */
    for(int i = 0; i < 4; i++){
            printf("%d ", acker(i,i));
    }
    return 0;
}

Source
http://en.wikipedia.org/wiki/Ackermann_function
http://www.dreamincode.net/code/snippet5771.htm
http://www.cplusplus.com/forum/articles/2935/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s