question04

Top  Previous  Next

Write a recursive function which returns the value of A(m,n).

This function grows very quickly in value. This function A(m,n) is defined as:

 

if m=0 then n+1

if m>0 and n=0 then A(m-1,1)

if m>0 and n>0 then A(m-1,A(m,n-1))

 

INPUT

m     n  

OUTPUT

0

0

1

0

1

2

0

2

3

0

3

4

0

4

5

1

0

2

1

1

3

1

2

4

1

3

5

1

4

6

2

0

3

2

1

5

2

2

7

2

3

9

2

4

11

 

 

mytoggle_plus1See the solution:

function Ackermann(m, n : Integer) : Integer;

begin

    if m = 0 then

        Result := n+1

    else if n = 0 then

        Result := Ackermann(m-11)

    else Result := Ackermann(m-1, Ackermann(m, n-1));

end;

 

{---}

   for var i := 0 to 3 do

     for var j := 0 to 4 do

     begin

      WriteLn( Ackermann(i,j) );

     end;

1,2,3,4,5,

2,3,4,5,6,

3,5,7,9,11,

5,13,29,61,125