Ackermann Function
#17
Quote:The original program certainly runs into a stack overflow due to the use of the recursive function.

I was able to calculate a(4,1)  using the following code adapted from the basic256 version as that doesn't have recursive functions built in. I takes a couple minutes to calculate but it works. I have no idea if it can calculate A(4,2).


The program works great! I've tried adding a timer, but the damn thing do not want. I assume that has to do with "Gosub". - I tried it out for two hours. 

The stack overflow can't be true. Here's a page that computes A(4, 1) in no time. One would have to be able to see the source code for it.

Ackermann function

Code: (Select All)
'Ackermann Funktion von Basic256 angepasst James D. Jarvis
'Zeitmessung funktioniert nicht
'16. Juli 2022

Option _Explicit

Dim stack(50000000, 3) As _Integer64
Dim lev, a, b As _Integer64
Dim t_start As Single

Input "1st #"; a
Input "2nd #"; b

'Zeitmessung starten
t_start = Timer

stack(0, 0) = a
stack(0, 1) = b
lev = 0

GoSub ackermann
Print "A("; stack(0, 0); ","; stack(0, 1); ") ="; stack(0, 2)
End

ackermann:
If stack(lev, 0) = 0 Then
  stack(lev, 2) = stack(lev, 1) + 1
  Return
End If

If stack(lev, 1) = 0 Then
  lev = lev + 1
  stack(lev, 0) = stack(lev - 1, 0) - 1
  stack(lev, 1) = 1
  GoSub ackermann

  stack(lev - 1, 2) = stack(lev, 2)
  lev = lev - 1
  Return
End If

lev = lev + 1
stack(lev, 0) = stack(lev - 1, 0)
stack(lev, 1) = stack(lev - 1, 1) - 1
GoSub ackermann

stack(lev, 0) = stack(lev - 1, 0) - 1
stack(lev, 1) = stack(lev, 2)
GoSub ackermann

stack(lev - 1, 2) = stack(lev, 2)
lev = lev - 1
Return

't_ende = Timer

Print
Print Timer - t_start;
Print " Sekunden."

End
Reply


Messages In This Thread
Ackermann Function - by Kernelpanic - 07-15-2022, 04:50 PM
RE: Ackermann Function - by bplus - 07-15-2022, 05:59 PM
RE: Ackermann Function - by Kernelpanic - 07-15-2022, 09:18 PM
RE: Ackermann Function - by Kernelpanic - 07-15-2022, 10:29 PM
RE: Ackermann Function - by James D Jarvis - 07-15-2022, 10:40 PM
RE: Ackermann Function - by Kernelpanic - 07-15-2022, 11:19 PM
RE: Ackermann Function - by James D Jarvis - 07-15-2022, 11:57 PM
RE: Ackermann Function - by bplus - 07-15-2022, 10:48 PM
RE: Ackermann Function - by Kernelpanic - 07-15-2022, 11:15 PM
RE: Ackermann Function - by bplus - 07-15-2022, 11:26 PM
RE: Ackermann Function - by Kernelpanic - 07-15-2022, 11:45 PM
RE: Ackermann Function - by Kernelpanic - 07-16-2022, 12:16 AM
RE: Ackermann Function - by James D Jarvis - 07-16-2022, 12:39 AM
RE: Ackermann Function - by bplus - 07-16-2022, 01:02 AM
RE: Ackermann Function - by Kernelpanic - 07-16-2022, 08:39 PM
RE: Ackermann Function - by James D Jarvis - 07-16-2022, 03:09 PM
RE: Ackermann Function - by bplus - 07-16-2022, 04:38 PM
RE: Ackermann Function - by Kernelpanic - 07-16-2022, 08:29 PM
RE: Ackermann Function - by Jack - 07-16-2022, 10:16 PM
RE: Ackermann Function - by Kernelpanic - 07-16-2022, 10:52 PM
RE: Ackermann Function - by Jack - 07-16-2022, 11:47 PM
RE: Ackermann Function - by Kernelpanic - 07-19-2022, 02:44 PM
RE: Ackermann Function - by Jack - 07-19-2022, 05:29 PM
RE: Ackermann Function - by Kernelpanic - 07-19-2022, 08:43 PM
RE: Ackermann Function - by Jack - 07-19-2022, 10:05 PM
RE: Ackermann Function - by Kernelpanic - 07-19-2022, 10:53 PM
RE: Ackermann Function - by Jack - 07-19-2022, 11:23 PM
RE: Ackermann Function - by Jack - 07-19-2022, 11:40 PM
RE: Ackermann Function - by Kernelpanic - 07-20-2022, 12:44 AM
RE: Ackermann Function - by Jack - 07-20-2022, 12:52 AM
RE: Ackermann Function - by Kernelpanic - 07-20-2022, 11:40 AM
RE: Ackermann Function - by Kernelpanic - 07-20-2022, 08:44 PM



Users browsing this thread: 11 Guest(s)