How to resolve the algorithm Ackermann function step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ackermann function step by step in the Oberon-2 programming language

Table of Contents

Problem Statement

The Ackermann function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. It grows very quickly in value, as does the size of its call tree.

The Ackermann function is usually defined as follows:

Its arguments are never negative and it always terminates.

Write a function which returns the value of

A ( m , n )

{\displaystyle A(m,n)}

. Arbitrary precision is preferred (since the function grows so quickly), but not required.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ackermann function step by step in the Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE ackerman;

IMPORT  Out;

VAR     m, n    : INTEGER;

PROCEDURE Ackerman (x, y   : INTEGER) : INTEGER;

BEGIN
  IF    x = 0  THEN  RETURN  y + 1
  ELSIF y = 0  THEN  RETURN  Ackerman (x - 1 , 1)
  ELSE
    RETURN  Ackerman (x - 1 , Ackerman (x , y - 1))
  END
END Ackerman;

BEGIN
  FOR  m := 0  TO  3  DO
    FOR  n := 0  TO  6  DO
      Out.Int (Ackerman (m, n), 10);
      Out.Char (9X)
    END;
    Out.Ln
  END;
  Out.Ln
END ackerman.

  

You may also check:How to resolve the algorithm Partition function P step by step in the Rust programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Arturo programming language
You may also check:How to resolve the algorithm Minesweeper game step by step in the Julia programming language
You may also check:How to resolve the algorithm Long year step by step in the Draco programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the PL/M programming language