How to resolve the algorithm Sudan function step by step in the Pascal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sudan function step by step in the Pascal programming language

Table of Contents

Problem Statement

The Sudan function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. This is also true of the better-known Ackermann function. The Sudan function was the first function having this property to be published. The Sudan function is usually defined as follows (svg):

F

0

( x , y )

= x + y

F

n + 1

( x , 0 )

= x

if

n ≥ 0

F

n + 1

( x , y + 1 )

=

F

n

(

F

n + 1

( x , y ) ,

F

n + 1

( x , y ) + y + 1 )

if

n ≥ 0

{\displaystyle {\begin{array}{lll}F_{0}(x,y)&=x+y\F_{n+1}(x,0)&=x&{\text{if }}n\geq 0\F_{n+1}(x,y+1)&=F_{n}(F_{n+1}(x,y),F_{n+1}(x,y)+y+1)&{\text{if }}n\geq 0\\end{array}}}

Write a function which returns the value of F(x, y).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sudan function step by step in the Pascal programming language

Source code in the pascal programming language

program Sudan;
//trans  https://rosettacode.org/wiki/Sudan_function#Delphi
{$IFDEF FPC} {$MODE DELPHI} {$OPTIMIZATION ON,ALL}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
  sysutils;
function SudanFunction(N,X,Y: UInt64): UInt64;
begin
  if n = 0 then 
    Result:=X + Y
   else
     if y = 0 then 
       Result:=X
     else 
       Result:=SudanFunction(N - 1, SudanFunction(N, X, Y - 1), SudanFunction(N, X, Y - 1) + Y);
end;

procedure ShowSudanFunction(N,X,Y: UInt64);
begin
  writeln(Format('Sudan(%d,%d,%d)=%d',[n,x,y,SudanFunction(N,X,Y)]));
end;

procedure ShowSudanFunctions;
var 
  N,X,Y: UInt64;
  S: string;
begin
for N:=0 to 1 do
	begin
	writeln(Format('Sudan(%d,X,Y)',[N]));
	writeln('Y/X    0   1   2   3   4   5');
	writeln('----------------------------');
	for Y:=0 to 6 do
		begin
		S:=Format('%2d | ',[Y]);
		for X:=0 to 5 do
			begin
			S:=S+Format('%3d ',[SudanFunction(N,X,Y)]);
			end;
		writeln(S);
		end;
	writeln('');
	end;
end;	

BEGIN
  ShowSudanFunctions;
  ShowSudanFunction( 1, 3, 3);
  ShowSudanFunction( 2, 1, 1);
  ShowSudanFunction( 2, 2, 1);
  ShowSudanFunction( 2, 1, 2);  
  ShowSudanFunction( 3, 1, 1);  
  ShowSudanFunction( 2, 2, 2);
END.


  

You may also check:How to resolve the algorithm One-time pad step by step in the Julia programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Racket programming language
You may also check:How to resolve the algorithm Selectively replace multiple instances of a character within a string step by step in the RPL programming language
You may also check:How to resolve the algorithm Combinations step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Action! programming language