Michael Wirth presents in the The Craft of Coding Blog a Fortran function to compute the Ackermann function. Using it in
module ack_mod
implicit none
private
public :: Ackermann
contains
recursive function Ackermann(m,n) result(a)
integer, intent(in) :: m,n
integer :: a
if (m==0) then
a = n + 1
else if (n==0) then
a = Ackermann(m-1, 1)
else
a = Ackermann(m-1, Ackermann(m, n-1))
end if
end function ackermann
end module ack_mod
program main
use ack_mod, only: Ackermann
implicit none
integer :: m,n
write (*,"(2a4,a10)") "m","n","Ack(m,n)"
do m=0,4
do n=0,3
if (m < 4 .or. n < 2) write (*,"(2i4,i10)") m,n,Ackermann(m,n)
end do
end do
end program main
with the default compiler options for Intel, gfortran and g95 gives, matching Wikipedia,
m n Ack(m,n)
0 0 1
0 1 2
0 2 3
0 3 4
1 0 2
1 1 3
1 2 4
1 3 5
2 0 3
2 1 5
2 2 7
2 3 9
3 0 5
3 1 13
3 2 29
3 3 61
4 0 13
For g95, after giving the above output the program says
Exception: Stack overflow
At line 26 of file ackermann.f90 (Unit 6)
Traceback: not available, compile with -ftrace=frame or -ftrace=full
but the Intel and gfortran programs end silently. It’s better to get an error message when a program does not end normally. Are there options to get such a message?
Compiling with ifort /F512000000 ackermann.f90
ifort is able to compute A(4,1) = 65533. Is there a gfortran option that enables the calculation of A(4,1)?