07-15-2022, 05:59 PM (This post was last modified: 07-15-2022, 06:01 PM by bplus.)
(07-15-2022, 04:50 PM)Kernelpanic Wrote: The Ackermann function, but the program crashes as soon as one enter "ackermann(4, 1)". Why?
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.
07-15-2022, 09:18 PM (This post was last modified: 07-15-2022, 09:19 PM by Kernelpanic.)
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.
(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.
_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
. . . 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));
}