How to resolve the algorithm Ackermann function step by step in the FBSL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ackermann function step by step in the FBSL 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 FBSL programming language

Source code in the fbsl programming language

#APPTYPE CONSOLE

TestAckermann()

PAUSE

SUB TestAckermann()
	FOR DIM m = 0 TO 3
		FOR DIM n = 0 TO 10
			PRINT AckermannF(m, n), " ";
		NEXT
		PRINT
	NEXT
END SUB

FUNCTION AckermannF(m AS INTEGER, n AS INTEGER) AS INTEGER
	IF NOT m THEN RETURN n + 1
	IF NOT n THEN RETURN AckermannA(m - 1, 1)
	RETURN AckermannC(m - 1, AckermannF(m, n - 1))
END FUNCTION

DYNC AckermannC(m AS INTEGER, n AS INTEGER) AS INTEGER


	int Ackermann(int m, int n)
	{
		if (!m) return n + 1;
		if (!n) return Ackermann(m - 1, 1);
		return Ackermann(m - 1, Ackermann(m, n - 1));
	}
	
	int main(int m, int n)
	{
		return Ackermann(m, n);
	}


END DYNC

DYNASM AckermannA(m AS INTEGER, n AS INTEGER) AS INTEGER


	ENTER 0, 0
	INVOKE Ackermann, m, n
	LEAVE
	RET
	
	@Ackermann
	ENTER 0, 0

	.IF DWORD PTR [m] .THEN
		JMP @F
	.ENDIF
	MOV EAX, n
	INC EAX
	JMP xit

	@@
	.IF DWORD PTR [n] .THEN
		JMP @F
	.ENDIF
	MOV EAX, m
	DEC EAX
	INVOKE Ackermann, EAX, 1
	JMP xit

	@@
	MOV EAX, n
	DEC EAX
	INVOKE Ackermann, m, EAX
	MOV ECX, m
	DEC ECX
	INVOKE Ackermann, ECX, EAX
	
	@xit
	LEAVE
	RET 8


END DYNASM


  

You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the Ring programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Sidef programming language
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the PicoLisp programming language