Sie sind auf Seite 1von 3

Recursion – Ackermann’s Function

Recursion
A function may be called from another function or it may call itself. Recursion implies a
function calling itself. If a function call occurs inside the function itself, it is called direct
recursion. If a function calls another function, which in turn makes a call to the first one,
it is called indirect recursion.

Each invocation of a function causes a new allocation of the variables declared inside it.
The storage for auto variables is automatically allocated and freed on function entry and
return.

Advantages of recursive functions:


• Recursive functions are more concise and understandable.

Disadvantages of recursive functions:


• Recursive functions are slower than the corresponding iterative functions.
• Recursive functions are slow because of the overhead of passing arguments and
returning values.
• Recursive functions take up more memory. Each time the function calls itself, the
current status of the function must be preserved (the line being executed, the
values so far). This information is saved onto a data structure called a “stack”. If
recursion goes on for a long time, we get the “Stack Overflow” error. So it is
important to give an “end – condition” so that recursion eventually terminates.

Ackermann function
It takes two natural numbers as arguments and yields another natural number. Its value
grows extremely quickly; even for small inputs. For example for (4,3), the values of the
Ackermann function are so large that they cannot be feasibly computed, and in fact their
decimal expansions require more digits than there are particles in the entire visible
universe.

Definition and properties


The Ackermann function is defined recursively for non-negative integers m and n as
follows
Ack (0, n) = n + 1 for n >= 0

Ack (m, 0) = Ack (m − 1, 1) for m >= 1

Ack (m, n) = Ack (m − 1, Ack (m, n − 1)) for m >= 1, n >= 1

This may also be represented as:

Prof. Mukesh N. Tekwani Page 1 of 3


Recursion – Ackermann’s Function

The evaluation of these functions always terminates. The recursion is bounded because in
each recursion either m decreases, or m remains the same and n decreases. Each time that
n reaches zero, m decreases, so m eventually reaches zero as well. However, when m
decreases there is no upper bound on how much n can increase.

For small values of m like 1, 2, or 3, the Ackermann function grows relatively slowly
with respect to n. For m ≥ 4, however, it grows much more quickly. The expansion of
A(4, 3) cannot be recorded in the known physical universe.

Computing A(1, n) takes linear time in n. Computing A(2, n) takes quadratic time in n.
The time required for higher values of m, is much more.

Explanation
We now illustrate the expansion of the function for m = 1, and n = 2. The
evaluation of A(1, 2) proceeds as follows:

A(1, 2) = A(0, A(1, 1))


= A(0, A(0, A(1, 0)))
= A(0, A(0, A(0, 1)))
= A(0, A(0, 2))
= A(0, 3)
=4

Applications of Ackermann’s function

The Ackermann function, due to its definition in terms of extremely deep recursion, can
be used as a benchmark of a compiler's ability to optimize recursion. This function can be
used to compare the amount of time required to evaluate this function for fixed arguments
in many different programming language implementations.

Prof. Mukesh N. Tekwani Page 2 of 3


Recursion – Ackermann’s Function

Program
/* Ackermann's function */
#include <stdio.h>
#include <conio.h>

int ack(int m, int n);

void main()
{
int n; /* number entered by the user */
int res1, res2, res3, res4; /* value of fn ack() */

clrscr();

printf("Please enter value of n ");


scanf("%d", &n);

res1 = ack(1, n);


printf("Ack(1, %d): %d\n", n, res1);

res2 = ack(1, n + 2);


printf("Ack(1, %d + 2): %d\n", n, res2);

res3 = ack(2, n);


printf("Ack(2, %d): %d\n", n, res3);

res4 = ack(4, 0);


printf("Ack(4, 0): %d\n", res4);

getch();
}

int ack(int m, int n)


{
if ((m == 0) && (n >= 0))
return (n + 1);

if ((m >= 1) && (n == 0))


return(ack(m - 1, 1));

if ((m >= 1 ) && (n >= 1))


return(ack(m - 1, ack(m, n - 1)));
}
/* E O F */

Prof. Mukesh N. Tekwani Page 3 of 3

Das könnte Ihnen auch gefallen