RosettaCodeData/Task/Ackermann-function/Phix/ackermann-function.phix

38 lines
936 B
Plaintext

--
-- Ackermann.exw
-- =============
--
-- optimised. still no bignum library, so ack(4,2), which is power(2,65536)-3, which is
-- apparently 19729 digits, and any above, are beyond (the CPU/FPU hardware) and this.
-- (replaced ack(atom,atom) with ack(int,int) since the former fares no better.)
function ack(integer m, integer n)
if m=0 then
return n+1
elsif m=1 then
return n+2
elsif m=2 then
return 2*n+3
elsif m=3 then
return power(2,n+3)-3
elsif m>0 and n=0 then
return ack(m-1,1)
else
return ack(m-1,ack(m,n-1))
end if
end function
procedure Ackermann()
for i=0 to 3 do
for j=0 to 10 do
printf(1,"%5d",ack(i,j))
end for
puts(1,"\n")
end for
printf(1,"ack(4,1) %5d\n",ack(4,1))
-- printf(1,"ack(4,2) %5d\n",ack(4,2)) -- power function overflow
if getc(0) then end if
end procedure
Ackermann()