07-15-2022, 11:57 PM
(07-15-2022, 11:19 PM)Kernelpanic Wrote:(07-15-2022, 10:40 PM)James D Jarvis Wrote: Looks like you are getting a stack overflow due to the depth of the recursion.
Yes, but the overflow should not occur at A(4, 1).
The program crashes sometime after 70,609,342(but less than 70,899,999,999) recursive calls to the function. The result itself may well be within the limits of the data type but the number of recursive calls required to get there is extremely high according to the logic used.
Here's how I found that number of recursive calls.
Code: (Select All)
'Ackermann Funktion - 15. Juli 2022
'Absturz schon bei 4, 1 = 65533 (?)
Option _Explicit
Declare Function ackermann(m as long, n as long) as long
Dim m, n As Long
Dim i, j As Long
Dim Shared c As Long
Dim Shared aa$, ask$
Print
Print "Ackermann Funktion - Geben Sie zwei Zahlen ein"
Print
Input "Zahl 1: ", m
Input "Zahl 2: ", n
Print
i = 0: j = 0
For i = 0 To m
For j = 0 To n
Print Using "Ackermann (#, #) = ####"; i, j, ackermann(i, j)
Next j
Next i
End
Function ackermann (m As Long, n As Long)
c = c + 1
Print c
aa$ = InKey$
If aa$ <> "" Then
Input "quit?"; ask$
If ask$ = "y" Then End
End If
If m = 0 Then ackermann = n + 1
If m > 0 And n = 0 Then
ackermann = ackermann(m - 1, 1)
End If
If m > 0 And n > 0 Then
ackermann = ackermann(m - 1, ackermann(m, n - 1))
End If
End Function