"Fossies" - the Fresh Open Source Software Archive 
Member "FreeBASIC-1.09.0-win64/examples/manual/proguide/recursion_iteration/ackermann.bas" (1 Jan 2022, 1640 Bytes) of package /windows/misc/FreeBASIC-1.09.0-win64.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Visual Basic source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
1 '' examples/manual/proguide/recursion_iteration/ackermann.bas
2 ''
3 '' NOTICE: This file is part of the FreeBASIC Compiler package and can't
4 '' be included in other distributions without authorization.
5 ''
6 '' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgRecursionIteration
7 '' --------
8
9 Function recursiveAckermann (ByVal m As Integer, ByVal n As Integer) As Integer
10 If m = 0 Then
11 Return n + 1
12 Else
13 If n = 0 Then
14 Return recursiveAckermann(m - 1, 1)
15 Else
16 Return recursiveAckermann(m - 1, recursiveAckermann(m, n - 1))
17 End If
18 End If
19 End Function
20
21 #include "DynamicUserStackTypeCreateMacro.bi"
22 DynamicUserStackTypeCreate(DynamicUserStackTypeForInteger, Integer)
23
24 Function iterativeAckermann (ByVal m As Integer, ByVal n As Integer) As Integer
25 Dim As DynamicUserStackTypeForInteger Sm, Sn
26 Sm.push = m : Sn.push = n
27 While Sm.used > 0
28 m = Sm.pop : n = Sn.pop
29 If m = 0 Then
30 Sn.push = n + 1 ' Return n + 1 (and because of nested call)
31 Else
32 If n = 0 Then
33 Sm.push = m - 1 : Sn.push = 1 ' Return Ackermann(m - 1, 1)
34 Else
35 Sm.push = m - 1 : Sm.push = m : Sn.push = n - 1 ' Return Ackermann(m - 1, Ackermann(m, n - 1))
36 End If
37 End If
38 Wend
39 Return Sn.pop ' (because of Sn.push = n + 1)
40 End Function
41
42
43
44 Print recursiveAckermann(3, 0), recursiveAckermann(3, 1), recursiveAckermann(3, 2), recursiveAckermann(3, 3), recursiveAckermann(3, 4)
45 Print iterativeAckermann(3, 0), iterativeAckermann(3, 1), iterativeAckermann(3, 2), iterativeAckermann(3, 3), iterativeAckermann(3, 4)
46
47 Sleep
48