Ackermann
Posted: Fri May 08, 2015 9:42 am
I have made a test- and demo-program for the Ackermaan-function.
That function is often used used as a benchmark of a compiler's ability to optimize recursion.
See also http://en.m.wikipedia.org/wiki/Ackermann_function
Due to the deep recursion, Smart Basic will crash for higher values.
E.g it crashes at calculating the values of A(3,10) and A(4,1) on iPad Air and iOS 8.3
The calculation of A(3,9) took 386 seconds after 1.11644E+07 function calls in recursion.
Incredibly deep! Well done Mr. K
The values are written to file until the app crashes.
Screenshots of the running program and the file "Ackkermann(3,n).txt" are attached.
In the editor in the section marked in red the value of m should be changed for generating different files.
That function is often used used as a benchmark of a compiler's ability to optimize recursion.
See also http://en.m.wikipedia.org/wiki/Ackermann_function
Due to the deep recursion, Smart Basic will crash for higher values.
E.g it crashes at calculating the values of A(3,10) and A(4,1) on iPad Air and iOS 8.3
The calculation of A(3,9) took 386 seconds after 1.11644E+07 function calls in recursion.
Incredibly deep! Well done Mr. K
The values are written to file until the app crashes.
Screenshots of the running program and the file "Ackkermann(3,n).txt" are attached.
In the editor in the section marked in red the value of m should be changed for generating different files.
Code: Select all
'Ackermann for Smart Basic
'Test and demo-program for Ackkermann-function
'by Dutchman, may 2015
/*
The Ackermann function is a classic example of a recursive function.
It grows very quickly in value, as does the size of its call tree.
Its arguments are never negative and it always terminates.
See also: http://en.m.wikipedia.org/wiki/Ackermann_function
The Ackermann function, due to its definition in terms of extremely deep recursion,
can be used as a benchmark of a compiler's ability to optimize recursion.
*/
SET UNDERGROUND ON ' keep calculating in background
TIME RESET
PRINT "Ackermann-function A(m,n)"
PRINT
'GOTO Crashrun 'decomment to skip first section
'------- make 4x7 (mxn) table
PRINT "The first 4x7 Ackermann's numbers:"
FOR m=0 TO 3
FOR n=0 TO 6
count=0
t=TIME()
PRINT "####":A(m,n);
t=TIME()-t
NEXT n
PRINT
NEXT m
PRINT "Last number ";
GOSUB stats
PRINT
'
crashrun:
'r'--- calculate A(mfixed,n)
'----- and store result in file until crash
m=3 'value for which a series will be calculated
''
m$=STR$(m,"#")
n=0 'startvalue until crash
File$="Ackermann("&m$&",n).txt"
IF FILE_EXISTS(File$) THEN FILE File$ DELETE
FILE File$ PRINT "Function","Value","Runtime","Function calls"
DO
n$=STR$(n,"#")
count=0
t=TIME()
PRINT "A(";m$;",";n$;")";
Amn=A(m,n)
t=TIME()-t
PRINT "=";Amn;
GOSUB Stats
GOSUB Store
PRINT
n+=1
UNTIL crash
END
'
'===== Functions and Subroutines =====
DEF A(m,n)'Ackermann-function
REM m and n should be positive integers
.count+=1 'comment this out for a bit higher speed
IF m=0 THEN RETURN n+1
IF n=0 THEN RETURN A(m-1,1)
RETURN A(m-1,A(m,n-1))
END DEF
'
Store:
FILE File$ PRINT "A("&m$&","&n$&")",Amn,F$:t,count
RETURN
'
Stats:
IF t>0.1 THEN ! F$="#.#" ! ELSE ! F$="#.###"
ENDIF
PRINT "in ";F$:t;"seconds";
'print count if it is not commented out
IF count>0 THEN
PRINT " and";count;"function calls."
ELSE
PRINT "."
ENDIF
RETURN