Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ackermann Function
#1
The Ackermann function, but the program crashes as soon as one enter "ackermann(4, 1)". Why?  Huh

The result of (4, 1) is 65533, which is in range. The program crashes, both in QB64 and in C (GCC - WinGW 11.02). 

Code: (Select All)
'Ackermann Funktion - 15. Juli 2022
'Absturz schon bei 4, 1 = 65533 (?)

Option _Explicit

Declare Function ackermann(m as Integer, n as Integer) as Long

Dim m, n As Long
Dim i, j As Integer

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 Integer, n As Integer)
  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

[Image: Ackermann-Absturz2022-07-15.jpg]
Reply
#2
(07-15-2022, 04:50 PM)Kernelpanic Wrote: The Ackermann function, but the program crashes as soon as one enter "ackermann(4, 1)". Why?  Huh

The result of (4, 1) is 65533, which is in range. The program crashes, both in QB64 and in C (GCC - WinGW 11.02). 

Because the Integer Range is +- 32K and change. Try _Unsigned Long or _Unsigned _Integer64 IF the numbers and calculations with them are Always Positive (no big variable subtractions) that will get you the greatest upper limit.

Of what use are the Ackermann numbers?
b = b + ...
Reply
#3
Quote:Because the Integer Range is +- 32K and change. Try _Unsigned Long or _Unsigned _Integer64 IF the numbers and calculations with them are Always Positive (no big variable subtractions) that will get you the greatest upper limit.

Of what use are the Ackermann numbers?


The number 65533 cannot be outside of "Long" because "Long" has a range from: LONG integer values range from -2147483648 to 2147483647

It's like Fibonacci numbers: interesting exercises. In addition, these numbers play an important role in biology.

The Ackermann function it is about the time. That early used to work, I wrote the program in QuickC about 25 years ago. At that time you could still measure the time with simple numbers, but not anymore today, because small numbers are too fast.

Ackermannfunktion


It doesn't work with "_Integer64" neither. The worm is in there.
[Image: Ackermann-Int64-2022-07-15.jpg]
Reply
#4
No, it can't be because of "Long".

Ackermann(3, 11) - (3, 12) is out of range.
[Image: Ackerrmann-3-11-2022-07-16.jpg]
Reply
#5
Looks like you are getting a stack overflow due to the depth of the recursion.
Reply
#6
It is a recursive problem. I made everything _Unsigned _integer64 and got the little circle swirl before QB64 bugs out.
b = b + ...
Reply
#7
(07-15-2022, 10:48 PM)bplus Wrote: It is a recursive problem. I made everything _Unsigned _integer64 and got the little circle swirl before QB64 bugs out.

What and which? All variables? Please, show the source code. I can't.
Reply
#8
(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).
Reply
#9
Consolation prize: FreeBasic can't do any better! http://www.rosettacode.org/wiki/Ackerman...#FreeBASIC

Code: (Select All)
_Define A-Z As _UNSIGNED _INTEGER64
Print
Print "Ackermann"
Print
For m = 0 To 4
    For n = 0 To 10
        If m = 4 And n > 0 Then Exit For 'bug out this is as far as FreeBasic gets at RC
        Print Using "######"; ackermann(m, n);
    Next
    Print
Next

Function ackermann (m, n)
    If m = 0 Then
        ackermann = n + 1
    ElseIf m > 0 And n = 0 Then
        ackermann = ackermann(m - 1, 1)
    ElseIf m > 0 And n > 0 Then
        ackermann = ackermann(m - 1, ackermann(m, n - 1))
    End If
End Function


Attached Files Image(s)
   
b = b + ...
Reply
#10
Quote:Consolation prize: FreeBasic can't do any better! http://www.rosettacode.org/wiki/Ackerman...#FreeBASIC


Thanks!

"C" can it: Only "int" -> Ackermann Funktion

. . . but by myself the same problem. Code & screenshot.

Code: (Select All)
//Ackermann Funktion rekursiv - 15. Juli 2022
//Absturz schon bei 4, 1 = 65533 (?)

#include <stdio.h>
#include <stdlib.h>

long int ackermann(int m, int n);

int main(void)
{
  int m, n;
  printf("\nDemonstriert die Ackermann Funktion.\n");
    printf("\nGeben Sie zwei Zahlen ein a <> b: ");
  scanf("%d %d", &m,&n);
    
    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            //i und j sonst wird nur das Endergebnis ausgegeben
            printf("\nAckerman(%d, %d) ergibt: %ld", i, j, ackermann(i, j));
        }
    }               
  return(0);
}

long 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));
}

[Image: In-C-auch-2022-07-16.jpg]
Reply




Users browsing this thread: 2 Guest(s)