Ackermann function: Difference between revisions

added RPL
(added RPL)
(47 intermediate revisions by 27 users not shown)
Line 34:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F ack2(m, n) -> Int
R I m == 0 {(n + 1)
} E I m == 1 {(n + 2)
Line 44:
print(ack2(0, 0))
print(ack2(3, 4))
print(ack2(4, 1))</langsyntaxhighlight>
 
{{out}}
Line 57:
The OS/360 linkage is a bit tricky with the S/360 basic instruction set.
To simplify, the program is recursive not reentrant.
<langsyntaxhighlight lang="360asm">* Ackermann function 07/09/2015
&LAB XDECO &REG,&TARGET
.*-----------------------------------------------------------------*
Line 155:
STACKLEN EQU *-STACK
YREGS
END ACKERMAN</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">
Line 199:
This implementation is based on the code shown in the computerphile episode in the youtube link at the top of this page (time index 5:00).
 
<langsyntaxhighlight lang="68000devpac">;
; Ackermann function for Motorola 68000 under AmigaOs 2+ by Thorham
;
Line 310:
 
outString
dc.b "ackermann (%ld,%ld) is: %ld",10,0</langsyntaxhighlight>
 
=={{header|8080 Assembly}}==
Line 317:
and <code>n ∊ [0,9)</code>, on a real 8080 this takes a little over two minutes.
 
<langsyntaxhighlight lang="8080asm"> org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 397:
db '*****' ; Placeholder for number
pnum: db 9,'$'
nl: db 13,10,'$'</langsyntaxhighlight>
 
{{out}}
Line 410:
This code does 16-bit math just like the 8080 version.
 
<langsyntaxhighlight lang="asm"> cpu 8086
bits 16
org 100h
Line 469:
db '*****' ; Placeholder for ASCII number
pnum: db 9,'$'
nl: db 13,10,'$'</langsyntaxhighlight>
 
{{out}}
Line 479:
 
=={{header|8th}}==
<syntaxhighlight lang="forth">
<lang Forth>
\ Ackermann function, illustrating use of "memoization".
 
Line 554:
4 1 ackOf
 
bye</langsyntaxhighlight>
 
{{out|The output}}
Line 571:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program ackermann64.s */
Line 693:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 739:
=={{header|ABAP}}==
 
<syntaxhighlight lang="abap">
<lang ABAP>
REPORT zhuberv_ackermann.
 
Line 785:
lv_result = zcl_ackermann=>ackermann( m = pa_m n = pa_n ).
WRITE: / lv_result.
</syntaxhighlight>
</lang>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN m ack n:
SELECT:
m=0: RETURN n+1
m>0 AND n=0: RETURN (m-1) ack 1
m>0 AND n>0: RETURN (m-1) ack (m ack (n-1))
 
FOR m IN {0..3}:
FOR n IN {0..8}:
WRITE (m ack n)>>6
WRITE /</syntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 10
3 5 7 9 11 13 15 17 19
5 13 29 61 125 253 509 1021 2045</pre>
 
=={{header|Acornsoft Lisp}}==
{{trans|Common Lisp}}
 
<syntaxhighlight lang="lisp">
(defun ack (m n)
(cond ((zerop m) (add1 n))
((zerop n) (ack (sub1 m) 1))
(t (ack (sub1 m) (ack m (sub1 n))))))
</syntaxhighlight>
 
{{Out}}
 
<pre>
Evaluate : (ack 3 5)
 
Value is : 253
</pre>
 
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<langsyntaxhighlight Actionlang="action!">DEFINE MAXSIZE="1000"
CARD ARRAY stack(MAXSIZE)
CARD stacksize=[0]
Line 845 ⟶ 880:
OD
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ackermann_function.png Screenshot from Atari 8-bit computer]
Line 872 ⟶ 907:
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">public function ackermann(m:uint, n:uint):uint
{
if (m == 0)
Line 884 ⟶ 919:
 
return ackermann(m - 1, ackermann(m, n - 1));
}</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Test_Ackermann is
Line 907 ⟶ 942:
New_Line;
end loop;
end Test_Ackermann;</langsyntaxhighlight>
The implementation does not care about arbitrary precision numbers because the Ackermann function does not only grow, but also slow quickly, when computed recursively.
{{out}} the first 4x7 Ackermann's numbers:
Line 916 ⟶ 951:
 
=={{header|Agda}}==
{{works with|Agda|2.56.23}}
{{libheader|agda-stdlib v0v1.137}}
<langsyntaxhighlight lang="agda">
open import Data.Nat
open import Data.Nat.Show
open import IO
module Ackermann where
 
open import Data.Nat using (ℕ ; zero ; suc ; _+_)
ack : ℕ -> ℕ -> ℕ
 
ack : ℕ → ℕ → ℕ
ack zero n = n + 1
ack (suc m) zero = ack m 1
ack (suc m) (suc n) = ack m (ack (suc m) n)
main = run (putStrLn (show (ack 3 9)))
</lang>
 
open import Agda.Builtin.IO using (IO)
Note the unicode ℕ characters, they can be input in emacs agda mode using "\bN". Running in bash:
open import Agda.Builtin.Unit using (⊤)
open import Agda.Builtin.String using (String)
open import Data.Nat.Show using (show)
 
postulate putStrLn : String → IO ⊤
{-# FOREIGN GHC import qualified Data.Text as T #-}
{-# COMPILE GHC putStrLn = putStrLn . T.unpack #-}
 
main : IO ⊤
main = putStrLn (show (ack 3 9))
 
 
-- Output:
-- 4093
</syntaxhighlight>
 
The Unicode characters can be entered in Emacs Agda as follows:
* ℕ : \bN
* → : \to
* ⊤ : \top
 
Running in Bash:
 
<langsyntaxhighlight lang="bash">
agda --compile Ackermann.agda
./Ackermann
</syntaxhighlight>
</lang>
 
{{out}}
Line 947 ⟶ 1,000:
=={{header|ALGOL 60}}==
{{works with|A60}}
<langsyntaxhighlight lang="algol60">begin
integer procedure ackermann(m,n);value m,n;integer m,n;
ackermann:=if m=0 then n+1
Line 958 ⟶ 1,011:
outstring(1,"\n")
end
end </langsyntaxhighlight>
{{out}}
<pre>
Line 972 ⟶ 1,025:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<langsyntaxhighlight lang="algol68">PROC test ackermann = VOID:
BEGIN
PROC ackermann = (INT m, n)INT:
Line 992 ⟶ 1,045:
OD
END # test ackermann #;
test ackermann</langsyntaxhighlight>
{{out}}
<pre>
Line 1,003 ⟶ 1,056:
=={{header|ALGOL W}}==
{{Trans|ALGOL 60}}
<langsyntaxhighlight lang="algolw">begin
integer procedure ackermann( integer value m,n ) ;
if m=0 then n+1
Line 1,012 ⟶ 1,065:
for n := 1 until 6 do writeon( ackermann( m, n ) );
end for_m
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,023 ⟶ 1,076:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">ackermann←{
0=1⊃⍵:1+2⊃⍵
0=2⊃⍵:∇(¯1+1⊃⍵)1
∇(¯1+1⊃⍵),∇(1⊃⍵),¯1+2⊃⍵
}</langsyntaxhighlight>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on ackermann(m, n)
 
<lang AppleScript>on ackermann(m, n)
if m is equal to 0 then return n + 1
if n is equal to 0 then return ackermann(m - 1, 1)
return ackermann(m - 1, ackermann(m, n - 1))
end ackermann</langsyntaxhighlight>
 
=={{header|Argile}}==
{{works with|Argile|1.0.0}}
<langsyntaxhighlight Argilelang="argile">use std
 
for each (val nat n) from 0 to 6
Line 1,048 ⟶ 1,100:
return (n+1) if m == 0
return (A (m - 1) 1) if n == 0
A (m - 1) (A m (n - 1))</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI or android 32 bits */
/* program ackermann.s */
Line 1,171 ⟶ 1,224:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,217 ⟶ 1,270:
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">ackermann: function [m,n][
 
<lang rebol>ackermann: function [m,n][
(m=0)? -> n+1 [
(n=0)? -> ackermann m-1 1
Line 1,229 ⟶ 1,281:
print ["ackermann" a b "=>" ackermann a b]
]
]</langsyntaxhighlight>
 
{{out}}
 
<pre>ackermann 0 0 => 1
ackermann 0 1 => 2
Line 1,255 ⟶ 1,305:
 
=={{header|ATS}}==
<langsyntaxhighlight ATSlang="ats">fun ackermann
{m,n:nat} .<m,n>.
(m: int m, n: int n): Nat =
Line 1,262 ⟶ 1,312:
| (_, 0) =>> ackermann (m-1, 1)
| (_, _) =>> ackermann (m-1, ackermann (m, n-1))
// end of [ackermann]</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">A(m, n) {
If (m > 0) && (n = 0)
Return A(m-1,1)
Line 1,275 ⟶ 1,325:
 
; Example:
MsgBox, % "A(1,2) = " A(1,2)</langsyntaxhighlight>
 
=={{header|AutoIt}}==
=== Classical version ===
<langsyntaxhighlight lang="autoit">Func Ackermann($m, $n)
If ($m = 0) Then
Return $n+1
Line 1,289 ⟶ 1,339:
EndIf
EndIf
EndFunc</langsyntaxhighlight>
 
=== Classical + cache implementation ===
Line 1,295 ⟶ 1,345:
This version works way faster than the classical one: Ackermann(3, 5) runs in 12,7 ms, while the classical version takes 402,2 ms.
 
<langsyntaxhighlight lang="autoit">Global $ackermann[2047][2047] ; Set the size to whatever you want
Func Ackermann($m, $n)
If ($ackermann[$m][$n] <> 0) Then
Line 1,312 ⟶ 1,362:
Return $return
EndIf
EndFunc ;==>Ackermann</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">function ackermann(m, n)
{
if ( m == 0 ) {
Line 1,332 ⟶ 1,382:
}
}
}</langsyntaxhighlight>
 
=={{header|Babel}}==
<langsyntaxhighlight lang="babel">main:
{((0 0) (0 1) (0 2)
(0 3) (0 4) (1 0)
Line 1,366 ⟶ 1,416:
if }
 
zero?!: { 0 = }</syntaxhighlight>
</lang>
{{out}}
<pre>A(0 0 ) = 1
A(0 0 ) = 1
A(0 1 ) = 2
A(0 2 ) = 3
Line 1,391 ⟶ 1,439:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
<lang basic>100 DIM R%(2900),M(2900),N(2900)
<syntaxhighlight lang="basic">100 DIM R%(2900),M(2900),N(2900)
110 FOR M = 0 TO 3
120 FOR N = 0 TO 4
Line 1,414 ⟶ 1,463:
250 M = M(SP) : N = N(SP) : IF SP = 0 THEN RETURN
260 FOR SP = SP - 1 TO 0 STEP -1 : IF R%(SP) THEN M = M(SP) : N = N(SP) : NEXT SP : SP = 0 : RETURN
270 M = M - 1 : N = ACK : R%(SP) = 1 : SP = SP + 1 : GOTO 200</langsyntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="basic256">dim stack(5000, 3) # BASIC-256 lacks functions (as of ver. 0.9.6.66)
stack[0,0] = 3 # M
stack[0,1] = 7 # N
Line 1,449 ⟶ 1,498:
stack[lev-1,2] = stack[lev,2]
lev = lev-1
return</langsyntaxhighlight>
{{out}}
<pre> A(3,7) = 1021</pre>
<pre>
A(3,7) = 1021
</pre>
 
<langsyntaxhighlight lang="basic256"># BASIC256 since 0.9.9.1 supports functions
for m = 0 to 3
for n = 0 to 4
Line 1,473 ⟶ 1,520:
endif
end if
end function</langsyntaxhighlight>
 
{{out}}
<pre>0 0 1
0 0 1
0 1 2
0 2 3
Line 1,499 ⟶ 1,544:
 
==={{header|BBC BASIC}}===
<langsyntaxhighlight lang="bbcbasic"> PRINT FNackermann(3, 7)
END
Line 1,505 ⟶ 1,550:
IF M% = 0 THEN = N% + 1
IF N% = 0 THEN = FNackermann(M% - 1, 1)
= FNackermann(M% - 1, FNackermann(M%, N%-1))</langsyntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 for m = 0 to 4
110 print using "###";m;
120 for n = 0 to 6
130 if m = 4 and n = 1 then goto 160
140 print using "######";ack(m,n);
150 next n
160 print
170 next m
180 end
190 sub ack(m,n)
200 if m = 0 then ack = n+1
210 if m > 0 and n = 0 then ack = ack(m-1,1)
220 if m > 0 and n > 0 then ack = ack(m-1,ack(m,n-1))
230 end sub</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION ack(m, n)
IF m = 0 THEN LET ack = n+1
IF m > 0 AND n = 0 THEN LET ack = ack(m-1, 1)
IF m > 0 AND n > 0 THEN LET ack = ack(m-1, ack(m, n-1))
END FUNCTION
 
FOR m = 0 TO 4
PRINT USING "###": m;
FOR n = 0 TO 8
! A(4, 1) OR higher will RUN OUT of stack memory (default 1M)
! change n = 1 TO n = 2 TO calculate A(4, 2), increase stack!
IF m = 4 AND n = 1 THEN EXIT FOR
PRINT USING "######": ack(m, n);
NEXT n
PRINT
NEXT m
 
END</syntaxhighlight>
 
==={{header|QuickBasic}}===
Line 1,511 ⟶ 1,593:
BASIC runs out of stack space very quickly.
The call <tt>ack(3, 4)</tt> gives a stack error.
<langsyntaxhighlight lang="qbasic">DECLARE FUNCTION ack! (m!, n!)
 
FUNCTION ack (m!, n!)
Line 1,522 ⟶ 1,604:
ack = ack(m - 1, ack(m, n - 1))
END IF
END FUNCTION</langsyntaxhighlight>
 
=={{header|Batch File}}==
Had trouble with this, so called in the gurus at [http://stackoverflow.com/questions/2680668/what-is-wrong-with-this-recursive-windows-cmd-script-it-wont-do-ackermann-prope StackOverflow]. Thanks to Patrick Cuff for pointing out where I was going wrong.
<langsyntaxhighlight lang="dos">::Ackermann.cmd
@echo off
set depth=0
Line 1,556 ⟶ 1,638:
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )</langsyntaxhighlight>
Because of the <code>exit</code> statements, running this bare closes one's shell, so this test routine handles the calling of Ackermann.cmd
<langsyntaxhighlight lang="dos">::Ack.cmd
@echo off
cmd/c ackermann.cmd %1 %2
echo Ackermann(%1, %2)=%errorlevel%</langsyntaxhighlight>
A few test runs:
<pre>D:\Documents and Settings\Bruce>ack 0 4
Line 1,579 ⟶ 1,661:
{{works with|OpenBSD bc}}
{{Works with|GNU bc}}
<langsyntaxhighlight lang="bc">define ack(m, n) {
if ( m == 0 ) return (n+1);
if ( n == 0 ) return (ack(m-1, 1));
Line 1,590 ⟶ 1,672:
}
}
quit</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight BCPLlang="bcpl">GET "libhdr"
 
LET ack(m, n) = m=0 -> n+1,
Line 1,603 ⟶ 1,685:
writef("ack(%n, %n) = %n*n", m, n, ack(m,n))
RESULTIS 0
}</langsyntaxhighlight>
 
=={{header|beeswax}}==
Line 1,609 ⟶ 1,691:
Iterative slow version:
 
<langsyntaxhighlight lang="beeswax">
>M?f@h@gMf@h3yzp if m>0 and n>0 => replace m,n with m-1,m,n-1
>@h@g'b?1f@h@gM?f@hzp if m>0 and n=0 => replace m,n with m-1,1
Line 1,615 ⟶ 1,697:
>I;
b < <
</syntaxhighlight>
</lang>
 
A functional and recursive realization of the version above. Functions are realized by direct calls of functions via jumps (instruction <code>J</code>) to the entry points of two distinct functions:
Line 1,625 ⟶ 1,707:
Each block of <code>1FJ</code> or <code>1fFJ</code> in the code is a call of the Ackermann function itself.
 
<langsyntaxhighlight lang="beeswax">Ag~1?Lp1~2@g'p?g?Pf1FJ Ackermann function. if m=0 => run Ackermann function (m, n+1)
xI; x@g'p??@Mf1fFJ if m>0 and n=0 => run Ackermann (m-1,1)
xM@~gM??f~f@f1FJ if m>0 and n>0 => run Ackermann(m,Ackermann(m-1,n-1))
_ii1FJ input function. Input m,n, then execute Ackermann(m,n)</langsyntaxhighlight>
 
Highly optimized and fast version, returns A(4,1)/A(5,0) almost instantaneously:
<langsyntaxhighlight lang="beeswax">
>Mf@Ph?@g@2h@Mf@Php if m>4 and n>0 => replace m,n with m-1,m,n-1
>~4~L#1~2hg'd?1f@hgM?f2h p if m>4 and n=0 => replace m,n with m-1,1
Line 1,642 ⟶ 1,724:
z b < < <
d <
</syntaxhighlight>
</lang>
 
Higher values than A(4,1)/(5,0) lead to UInt64 wraparound, support for numbers bigger than 2^64-1 is not implemented in these solutions.
Line 1,651 ⟶ 1,733:
{{trans|ERRE}}
Since Befunge-93 doesn't have recursive capabilities we need to use an iterative algorithm.
<langsyntaxhighlight lang="befunge">&>&>vvg0>#0\#-:#1_1v
@v:\<vp0 0:-1<\+<
^>00p>:#^_$1+\:#^_$.
</syntaxhighlight>
</lang>
 
===Befunge-98===
{{works with|CCBI|2.1}}
<langsyntaxhighlight lang="befunge">r[1&&{0
>v
j
Line 1,665 ⟶ 1,747:
^ v:\_$1+
\^v_$1\1-
u^>1-0fp:1-\0fg101-</langsyntaxhighlight>
The program reads two integers (first m, then n) from command line, idles around funge space, then outputs the result of the Ackerman function.
Since the latter is calculated truly recursively, the execution time becomes unwieldy for most m>3.
 
=={{header|Binary Lambda Calculus}}==
 
The Ackermann function on Church numerals (arbitrary precision), as shown in https://github.com/tromp/AIT/blob/master/fast_growing_and_conjectures/ackermann.lam is the 63 bit BLC
program
 
<pre>010000010110000001010111110101100010110000000011100101111011010</pre>
 
=={{header|BQN}}==
<langsyntaxhighlight BQNlang="bqn">A ← {
A 0‿n: n+1;
A m‿0: A (m-1)‿1;
A m‿n: A (m-1)‿(A m‿(n-1))
}</langsyntaxhighlight>
Example usage:
<syntaxhighlight lang="text"> A 0‿3
4
A 1‿4
Line 1,683 ⟶ 1,772:
11
A 3‿4
125</langsyntaxhighlight>
 
=={{header|Bracmat}}==
Line 1,690 ⟶ 1,779:
The value of A(4,1) cannot be computed due to stack overflow.
It can compute A(3,9) (4093), but probably not A(3,10)
<langsyntaxhighlight lang="bracmat">( Ack
= m n
. !arg:(?m,?n)
Line 1,697 ⟶ 1,786:
| Ack$(!m+-1,Ack$(!m,!n+-1))
)
);</langsyntaxhighlight>
The second version is a purely non-recursive solution that easily can compute A(4,1).
The program uses a stack for Ackermann function calls that are to be evaluated, but that cannot be computed given the currently known function values - the "known unknowns".
Line 1,705 ⟶ 1,794:
 
Although all known values are stored in the hash table, the converse is not true: an element in the hash table is either a "known known" or an "unknown unknown" associated with an "known unknown".
<langsyntaxhighlight lang="bracmat"> ( A
= m n value key eq chain
, find insert future stack va val
Line 1,765 ⟶ 1,854:
& !value
)
& new$hash:?cache</langsyntaxhighlight>
{{out|Some results}}
<pre>
Line 1,774 ⟶ 1,863:
</pre>
The last solution is a recursive solution that employs some extra formulas, inspired by the Common Lisp solution further down.
<langsyntaxhighlight lang="bracmat">( AckFormula
= m n
. !arg:(?m,?n)
Line 1,784 ⟶ 1,873:
| AckFormula$(!m+-1,AckFormula$(!m,!n+-1))
)
)</langsyntaxhighlight>
{{Out|Some results}}
<pre>AckFormula$(4,1):65533
Line 1,792 ⟶ 1,881:
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">ackermann = { m, n |
when { m == 0 } { n + 1 }
{ m > 0 && n == 0 } { ackermann(m - 1, 1) }
Line 1,798 ⟶ 1,887:
}
 
p ackermann 3, 4 #Prints 125</langsyntaxhighlight>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">:import std/Combinator .
:import std/Number/Unary U
:import std/Math .
 
# unary ackermann
ackermann-unary [0 [[U.inc 0 1 (+1u)]] U.inc]
 
:test (ackermann-unary (+0u) (+0u)) ((+1u))
:test (ackermann-unary (+3u) (+4u)) ((+125u))
 
# ternary ackermann (lower space complexity)
ackermann-ternary y [[[=?1 ++0 (=?0 (2 --1 (+1)) (2 --1 (2 1 --0)))]]]
 
:test ((ackermann-ternary (+0) (+0)) =? (+1)) ([[1]])
:test ((ackermann-ternary (+3) (+4)) =? (+125)) ([[1]])</syntaxhighlight>
 
=={{header|C}}==
Straightforward implementation per Ackermann definition:
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
int ackermann(int m, int n)
Line 1,819 ⟶ 1,925:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>A(0, 0) = 1
Line 1,842 ⟶ 1,948:
A(4, 1) = 65533</pre>
Ackermann function makes <i>a lot</i> of recursive calls, so the above program is a bit naive. We need to be slightly less naive, by doing some simple caching:
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,882 ⟶ 1,988:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>A(0, 0) = 1
Line 1,906 ⟶ 2,012:
Whee. Well, with some extra work, we calculated <i>one more</i> n value, big deal, right?
But see, <code>A(4, 2) = A(3, A(4, 1)) = A(3, 65533) = A(2, A(3, 65532)) = ...</code> you can see how fast it blows up. In fact, no amount of caching will help you calculate large m values; on the machine I use A(4, 2) segfaults because the recursions run out of stack space--not a whole lot I can do about it. At least it runs out of stack space <i>quickly</i>, unlike the first solution...
 
A couple of alternative approaches...
<syntaxhighlight lang="c">/* Thejaka Maldeniya */
 
#include <conio.h>
 
unsigned long long HR(unsigned int n, unsigned long long a, unsigned long long b) {
// (Internal) Recursive Hyperfunction: Perform a Hyperoperation...
 
unsigned long long r = 1;
 
while(b--)
r = n - 3 ? HR(n - 1, a, r) : /* Exponentiation */ r * a;
 
return r;
}
 
unsigned long long H(unsigned int n, unsigned long long a, unsigned long long b) {
// Hyperfunction (Recursive-Iterative-O(1) Hybrid): Perform a Hyperoperation...
 
switch(n) {
case 0:
// Increment
return ++b;
case 1:
// Addition
return a + b;
case 2:
// Multiplication
return a * b;
}
 
return HR(n, a, b);
}
 
unsigned long long APH(unsigned int m, unsigned int n) {
// Ackermann-Péter Function (Recursive-Iterative-O(1) Hybrid)
return H(m, 2, n + 3) - 3;
}
 
unsigned long long * p = 0;
 
unsigned long long APRR(unsigned int m, unsigned int n) {
if (!m) return ++n;
 
unsigned long long r = p ? p[m] : APRR(m - 1, 1);
 
--m;
while(n--)
r = APRR(m, r);
 
return r;
}
 
unsigned long long APRA(unsigned int m, unsigned int n) {
return
m ?
n ?
APRR(m, n)
: p ? p[m] : APRA(--m, 1)
: ++n
;
}
 
unsigned long long APR(unsigned int m, unsigned int n) {
unsigned long long r = 0;
 
// Allocate
p = (unsigned long long *) malloc(sizeof(unsigned long long) * (m + 1));
 
// Initialize
for(; r <= m; ++r)
p[r] = r ? APRA(r - 1, 1) : APRA(r, 0);
 
// Calculate
r = APRA(m, n);
 
// Free
free(p);
 
return r;
}
 
unsigned long long AP(unsigned int m, unsigned int n) {
return APH(m, n);
return APR(m, n);
}
 
int main(int n, char ** a) {
unsigned int M, N;
 
if (n != 3) {
printf("Usage: %s <m> <n>\n", *a);
return 1;
}
 
printf("AckermannPeter(%u, %u) = %llu\n", M = atoi(a[1]), N = atoi(a[2]), AP(M, N));
 
//printf("\nPress any key...");
//getch();
return 0;
}</syntaxhighlight>
 
A couple of more iterative techniques...
<syntaxhighlight lang="c">/* Thejaka Maldeniya */
 
#include <conio.h>
 
unsigned long long HI(unsigned int n, unsigned long long a, unsigned long long b) {
// Hyperfunction (Iterative): Perform a Hyperoperation...
 
unsigned long long *I, r = 1;
unsigned int N = n - 3;
 
if (!N)
// Exponentiation
while(b--)
r *= a;
else if(b) {
n -= 2;
 
// Allocate
I = (unsigned long long *) malloc(sizeof(unsigned long long) * n--);
 
// Initialize
I[n] = b;
 
// Calculate
for(;;) {
if(I[n]) {
--I[n];
if (n)
I[--n] = r, r = 1;
else
r *= a;
} else
for(;;)
if (n == N)
goto a;
else if(I[++n])
break;
}
a:
 
// Free
free(I);
}
 
return r;
}
 
unsigned long long H(unsigned int n, unsigned long long a, unsigned long long b) {
// Hyperfunction (Iterative-O(1) Hybrid): Perform a Hyperoperation...
 
switch(n) {
case 0:
// Increment
return ++b;
case 1:
// Addition
return a + b;
case 2:
// Multiplication
return a * b;
}
 
return HI(n, a, b);
}
 
unsigned long long APH(unsigned int m, unsigned int n) {
// Ackermann-Péter Function (Recursive-Iterative-O(1) Hybrid)
return H(m, 2, n + 3) - 3;
}
 
unsigned long long * p = 0;
 
unsigned long long APIA(unsigned int m, unsigned int n) {
if (!m) return ++n;
 
// Initialize
unsigned long long *I, r = p ? p[m] : APIA(m - 1, 1);
unsigned int M = m;
 
if (n) {
// Allocate
I = (unsigned long long *) malloc(sizeof(unsigned long long) * (m + 1));
 
// Initialize
I[m] = n;
 
// Calculate
for(;;) {
if(I[m]) {
if (m)
--I[m], I[--m] = r, r = p ? p[m] : APIA(m - 1, 1);
else
r += I[m], I[m] = 0;
} else
for(;;)
if (m == M)
goto a;
else if(I[++m])
break;
}
a:
 
// Free
free(I);
}
 
return r;
}
 
unsigned long long API(unsigned int m, unsigned int n) {
unsigned long long r = 0;
 
// Allocate
p = (unsigned long long *) malloc(sizeof(unsigned long long) * (m + 1));
 
// Initialize
for(; r <= m; ++r)
p[r] = r ? APIA(r - 1, 1) : APIA(r, 0);
 
// Calculate
r = APIA(m, n);
 
// Free
free(p);
 
return r;
}
 
unsigned long long AP(unsigned int m, unsigned int n) {
return APH(m, n);
return API(m, n);
}
 
int main(int n, char ** a) {
unsigned int M, N;
 
if (n != 3) {
printf("Usage: %s <m> <n>\n", *a);
return 1;
}
 
printf("AckermannPeter(%u, %u) = %llu\n", M = atoi(a[1]), N = atoi(a[2]), AP(M, N));
 
//printf("\nPress any key...");
//getch();
return 0;
}</syntaxhighlight>
 
A few further tweaks/optimizations may be possible.
 
=={{header|C sharp|C#}}==
 
===Basic Version===
<langsyntaxhighlight lang="csharp">using System;
class Program
{
Line 1,941 ⟶ 2,300:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,967 ⟶ 2,326:
 
===Efficient Version===
<langsyntaxhighlight lang="csharp">
using System;
using System.Numerics;
Line 2,095 ⟶ 2,454:
}
}
</syntaxhighlight>
</lang>
Possibly the most efficient implementation of Ackermann in C#. It successfully runs Ack(4,2) when executed in Visual Studio. Don't forget to add a reference to System.Numerics.
 
Line 2,101 ⟶ 2,460:
 
===Basic version===
<langsyntaxhighlight lang="cpp">#include <iostream>
 
unsigned int ackermann(unsigned int m, unsigned int n) {
Line 2,120 ⟶ 2,479:
}
}
</syntaxhighlight>
</lang>
 
===Efficient version===
Line 2,126 ⟶ 2,485:
C++11 with boost's big integer type. Compile with:
g++ -std=c++11 -I /path/to/boost ackermann.cpp.
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
#include <string>
Line 2,179 ⟶ 2,538:
<< text.substr(0, 80) << "\n...\n"
<< text.substr(text.length() - 80) << "\n";
}</langsyntaxhighlight>
<pre>
<pre>
Line 2,230 ⟶ 2,589:
 
=={{header|Chapel}}==
<langsyntaxhighlight lang="chapel">proc A(m:int, n:int):int {
if m == 0 then
return n + 1;
Line 2,237 ⟶ 2,596:
else
return A(m - 1, A(m, n - 1));
}</langsyntaxhighlight>
 
=={{header|Clay}}==
<langsyntaxhighlight Claylang="clay">ackermann(m, n) {
if(m == 0)
return n + 1;
Line 2,247 ⟶ 2,606:
 
return ackermann(m - 1, ackermann(m, n - 1));
}</langsyntaxhighlight>
 
=={{header|CLIPS}}==
'''Functional solution'''
<langsyntaxhighlight lang="clips">(deffunction ackerman
(?m ?n)
(if (= 0 ?m)
Line 2,260 ⟶ 2,619:
)
)
)</langsyntaxhighlight>
{{out|Example usage}}
<pre>CLIPS> (ackerman 0 4)
Line 2,272 ⟶ 2,631:
</pre>
'''Fact-based solution'''
<langsyntaxhighlight lang="clips">(deffacts solve-items
(solve 0 4)
(solve 1 4)
Line 2,339 ⟶ 2,698:
(retract ?solve)
(printout t "A(" ?m "," ?n ") = " ?result crlf)
)</langsyntaxhighlight>
When invoked, each required A(m,n) needed to solve the requested (solve ?m ?n) facts gets generated as its own fact. Below shows the invocation of the above, as well as an excerpt of the final facts list. Regardless of how many input (solve ?m ?n) requests are made, each possible A(m,n) is only solved once.
<pre>CLIPS> (reset)
Line 2,367 ⟶ 2,726:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn ackermann [m n]
(cond (zero? m) (inc n)
(zero? n) (ackermann (dec m) 1)
:else (ackermann (dec m) (ackermann m (dec n)))))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Ackermann function
ack = proc (m, n: int) returns (int)
if m=0 then return(n+1)
Line 2,391 ⟶ 2,750:
stream$putl(po, "")
end
end start_up </langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
Line 2,399 ⟶ 2,758:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Ackermann.
 
Line 2,430 ⟶ 2,789:
 
GOBACK
.</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">ackermann = (m, n) ->
if m is 0 then n + 1
else if m > 0 and n is 0 then ackermann m - 1, 1
else ackermann m - 1, ackermann m, n - 1</langsyntaxhighlight>
 
=={{header|Comal}}==
<langsyntaxhighlight lang="comal">0010 //
0020 // Ackermann function
0030 //
Line 2,455 ⟶ 2,814:
0150 PRINT
0160 ENDFOR m#
0170 END</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5
Line 2,463 ⟶ 2,822:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun ackermann (m n)
(cond ((zerop m) (1+ n))
((zerop n) (ackermann (1- m) 1))
(t (ackermann (1- m) (ackermann m (1- n))))))</langsyntaxhighlight>
More elaborately:
<langsyntaxhighlight lang="lisp">(defun ackermann (m n)
(case m ((0) (1+ n))
((1) (+ 2 n))
Line 2,477 ⟶ 2,836:
(loop for m from 0 to 4 do
(loop for n from (- 5 m) to (- 6 m) do
(format t "A(~d, ~d) = ~d~%" m n (ackermann m n))))</langsyntaxhighlight>
{{out}}<pre>A(0, 5) = 6
A(0, 6) = 7
Line 2,491 ⟶ 2,850:
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE NpctAckerman;
Line 2,521 ⟶ 2,880:
 
END NpctAckerman.
</syntaxhighlight>
</lang>
Execute: ^Q NpctAckerman.Do<br/>
<pre>
Line 2,532 ⟶ 2,891:
 
=={{header|Coq}}==
===Standard===
<lang coq>Require Import Arith.
<syntaxhighlight lang="coq">
Fixpoint A m := fix A_m n :=
Fixpoint ack (m : nat) : nat -> nat :=
match m with
fix ack_m |(n 0: =>nat) n: +nat 1:=
|match Sm pm =>with
match| n0 with=> S n
| S | 0pm => A pm 1
|match Sn pn => A pm (A_m pn)with
end | 0 => ack pm 1
| S pn => ack pm (ack_m pn)
end.</lang>
end
end.
 
 
(*
Example:
A(3, 2) = 29
*)
 
Eval compute in ack 3 2.
</syntaxhighlight>
 
{{out}}
<pre>
= 29
: nat
</pre>
 
===Using fold===
<lang coq>Require Import Utf8.
<syntaxhighlight lang="coq">
Require Import Utf8.
 
Section FOLD.
Context {A : Type} (f : A → A) (a : A).
Fixpoint fold (n : nat) : A :=
match n with
| O => a
| S n'k => f (fold n'k)
end.
End FOLD.
Line 2,556 ⟶ 2,934:
Definition ackermann : nat → nat → nat :=
fold (λ g, fold g (g (S O))) S.
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">def ack(m, n)
if m == 0
n + 1
Line 2,574 ⟶ 2,952:
puts (0..6).map { |n| ack(m, n) }.join(' ')
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,586 ⟶ 2,964:
=={{header|D}}==
===Basic version===
<langsyntaxhighlight lang="d">ulong ackermann(in ulong m, in ulong n) pure nothrow @nogc {
if (m == 0)
return n + 1;
Line 2,596 ⟶ 2,974:
void main() {
assert(ackermann(2, 4) == 11);
}</langsyntaxhighlight>
 
===More Efficient Version===
{{trans|Mathematica}}
<langsyntaxhighlight lang="d">import std.stdio, std.bigint, std.conv;
 
BigInt ipow(BigInt base, BigInt exp) pure nothrow {
Line 2,643 ⟶ 3,021:
writefln("ackermann(4, 2)) (%d digits):\n%s...\n%s",
a.length, a[0 .. 94], a[$ - 96 .. $]);
}</langsyntaxhighlight>
 
{{out}}
Line 2,677 ⟶ 3,055:
=={{header|Dart}}==
no caching, the implementation takes ages even for A(4,1)
<langsyntaxhighlight lang="dart">int A(int m, int n) => m==0 ? n+1 : n==0 ? A(m-1,1) : A(m-1,A(m,n-1));
 
main() {
Line 2,689 ⟶ 3,067:
print(A(3,5));
print(A(4,0));
}</langsyntaxhighlight>
 
=={{header|Dc}}==
This needs a modern Dc with <code>r</code> (swap) and <code>#</code> (comment). It easily can be adapted to an older Dc, but it will impact readability a lot.
<langsyntaxhighlight lang="dc">[ # todo: n 0 -- n+1 and break 2 levels
+ 1 + # n+1
q
Line 2,715 ⟶ 3,093:
] sA
 
3 9 lA x f</langsyntaxhighlight>
{{out}}
<pre>
Line 2,722 ⟶ 3,100:
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">function Ackermann(m,n:Int64):Int64;
begin
if m = 0 then
Line 2,730 ⟶ 3,108:
else
Result := Ackermann(m-1, Ackermann(m, n - 1));
end;</langsyntaxhighlight>
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">/* Ackermann function */
proc ack(word m, n) word:
if m=0 then n+1
Line 2,750 ⟶ 3,128:
writeln()
od
corp</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
Line 2,758 ⟶ 3,136:
 
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">function Ackermann(m, n : Integer) : Integer;
begin
if m = 0 then
Line 2,765 ⟶ 3,143:
Result := Ackermann(m-1, 1)
else Result := Ackermann(m-1, Ackermann(m, n-1));
end;</langsyntaxhighlight>
 
=={{header|Dylan}}==
<langsyntaxhighlight lang="dylan">define method ack(m == 0, n :: <integer>)
n + 1
end;
define method ack(m :: <integer>, n :: <integer>)
ack(m - 1, if (n == 0) 1 else ack(m, n - 1) end)
end;</langsyntaxhighlight>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">def A(m, n) {
return if (m <=> 0) { n+1 } \
else if (m > 0 && n <=> 0) { A(m-1, 1) } \
else { A(m-1, A(m,n-1)) }
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
<lang>func ackerm m n . r .
func ifackerm m =n 0.
if rm = n + 10
elif return n =+ 01
elif calln ackerm= m - 1 1 r0
return ackerm (m - 1) 1
else
else
call ackerm m n - 1 h
call return ackerm (m - 1) hackerm rm (n - 1)
.
.
callprint ackerm 3 6 r
</syntaxhighlight>
print r</lang>
 
=={{header|Egel}}==
<syntaxhighlight lang="egel">
<lang Egel>
def ackermann =
[ 0 N -> N + 1
| M 0 -> ackermann (M - 1) 1
| M N -> ackermann (M - 1) (ackermann M (N - 1)) ]
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
[https://github.com/ljr1981/rosettacode_answers/blob/main/src/ackerman_example/ackerman_example.e Example code]
<lang Eiffel>
 
note
[https://github.com/ljr1981/rosettacode_answers/blob/main/testing/rc_ackerman/rc_ackerman_test_set.e Test of Example code]
description: "Example of Ackerman function"
synopsis: "[
The EIS link below (in Eiffel Studio) will launch in either an in-IDE browser or
and external browser (your choice). The protocol informs Eiffel Studio about what
program to use to open the `src' reference, which can be URI, PDF, or DOC. See
second EIS for more information.
]"
EIS: "name=Ackermann_function", "protocol=URI", "tag=rosetta_code",
"src=http://rosettacode.org/wiki/Ackermann_function"
EIS: "name=eis_protocols", "protocol=URI", "tag=eiffel_docs",
"src=https://docs.eiffel.com/book/eiffelstudio/protocols"
 
<syntaxhighlight lang="eiffel">
class
ACKERMAN_EXAMPLE
APPLICATION
 
feature -- Basic Operations
create
make
 
feature {NONE} -- Initialization
 
make
do
print ("%N A(0,0):" + ackerman (0, 0).out)
print ("%N A(1,0):" + ackerman (1, 0).out)
print ("%N A(0,1):" + ackerman (0, 1).out)
print ("%N A(1,1):" + ackerman (1, 1).out)
print ("%N A(2,0):" + ackerman (2, 0).out)
print ("%N A(2,1):" + ackerman (2, 1).out)
print ("%N A(2,2):" + ackerman (2, 2).out)
print ("%N A(0,2):" + ackerman (0, 2).out)
print ("%N A(1,2):" + ackerman (1, 2).out)
print ("%N A(3,3):" + ackerman (3, 3).out)
print ("%N A(3,4):" + ackerman (3, 4).out)
end
 
feature -- Access
 
ackerman (m, n: NATURAL): NATURAL
-- Recursively compute the n-th term of a series.
require
non_negative_m: m >= 0
non_negative_n: n >= 0
do
if m = 0 then
Result := n + 1
elseif m > 0 and n = 0 then
Result := ackerman (m - 1, 1)
elseif m > 0 and n > 0 then
else
Result := ackerman (m - 1, ackerman (m, n - 1))
else
check invalid_arg_state: False end
end
end
 
end
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
<langsyntaxhighlight lang="ela">ack 0 n = n+1
ack m 0 = ack (m - 1) 1
ack m n = ack (m - 1) <| ack m <| n - 1</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<langsyntaxhighlight lang="elena">import extensions;
 
ackermann(m,n)
{
if(n < 0 || m < 0)
{
InvalidArgumentException.raise()
};
m =>
0 { ^n + 1 }
:! {
n =>
0 { ^ackermann(m - 1,1) }
:! { ^ackermann(m - 1,ackermann(m,n-1)) }
}
}
 
public program()
{
for(int i:=0,; i <= 3,; i += 1)
{
for(int j := 0,; j <= 5,; j += 1)
{
console.printLine("A(",i,",",j,")=",ackermann(i,j))
}
};
 
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,923 ⟶ 3,279:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Ackermann do
def ack(0, n), do: n + 1
def ack(m, 0), do: ack(m - 1, 1)
Line 2,931 ⟶ 3,287:
Enum.each(0..3, fn m ->
IO.puts Enum.map_join(0..6, " ", fn n -> Ackermann.ack(m, n) end)
end)</langsyntaxhighlight>
 
{{out}}
Line 2,942 ⟶ 3,298:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">(defun ackermann (m n)
(cond ((zerop m) (1+ n))
((zerop n) (ackermann (1- m) 1))
(t (ackermann (1- m)
(ackermann m (1- n))))))</langsyntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun ackermann = int by int m, int n
return when(m == 0,
n + 1,
when(n == 0,
ackermann(m - 1, 1),
ackermann(m - 1, ackermann(m, n - 1))))
end
for int m = 0; m <= 3; ++m
for int n = 0; n <= 6; ++n
writeLine("Ackermann(" + m + ", " + n + ") = " + ackermann(m, n))
end
end
</syntaxhighlight>
{{out}}
<pre>
Ackermann(0, 0) = 1
Ackermann(0, 1) = 2
Ackermann(0, 2) = 3
Ackermann(0, 3) = 4
Ackermann(0, 4) = 5
Ackermann(0, 5) = 6
Ackermann(0, 6) = 7
Ackermann(1, 0) = 2
Ackermann(1, 1) = 3
Ackermann(1, 2) = 4
Ackermann(1, 3) = 5
Ackermann(1, 4) = 6
Ackermann(1, 5) = 7
Ackermann(1, 6) = 8
Ackermann(2, 0) = 3
Ackermann(2, 1) = 5
Ackermann(2, 2) = 7
Ackermann(2, 3) = 9
Ackermann(2, 4) = 11
Ackermann(2, 5) = 13
Ackermann(2, 6) = 15
Ackermann(3, 0) = 5
Ackermann(3, 1) = 13
Ackermann(3, 2) = 29
Ackermann(3, 3) = 61
Ackermann(3, 4) = 125
Ackermann(3, 5) = 253
Ackermann(3, 6) = 509
</pre>
 
=={{header|Erlang}}==
 
<langsyntaxhighlight lang="erlang">
-module(ackermann).
-export([ackermann/2]).
Line 2,960 ⟶ 3,363:
ackermann(M, N) when M > 0 andalso N > 0 ->
ackermann(M-1, ackermann(M, N-1)).
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
Line 2,966 ⟶ 3,369:
substituted with the new ERRE control statements.
 
<langsyntaxhighlight lang="erre">
PROGRAM ACKERMAN
 
Line 3,021 ⟶ 3,424:
END FOR
END PROGRAM
</syntaxhighlight>
</lang>
 
Prints a list of Ackermann function values: from A(0,0) to A(3,9). Uses a stack to avoid
Line 3,039 ⟶ 3,442:
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>M=zeros(1000,1000);
>function map A(m,n) ...
Line 3,056 ⟶ 3,459:
3 5 7 9 11 13
5 13 29 61 125 253
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
This is based on the [[VBScript]] example.
<langsyntaxhighlight Euphorialang="euphoria">function ack(atom m, atom n)
if m = 0 then
return n + 1
Line 3,075 ⟶ 3,478:
end for
puts( 1, "\n" )
end for</langsyntaxhighlight>
 
=={{header|Ezhil}}==
<syntaxhighlight lang="ezhil">
<lang Ezhil>
நிரல்பாகம் அகெர்மன்(முதலெண், இரண்டாமெண்)
 
Line 3,117 ⟶ 3,520:
 
முடி
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
The following program implements the Ackermann function in F# but is not tail-recursive and so runs out of stack space quite fast.
<langsyntaxhighlight lang="fsharp">let rec ackermann m n =
match m, n with
| 0, n -> n + 1
Line 3,128 ⟶ 3,531:
 
do
printfn "%A" (ackermann (int fsi.CommandLineArgs.[1]) (int fsi.CommandLineArgs.[2]))</langsyntaxhighlight>
Transforming this into continuation passing style avoids limited stack space by permitting tail-recursion.
<langsyntaxhighlight lang="fsharp">let ackermann M N =
let rec acker (m, n, k) =
match m,n with
Line 3,136 ⟶ 3,539:
| m, 0 -> acker ((m - 1), 1, k)
| m, n -> acker (m, (n - 1), (fun x -> acker ((m - 1), x, k)))
acker (M, N, (fun x -> x))</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel math locals combinators ;
IN: ackermann
 
Line 3,147 ⟶ 3,550:
{ [ n 0 = ] [ m 1 - 1 ackermann ] }
[ m 1 - m n 1 - ackermann ackermann ]
} cond ;</langsyntaxhighlight>
 
=={{header|Falcon}}==
<langsyntaxhighlight lang="falcon">function ackermann( m, n )
if m == 0: return( n + 1 )
if n == 0: return( ackermann( m - 1, 1 ) )
Line 3,161 ⟶ 3,564:
end
>
end</langsyntaxhighlight>
The above will output the below.
Formating options to make this pretty are available,
Line 3,173 ⟶ 3,576:
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">[$$[%
\$$[%
1-\$@@a;! { i j -> A(i-1, A(i, j-1)) }
Line 3,184 ⟶ 3,587:
]?]a: { j i }
 
3 3 a;! . { 61 }</langsyntaxhighlight>
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">class Main
{
// assuming m,n are positive
Line 3,210 ⟶ 3,613:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,245 ⟶ 3,648:
=={{header|FBSL}}==
Mixed-language solution using pure FBSL, Dynamic Assembler, and Dynamic C layers of FBSL v3.5 concurrently. '''The following is a single script'''; the breaks are caused by switching between RC's different syntax highlighting schemes:
<langsyntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
TestAckermann()
Line 3,266 ⟶ 3,669:
END FUNCTION
 
DYNC AckermannC(m AS INTEGER, n AS INTEGER) AS INTEGER</langsyntaxhighlight><syntaxhighlight lang C="c">
int Ackermann(int m, int n)
{
Line 3,277 ⟶ 3,680:
{
return Ackermann(m, n);
}</langsyntaxhighlight><syntaxhighlight lang ="qbasic">
END DYNC
 
DYNASM AckermannA(m AS INTEGER, n AS INTEGER) AS INTEGER</langsyntaxhighlight><syntaxhighlight lang ="asm">
ENTER 0, 0
INVOKE Ackermann, m, n
Line 3,316 ⟶ 3,719:
LEAVE
RET 8
</langsyntaxhighlight><syntaxhighlight lang ="qbasic">END DYNASM</langsyntaxhighlight>
 
{{out}}
Line 3,329 ⟶ 3,732:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func A(m,n) = if m = 0 then n+1 else if n = 0 then A(m-1,1) else A(m-1,A(m,n-1)) fi fi.;
A(3,8)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,337 ⟶ 3,740:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: acker ( m n -- u )
over 0= IF nip 1+ EXIT THEN
swap 1- swap ( m-1 n -- )
dup 0= IF 1+ recurse EXIT THEN
1- over 1+ swap recurse recurse ;</langsyntaxhighlight>
{{out|Example of use}}
<pre>FORTH> 0 0 acker . 1 ok
FORTH> 3 4 acker . 125 ok</pre>
An optimized version:
<langsyntaxhighlight lang="forth">: ackermann ( m n -- u )
over ( case statement)
0 over = if drop nip 1+ else
Line 3,359 ⟶ 3,762:
then
then then then then
;</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM EXAMPLE
IMPLICIT NONE
Line 3,389 ⟶ 3,792:
END FUNCTION Ackermann
 
END PROGRAM EXAMPLE</langsyntaxhighlight>
 
=={{header|Free Pascal}}==
Line 3,395 ⟶ 3,798:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 28-10-2016
' compile with: fbc -s console
' to do A(4, 2) the stack size needs to be increased
Line 3,435 ⟶ 3,838:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9 10 11
Line 3,444 ⟶ 3,847:
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">def
ackermann( 0, n ) = n + 1
ackermann( m, 0 ) = ackermann( m - 1, 1 )
Line 3,450 ⟶ 3,853:
 
for m <- 0..3, n <- 0..4
printf( 'Ackermann( %d, %d ) = %d\n', m, n, ackermann(m, n) )</langsyntaxhighlight>
 
{{out}}
Line 3,479 ⟶ 3,882:
=={{header|Futhark}}==
 
<syntaxhighlight lang="futhark">
<lang Futhark>
fun ackermann(m: int, n: int): int =
if m == 0 then n + 1
else if n == 0 then ackermann(m-1, 1)
else ackermann(m - 1, ackermann(m, n-1))
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn Ackerman( m as NSInteger, n as NSInteger ) as NSInteger
NSInteger result
 
select
case m == 0 : result = ( n + 1 )
case n == 0 : result = fn Ackerman( ( m - 1 ), 1 )
case else : result = fn Ackerman( ( m - 1 ), fn Ackerman( m, ( n - 1 ) ) )
end select
end fn = result
 
Line 3,514 ⟶ 3,917:
 
HandleEvents
</syntaxhighlight>
</lang>
 
Output:
Line 3,562 ⟶ 3,965:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Ackermann_function}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Ackermann function 01.png]]
In '''[https://formulae.org/?example=Ackermann_function this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Ackermann function 02.png]]
 
[[File:Fōrmulæ - Ackermann function 03.png]]
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Function Ackermann(m As Float, n As Float) As Float
If m = 0 Then
Return n + 1
Line 3,586 ⟶ 3,995:
Next
Next
End</langsyntaxhighlight>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">ack := function(m, n)
if m = 0 then
return n + 1;
Line 3,599 ⟶ 4,008:
return fail;
fi;
end;</langsyntaxhighlight>
 
=={{header|Genyris}}==
<langsyntaxhighlight lang="genyris">def A (m n)
cond
(equal? m 0)
Line 3,610 ⟶ 4,019:
else
A (- m 1)
A m (- n 1)</langsyntaxhighlight>
 
=={{header|GML}}==
Define a script resource named ackermann and paste this code inside:
<langsyntaxhighlight GMLlang="gml">///ackermann(m,n)
var m, n;
m = argument0;
Line 3,629 ⟶ 4,038:
{
return (ackermann(m-1,ackermann(m,n-1,2),1))
}</langsyntaxhighlight>
 
=={{header|gnuplot}}==
<langsyntaxhighlight lang="gnuplot">A (m, n) = m == 0 ? n + 1 : n == 0 ? A (m - 1, 1) : A (m - 1, A (m, n - 1))
print A (0, 4)
print A (1, 4)
print A (2, 4)
print A (3, 4)</langsyntaxhighlight>
{{out}}
5
Line 3,645 ⟶ 4,054:
=={{header|Go}}==
===Classic version===
<langsyntaxhighlight lang="go">func Ackermann(m, n uint) uint {
switch 0 {
case m:
Line 3,653 ⟶ 4,062:
}
return Ackermann(m - 1, Ackermann(m, n - 1))
}</langsyntaxhighlight>
===Expanded version===
<langsyntaxhighlight lang="go">func Ackermann2(m, n uint) uint {
switch {
case m == 0:
Line 3,669 ⟶ 4,078:
}
return Ackermann2(m - 1, Ackermann2(m, n - 1))
}</langsyntaxhighlight>
===Expanded version with arbitrary precision===
<langsyntaxhighlight lang="go">package main
 
import (
Line 3,760 ⟶ 4,169:
)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,774 ⟶ 4,183:
 
=={{header|Golfscript}}==
<langsyntaxhighlight lang="golfscript">{
:_n; :_m;
_m 0= {_n 1+}
Line 3,781 ⟶ 4,190:
if}
if
}:ack;</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def ack ( m, n ) {
assert m >= 0 && n >= 0 : 'both arguments must be non-negative'
m == 0 ? n + 1 : n == 0 ? ack(m-1, 1) : ack(m-1, ack(m, n-1))
}</langsyntaxhighlight>
Test program:
<langsyntaxhighlight lang="groovy">def ackMatrix = (0..3).collect { m -> (0..8).collect { n -> ack(m, n) } }
ackMatrix.each { it.each { elt -> printf "%7d", elt }; println() }</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
Line 3,797 ⟶ 4,206:
5 13 29 61 125 253 509 1021 2045</pre>
Note: In the default groovyConsole configuration for WinXP, "ack(4,1)" caused a stack overflow error!
 
=={{header|Hare}}==
<syntaxhighlight lang="hare">use fmt;
 
fn ackermann(m: u64, n: u64) u64 = {
if (m == 0) {
return n + 1;
};
if (n == 0) {
return ackermann(m - 1, 1);
};
return ackermann(m - 1, ackermann(m, n - 1));
};
 
export fn main() void = {
for (let m = 0u64; m < 4; m += 1) {
for (let n = 0u64; n < 10; n += 1) {
fmt::printfln("A({}, {}) = {}", m, n, ackermann(m, n))!;
};
fmt::println()!;
};
};</syntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">ack :: Int -> Int -> Int
ack 0 n = succ n
ack m 0 = ack (pred m) 1
Line 3,805 ⟶ 4,236:
 
main :: IO ()
main = mapM_ print $ uncurry ack <$> [(0, 0), (3, 4)]</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,811 ⟶ 4,242:
 
Generating a list instead:
<langsyntaxhighlight lang="haskell">import Data.List (mapAccumL)
 
-- everything here are [Int] or [[Int]], which would overflow
Line 3,820 ⟶ 4,251:
f a b = (aa, head aa) where aa = drop b a
 
main = mapM_ print $ map (\n -> take (6 - n) $ ackermann !! n) [0..5]</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">class RosettaDemo
{
static public function main()
Line 3,842 ⟶ 4,273:
return ackermann(m-1, ackermann(m, n-1));
}
}</langsyntaxhighlight>
 
=={{header|Hoon}}==
<syntaxhighlight lang="hoon">
<lang Hoon>
|= [m=@ud n=@ud]
?: =(m 0)
Line 3,852 ⟶ 4,283:
$(n 1, m (dec m))
$(m (dec m), n $(n (dec n)))
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 3,858 ⟶ 4,289:
Taken from the public domain Icon Programming Library's [http://www.cs.arizona.edu/icon/library/procs/memrfncs.htm acker in memrfncs],
written by Ralph E. Griswold.
<langsyntaxhighlight Iconlang="icon">procedure acker(i, j)
static memory
 
Line 3,882 ⟶ 4,313:
write()
}
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,891 ⟶ 4,322:
 
=={{header|Idris}}==
<langsyntaxhighlight lang="idris">A : Nat -> Nat -> Nat
A Z n = S n
A (S m) Z = A m (S Z)
A (S m) (S n) = A m (A (S m) n)</langsyntaxhighlight>
 
=={{header|Ioke}}==
{{trans|Clojure}}
<langsyntaxhighlight lang="ioke">ackermann = method(m,n,
cond(
m zero?, n succ,
n zero?, ackermann(m pred, 1),
ackermann(m pred, ackermann(m, n pred)))
)</langsyntaxhighlight>
 
=={{header|J}}==
As posted at the [[j:Essays/Ackermann%27s%20Function|J wiki]]
<langsyntaxhighlight lang="j">ack=: c1`c1`c2`c3 @. (#.@,&*) M.
c1=: >:@] NB. if 0=x, 1+y
c2=: <:@[ ack 1: NB. if 0=y, (x-1) ack 1
c3=: <:@[ ack [ ack <:@] NB. else, (x-1) ack x ack y-1</langsyntaxhighlight>
{{out|Example use}}
<langsyntaxhighlight lang="j"> 0 ack 3
4
1 ack 3
Line 3,919 ⟶ 4,350:
9
3 ack 3
61</langsyntaxhighlight>
J's stack was too small for me to compute <tt>4 ack 1</tt>.
===Alternative Primitive Recursive Version===
Line 3,925 ⟶ 4,356:
 
The Ackermann function derived in this fashion is primitive recursive. This is possible because in J (as in some other languages) functions, or representations of them, are first-class values.
<langsyntaxhighlight lang="j"> Ack=. 3 -~ [ ({&(2 4$'>: 2x&+') ::(,&'&1'&'2x&*'@:(-&2))"0@:[ 128!:2 ]) 3 + ]</langsyntaxhighlight>
{{out|Example use}}
<langsyntaxhighlight lang="j"> 0 1 2 3 Ack 0 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
Line 3,942 ⟶ 4,373:
5 Ack 0
65533
</syntaxhighlight>
</lang>
 
A structured derivation of Ack follows:
 
<langsyntaxhighlight lang="j"> o=. @: NB. Composition of verbs (functions)
x=. o[ NB. Composing the left noun (argument)
Line 3,972 ⟶ 4,403:
(Ack=. (3 -~ [ Buck 3 + ])f.) NB. Ackermann function-level code
3 -~ [ ({&(2 4$'>: 2x&+') ::(,&'&1'&'2x&*'@:(-&2))"0@:[ 128!:2 ]) 3 + ]</langsyntaxhighlight>
 
=={{header|Java}}==
[[Category:Arbitrary precision]]
<langsyntaxhighlight lang="java">import java.math.BigInteger;
 
public static BigInteger ack(BigInteger m, BigInteger n) {
Line 3,983 ⟶ 4,414:
: ack(m.subtract(BigInteger.ONE),
n.equals(BigInteger.ZERO) ? BigInteger.ONE : ack(m, n.subtract(BigInteger.ONE)));
}</langsyntaxhighlight>
 
{{works with|Java|8+}}
<langsyntaxhighlight lang="java5">@FunctionalInterface
public interface FunctionalField<FIELD extends Enum<?>> {
public Object untypedField(FIELD field);
Line 3,994 ⟶ 4,425:
return (VALUE) untypedField(field);
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="java5">import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
Line 4,040 ⟶ 4,471:
}
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="java5">import java.math.BigInteger;
import java.util.Stack;
import java.util.function.BinaryOperator;
Line 4,185 ⟶ 4,616:
}
}
}</langsyntaxhighlight>
{{Iterative version}}
<langsyntaxhighlight lang="java5">/*
* Source https://stackoverflow.com/a/51092690/5520417
*/
Line 4,577 ⟶ 5,008:
}
 
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}}==
===ES5===
<langsyntaxhighlight lang="javascript">function ack(m, n) {
return m === 0 ? n + 1 : ack(m - 1, n === 0 ? 1 : ack(m, n - 1));
}</langsyntaxhighlight>
===Eliminating Tail Calls===
<langsyntaxhighlight lang="javascript">function ack(M,N) {
for (; M > 0; M--) {
N = N === 0 ? 1 : ack(M,N-1);
}
return N+1;
}</langsyntaxhighlight>
===Iterative, With Explicit Stack===
<langsyntaxhighlight lang="javascript">function stackermann(M, N) {
const stack = [];
for (;;) {
Line 4,611 ⟶ 5,042:
}
}
}</langsyntaxhighlight>
===Stackless Iterative===
<langsyntaxhighlight lang="javascript">#!/usr/bin/env nodejs
function ack(M, N){
const next = new Float64Array(M + 1);
Line 4,638 ⟶ 5,069:
}
var args = process.argv;
console.log(ack(parseInt(args[2]), parseInt(args[3])));</langsyntaxhighlight>
 
{{out}}
Line 4,648 ⟶ 5,079:
 
===ES6===
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 4,685 ⟶ 5,116:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[4,5,9,61]</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE ack == [[[pop null] popd succ]
From [http://www.latrobe.edu.au/phimvt/joy/jp-nestrec.html here]
<lang joy>DEFINE ack == [ [ [pop null] pop popdpred succ1 ack]
[[dup pred [ [nullswap] popdip pred 1ack ack ]]
cond.</syntaxhighlight>
[ [dup pred swap] dip pred ack ack ] ]
another using a combinator:
cond.</lang>
<syntaxhighlight lang="joy">DEFINE ack == [[[pop null] [popd succ]]
another using a combinator
<lang joy>DEFINE ack == [ [null] [pop nullpred 1] [popd succ] ]
[ [null] [popdup pred 1swap] dip pred] [] []]]
condnestrec.</syntaxhighlight>
[ [[dup pred swap] dip pred] [] [] ] ]
condnestrec.</lang>
Whenever there are two definitions with the same name, the last one is the one that is used, when invoked.
 
=={{header|jq}}==
{{ works with|jq|1.4}}
'''Also works with gojq, the Go implementation of jq.'''
 
Except for a minor tweak to the line using string interpolation, the following have also been tested using jaq, the Rust implementation of jq, as of April 13, 2023.
 
For infinite-precision integer arithmetic, use gojq or fq.
===Without Memoization===
<langsyntaxhighlight lang="jq"># input: [m,n]
def ack:
.[0] as $m | .[1] as $n
Line 4,711 ⟶ 5,145:
elif $n == 0 then [$m-1, 1] | ack
else [$m-1, ([$m, $n-1 ] | ack)] | ack
end ;</langsyntaxhighlight>
'''Example:'''
<langsyntaxhighlight lang="jq">range(0;5) as $i
| range(0; if $i > 3 then 1 else 6 end) as $j
| "A(\($i),\($j)) = \( [$i,$j] | ack )"</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh"># jq -n -r -f ackermann.jq
A(0,0) = 1
A(0,1) = 2
Line 4,742 ⟶ 5,176:
A(3,4) = 125
A(3,5) = 253
A(4,0) = 13</langsyntaxhighlight>
===With Memoization and Optimization===
<langsyntaxhighlight lang="jq"># input: [m,n, cache]
# output [value, updatedCache]
def ack:
Line 4,773 ⟶ 5,207:
 
def A(m;n): [m,n,{}] | ack | .[0];
</syntaxhighlight>
</lang>
'''Examples:'''
'''Example:'''<lang jq>A(4,1)</lang>
<syntaxhighlight lang="jq">A(4;1)</syntaxhighlight>
{{out}}
<syntaxhighlight lang ="sh">65533</langsyntaxhighlight>
 
Using gojq:
<syntaxhighlight lang="jq">A(4;2), A(3;1000000) | tostring | length</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">
19729
301031
</syntaxhighlight>
 
=={{header|Jsish}}==
From javascript entry.
<langsyntaxhighlight lang="javascript">/* Ackermann function, in Jsish */
 
function ack(m, n) {
Line 4,805 ⟶ 5,248:
ack(3,5) ==> 253
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 4,817 ⟶ 5,260:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function ack(m,n)
if m == 0
return n + 1
Line 4,825 ⟶ 5,268:
return ack(m-1,ack(m,n-1))
end
end</langsyntaxhighlight>
 
'''One-liner:'''
<langsyntaxhighlight lang="julia">ack2(m::Integer, n::Integer) = m == 0 ? n + 1 : n == 0 ? ack2(m - 1, 1) : ack2(m - 1, ack2(m, n - 1))</langsyntaxhighlight>
 
'''Using memoization''', [https://github.com/simonster/Memoize.jl source]:
<langsyntaxhighlight lang="julia">using Memoize
@memoize ack3(m::Integer, n::Integer) = m == 0 ? n + 1 : n == 0 ? ack3(m - 1, 1) : ack3(m - 1, ack3(m, n - 1))</langsyntaxhighlight>
 
'''Benchmarking''':
Line 4,844 ⟶ 5,287:
 
=={{header|K}}==
{{works with|Kona}}
See [https://github.com/kevinlawler/kona/wiki the K wiki]
<langsyntaxhighlight lang="k"> ack:{:[0=x;y+1;0=y;_f[x-1;1];_f[x-1;_f[x;y-1]]]}
ack[2;2]</lang>
7
ack[2;7]
17</syntaxhighlight>
 
=={{header|Kdf9 Usercode}}==
<langsyntaxhighlight lang="kdf9 usercode">V6; W0;
YS26000;
RESTART; J999; J999;
Line 4,898 ⟶ 5,344:
J99; (tail recursion for A[m-1, A[m, n-1]]);
 
FINISH;</langsyntaxhighlight>
 
=={{header|Klingphix}}==
<syntaxhighlight lang="text">:ack
%n !n %m !m
Line 4,917 ⟶ 5,363:
msec print
 
" " input</langsyntaxhighlight>
 
=={{header|Klong}}==
<syntaxhighlight lang="k">
<lang k>
ack::{:[0=x;y+1:|0=y;.f(x-1;1);.f(x-1;.f(x;y-1))]}
ack(2;2)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">
tailrec fun A(m: Long, n: Long): Long {
require(m >= 0L) { "m must not be negative" }
Line 4,951 ⟶ 5,397:
.forEach(::println)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,962 ⟶ 5,408:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def ack
{lambda {:m :n}
Line 4,985 ⟶ 5,431:
{ack 4 1} // too much
-> ???
</syntaxhighlight>
</lang>
 
=={{header|Lasso}}==
<langsyntaxhighlight lang="lasso">#!/usr/bin/lasso9
define ackermann(m::integer, n::integer) => {
Line 5,003 ⟶ 5,449:
y in generateSeries(0,8,2)
do stdoutnl(#x+', '#y+': ' + ackermann(#x, #y))
</syntaxhighlight>
</lang>
{{out}}
<pre>1, 0: 2
Line 5,022 ⟶ 5,468:
 
=={{header|LFE}}==
<langsyntaxhighlight lang="lisp">(defun ackermann
((0 n) (+ n 1))
((m 0) (ackermann (- m 1) 1))
((m n) (ackermann (- m 1) (ackermann m (- n 1)))))</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">Print Ackermann(1, 2)
 
Function Ackermann(m, n)
Line 5,041 ⟶ 5,487:
Ackermann = Ackermann((m - 1), Ackermann(m, (n - 1)))
End Select
End Function</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function ackermann m,n
switch
Case m = 0
Line 5,053 ⟶ 5,499:
return ackermann((m - 1), ackermann(m, (n - 1)))
end switch
end ackermann</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to ack :i :j
if :i = 0 [output :j+1]
if :j = 0 [output ack :i-1 1]
output ack :i-1 ack :i :j-1
end</langsyntaxhighlight>
 
=={{header|Logtalk}}==
<langsyntaxhighlight lang="logtalk">ack(0, N, V) :-
!,
V is N + 1.
Line 5,074 ⟶ 5,520:
N2 is N - 1,
ack(M, N2, V2),
ack(M2, V2, V).</langsyntaxhighlight>
 
=={{header|LOLCODE}}==
{{trans|C}}
<langsyntaxhighlight LOLCODElang="lolcode">HAI 1.3
 
HOW IZ I ackermann YR m AN YR n
Line 5,099 ⟶ 5,545:
IM OUTTA YR outer
 
KTHXBYE</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function ack(M,N)
if M == 0 then return N + 1 end
if N == 0 then return ack(M-1,1) end
return ack(M-1,ack(M, N-1))
end</langsyntaxhighlight>
 
=== Stackless iterative solution with multiple precision, fast ===
<langsyntaxhighlight lang="lua">
#!/usr/bin/env luajit
local gmp = require 'gmp' ('libgmp')
Line 5,148 ⟶ 5,594:
printf("%Zd\n", ack(tonumber(arg[1]), arg[2] and tonumber(arg[2]) or 0))
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,164 ⟶ 5,610:
 
=={{header|Lucid}}==
<langsyntaxhighlight lang="lucid">ack(m,n)
where
ack(m,n) = if m eq 0 then n+1
Line 5,170 ⟶ 5,616:
else ack(m-1, ack(m, n-1)) fi
fi;
end</langsyntaxhighlight>
 
=={{header|Luck}}==
<langsyntaxhighlight lang="luck">function ackermann(m: int, n: int): int = (
if m==0 then n+1
else if n==0 then ackermann(m-1,1)
else ackermann(m-1,ackermann(m,n-1))
)</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Def ackermann(m,n) =If(m=0-> n+1, If(n=0-> ackermann(m-1,1), ackermann(m-1,ackermann(m,n-1))))
Line 5,195 ⟶ 5,641:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`ack',`ifelse($1,0,`incr($2)',`ifelse($2,0,`ack(decr($1),1)',`ack(decr($1),ack($1,decr($2)))')')')dnl
ack(3,3)</langsyntaxhighlight>
{{out}}
<pre>61 </pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE ACKRMN
.MCALL .TTYOUT,.EXIT
ACKRMN::JMP MKTBL
 
; R0 = ACK(R0,R1)
ACK: MOV SP,R2 ; KEEP OLD STACK PTR
MOV #ASTK,SP ; USE PRIVATE STACK
JSR PC,1$
MOV R2,SP ; RESTORE STACK PTR
RTS PC
1$: TST R0
BNE 2$
INC R1
MOV R1,R0
RTS PC
2$: TST R1
BNE 3$
DEC R0
INC R1
BR 1$
3$: MOV R0,-(SP)
DEC R1
JSR PC,1$
MOV R0,R1
MOV (SP)+,R0
DEC R0
BR 1$
.BLKB 4000 ; BIG STACK
ASTK = .
 
; PRINT TABLE
MMAX = 4
NMAX = 7
MKTBL: CLR R3
1$: CLR R4
2$: MOV R3,R0
MOV R4,R1
JSR PC,ACK
JSR PC,PR0
INC R4
CMP R4,#NMAX
BLT 2$
MOV #15,R0
.TTYOUT
MOV #12,R0
.TTYOUT
INC R3
CMP R3,#MMAX
BLT 1$
.EXIT
; PRINT NUMBER IN R0 AS DECIMAL
PR0: MOV #4$,R1
1$: MOV #-1,R2
2$: INC R2
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R1)
MOV R2,R0
BNE 1$
3$: MOVB (R1)+,R0
.TTYOUT
BNE 3$
RTS PC
.ASCII /...../
4$: .BYTE 11,0
.END ACKRMN</syntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 5 7 9 11 13 15
5 13 29 61 125 253 509</pre>
 
=={{header|MAD}}==
Line 5,216 ⟶ 5,737:
the arguments to the recursive function via the stack or the global variables. The following program demonstrates this.
 
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
DIMENSION LIST(3000)
SET LIST TO LIST
Line 5,254 ⟶ 5,775:
VECTOR VALUES ACKF = $4HACK(,I1,1H,,I1,4H) = ,I4*$
END OF PROGRAM
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,294 ⟶ 5,815:
ACK(3,7) = 1021
ACK(3,8) = 2045</pre>
 
 
 
 
=={{header|Maple}}==
Strictly by the definition given above, we can code this as follows.
<syntaxhighlight lang="maple">
<lang Maple>
Ackermann := proc( m :: nonnegint, n :: nonnegint )
option remember; # optional automatic memoization
Line 5,311 ⟶ 5,829:
end if
end proc:
</syntaxhighlight>
</lang>
In Maple, the keyword <syntaxhighlight lang Maple="maple">thisproc</langsyntaxhighlight> refers to the currently executing procedure (closure) and is used when writing recursive procedures. (You could also use the name of the procedure, Ackermann in this case, but then a concurrently executing task or thread could re-assign that name while the recursive procedure is executing, resulting in an incorrect result.)
 
To make this faster, you can use known expansions for small values of <math>m</math>. (See [[wp:Ackermann_function|Wikipedia:Ackermann function]])
<syntaxhighlight lang="maple">
<lang Maple>
Ackermann := proc( m :: nonnegint, n :: nonnegint )
option remember; # optional automatic memoization
Line 5,332 ⟶ 5,850:
end if
end proc:
</syntaxhighlight>
</lang>
This makes it possible to compute <code>Ackermann( 4, 1 )</code> and <code>Ackermann( 4, 2 )</code> essentially instantly, though <code>Ackermann( 4, 3 )</code> is still out of reach.
 
To compute Ackermann( 1, i ) for i from 1 to 10 use
<syntaxhighlight lang="maple">
<lang Maple>
> map2( Ackermann, 1, [seq]( 1 .. 10 ) );
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
</syntaxhighlight>
</lang>
To get the first 10 values for m = 2 use
<syntaxhighlight lang="maple">
<lang Maple>
> map2( Ackermann, 2, [seq]( 1 .. 10 ) );
[5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
</syntaxhighlight>
</lang>
For Ackermann( 4, 2 ) we get a very long number with
<syntaxhighlight lang="maple">
<lang Maple>
> length( Ackermann( 4, 2 ) );
19729
</syntaxhighlight>
</lang>
digits.
 
Line 5,357 ⟶ 5,875:
This particular version of Ackermann's function was created in Mathcad Prime Express 7.0, a free version of Mathcad Prime 7.0 with restrictions (such as no programming or symbolics). All Prime Express numbers are complex. There is a recursion depth limit of about 4,500.
 
<langsyntaxhighlight Mathcadlang="mathcad">A(m,n):=if(m=0,n+1,if(n=0,A(m-1,1),A(m-1,A(m,n-1))))</langsyntaxhighlight>
 
The worksheet also contains an explictly-calculated version of Ackermann's function that calls the tetration function n<sub>a</sub>.
Line 5,372 ⟶ 5,890:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Two possible implementations would be:
<langsyntaxhighlight Mathematicalang="mathematica">$RecursionLimit=Infinity
Ackermann1[m_,n_]:=
If[m==0,n+1,
Line 5,382 ⟶ 5,900:
Ackermann2[0,n_]:=n+1;
Ackermann2[m_,0]:=Ackermann1[m-1,1];
Ackermann2[m_,n_]:=Ackermann1[m-1,Ackermann1[m,n-1]]</langsyntaxhighlight>
Note that the second implementation is quite a bit faster, as doing 'if' comparisons is slower than the built-in pattern matching algorithms.
Examples:
<langsyntaxhighlight Mathematicalang="mathematica">Flatten[#,1]&@Table[{"Ackermann2["<>ToString[i]<>","<>ToString[j]<>"] =",Ackermann2[i,j]},{i,3},{j,8}]//Grid</langsyntaxhighlight>
gives back:
<langsyntaxhighlight Mathematicalang="mathematica">Ackermann2[1,1] = 3
Ackermann2[1,2] = 4
Ackermann2[1,3] = 5
Line 5,410 ⟶ 5,928:
Ackermann2[3,6] = 509
Ackermann2[3,7] = 1021
Ackermann2[3,8] = 2045</langsyntaxhighlight>
If we would like to calculate Ackermann[4,1] or Ackermann[4,2] we have to optimize a little bit:
<langsyntaxhighlight Mathematicalang="mathematica">Clear[Ackermann3]
$RecursionLimit=Infinity;
Ackermann3[0,n_]:=n+1;
Line 5,419 ⟶ 5,937:
Ackermann3[3,n_]:=5+8 (2^n-1);
Ackermann3[m_,0]:=Ackermann3[m-1,1];
Ackermann3[m_,n_]:=Ackermann3[m-1,Ackermann3[m,n-1]]</langsyntaxhighlight>
Now computing Ackermann[4,1] and Ackermann[4,2] can be done quickly (<0.01 sec):
Examples 2:
<langsyntaxhighlight Mathematicalang="mathematica">Ackermann3[4, 1]
Ackermann3[4, 2]</langsyntaxhighlight>
gives back:
<div style="width:full;overflow:scroll"><langsyntaxhighlight Mathematicalang="mathematica">65533
2003529930406846464979072351560255750447825475569751419265016973710894059556311453089506130880........699146577530041384717124577965048175856395072895337539755822087777506072339445587895905719156733</langsyntaxhighlight></div>
Ackermann[4,2] has 19729 digits, several thousands of digits omitted in the result above for obvious reasons. Ackermann[5,0] can be computed also quite fast, and is equal to 65533.
Summarizing Ackermann[0,n_], Ackermann[1,n_], Ackermann[2,n_], and Ackermann[3,n_] can all be calculated for n>>1000. Ackermann[4,0], Ackermann[4,1], Ackermann[4,2] and Ackermann[5,0] are only possible now. Maybe in the future we can calculate higher Ackermann numbers efficiently and fast. Although showing the results will always be a problem.
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function A = ackermannFunction(m,n)
if m == 0
A = n+1;
Line 5,439 ⟶ 5,957:
A = ackermannFunction( m-1,ackermannFunction(m,n-1) );
end
end</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">ackermann(m, n) := if integerp(m) and integerp(n) then ackermann[m, n] else 'ackermann(m, n)$
 
ackermann[m, n] := if m = 0 then n + 1
Line 5,456 ⟶ 5,974:
ackermann(4, n) - (tetration(2, n + 3) - 3);
subst(n = 2, %);
ev(%, nouns);</langsyntaxhighlight>
 
=={{header|MAXScript}}==
Use with caution. Will cause a stack overflow for m > 3.
<langsyntaxhighlight lang="maxscript">fn ackermann m n =
(
if m == 0 then
Line 5,474 ⟶ 5,992:
ackermann (m-1) (ackermann m (n-1))
)
)</langsyntaxhighlight>
 
=={{header|Mercury}}==
This is the Ackermann function with some (obvious) elements elided. The <code>ack/3</code> predicate is implemented in terms of the <code>ack/2</code> function. The <code>ack/2</code> function is implemented in terms of the <code>ack/3</code> predicate. This makes the code both more concise and easier to follow than would otherwise be the case. The <code>integer</code> type is used instead of <code>int</code> because the problem statement stipulates the use of bignum integers if possible.
<langsyntaxhighlight lang="mercury">:- func ack(integer, integer) = integer.
ack(M, N) = R :- ack(M, N, R).
 
Line 5,487 ⟶ 6,005:
; M = integer(0) -> R = N + integer(1)
; N = integer(0) -> ack(M - integer(1), integer(1), R)
; ack(M - integer(1), ack(M, N - integer(1)), R) ).</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(
:n :m
(
Line 5,498 ⟶ 6,016:
((true) (m 1 - m n 1 - ackermann ackermann))
) case
) :ackermann</langsyntaxhighlight>
 
=={{header|MiniScript}}==
 
<langsyntaxhighlight MiniScriptlang="miniscript">ackermann = function(m, n)
if m == 0 then return n+1
if n == 0 then return ackermann(m - 1, 1)
Line 5,512 ⟶ 6,030:
print "(" + m + ", " + n + "): " + ackermann(m, n)
end for
end for</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П1 <-> П0 ПП 06 С/П ИП0 x=0 13 ИП1
1 + В/О ИП1 x=0 24 ИП0 1 П1 -
П0 ПП 06 В/О ИП0 П2 ИП1 1 - П1
ПП 06 П1 ИП2 1 - П0 ПП 06 В/О</langsyntaxhighlight>
 
=={{header|ML/I}}==
ML/I loves recursion, but runs out of its default amount of storage with larger numbers than those tested here!
===Program===
<langsyntaxhighlight MLlang="ml/Ii">MCSKIP "WITH" NL
"" Ackermann function
"" Will overflow when it reaches implementation-defined signed integer limit
Line 5,555 ⟶ 6,073:
a(3,1) => ACK(3,1)
a(3,2) => ACK(3,2)
a(4,0) => ACK(4,0)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight MLlang="ml/Ii">a(0,0) => 1
a(0,1) => 2
a(0,2) => 3
Line 5,575 ⟶ 6,093:
a(3,1) => 13
a(3,2) => 29
a(4,0) => 13</langsyntaxhighlight>
 
=={{header|mLite}}==
<langsyntaxhighlight lang="haskell">fun ackermann( 0, n ) = n + 1
| ( m, 0 ) = ackermann( m - 1, 1 )
| ( m, n ) = ackermann( m - 1, ackermann(m, n - 1) )</langsyntaxhighlight>
Test code providing tuples from (0,0) to (3,8)
<langsyntaxhighlight lang="haskell">fun jota x = map (fn x = x-1) ` iota x
 
fun test_tuples (x, y) = append_map (fn a = map (fn b = (b, a)) ` jota x) ` jota y
 
map ackermann (test_tuples(4,9))</langsyntaxhighlight>
Result
<pre>[1, 2, 3, 5, 2, 3, 5, 13, 3, 4, 7, 29, 4, 5, 9, 61, 5, 6, 11, 125, 6, 7, 13, 253, 7, 8, 15, 509, 8, 9, 17, 1021, 9, 10, 19, 2045]</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE ackerman;
 
IMPORT ASCII, NumConv, InOut;
Line 5,623 ⟶ 6,141:
END;
InOut.WriteLn
END ackerman.</langsyntaxhighlight>
{{out}}<pre>jan@Beryllium:~/modula/rosetta$ ackerman
1 2 3 4 5 6 7
Line 5,632 ⟶ 6,150:
=={{header|Modula-3}}==
The type CARDINAL is defined in Modula-3 as [0..LAST(INTEGER)], in other words, it can hold all positive integers.
<langsyntaxhighlight lang="modula3">MODULE Ack EXPORTS Main;
 
FROM IO IMPORT Put;
Line 5,655 ⟶ 6,173:
Put("\n");
END;
END Ack.</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7
Line 5,663 ⟶ 6,181:
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">Ackermann(m,n) ;
If m=0 Quit n+1
If m>0,n=0 Quit $$Ackermann(m-1,1)
Line 5,671 ⟶ 6,189:
Write $$Ackermann(1,8) ; 10
Write $$Ackermann(2,8) ; 19
Write $$Ackermann(3,5) ; 253</langsyntaxhighlight>
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Ackermann recursion, in Neko
Tectonics:
Line 5,696 ⟶ 6,214:
$print("Ackermann(", arg1, ",", arg2, "): ", ack(arg1,arg2), "\n")
catch problem
$print("Ackermann(", arg1, ",", arg2, "): ", problem, "\n")</langsyntaxhighlight>
 
{{out}}
Line 5,719 ⟶ 6,237:
=={{header|Nemerle}}==
In Nemerle, we can state the Ackermann function as a lambda. By using pattern-matching, our definition strongly resembles the mathematical notation.
<syntaxhighlight lang="nemerle">
<lang Nemerle>
using System;
using Nemerle.IO;
Line 5,740 ⟶ 6,258:
}
}
</syntaxhighlight>
</lang>
A terser version using implicit <code>match</code> (which doesn't use the alias <code>A</code> internally):
<syntaxhighlight lang="nemerle">
<lang Nemerle>
def ackermann(m, n) {
| (0, n) => n + 1
Line 5,749 ⟶ 6,267:
| _ => throw Exception("invalid inputs");
}
</syntaxhighlight>
</lang>
Or, if we were set on using the <code>A</code> notation, we could do this:
<syntaxhighlight lang="nemerle">
<lang Nemerle>
def ackermann = {
def A(m, n) {
Line 5,761 ⟶ 6,279:
A
}
</syntaxhighlight>
</lang>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 5,788 ⟶ 6,306:
end
return rval
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight lang="newlisp">
#! /usr/local/bin/newlisp
 
Line 5,798 ⟶ 6,316:
((zero? n) (ackermann (dec m) 1))
(true (ackermann (- m 1) (ackermann m (dec n))))))
</syntaxhighlight>
</lang>
 
<pre>
Line 5,807 ⟶ 6,325:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">from strutils import parseInt
 
proc ackermann(m, n: int64): int64 =
Line 5,832 ⟶ 6,350:
let second = getNumber()
echo "Result: ", $ackermann(first, second)
</syntaxhighlight>
</lang>
 
=={{header|Nit}}==
Line 5,838 ⟶ 6,356:
Source: [https://github.com/nitlang/nit/blob/master/examples/rosettacode/ackermann_function.nit the official Nit’s repository].
 
<langsyntaxhighlight lang="nit"># Task: Ackermann function
#
# A simple straightforward recursive implementation.
Line 5,855 ⟶ 6,373:
end
print ""
end</langsyntaxhighlight>
 
Output:
Line 5,894 ⟶ 6,412:
 
=={{header|Oberon-2}}==
<langsyntaxhighlight lang="oberon2">MODULE ackerman;
 
IMPORT Out;
Line 5,919 ⟶ 6,437:
END;
Out.Ln
END ackerman.</langsyntaxhighlight>
 
=={{header|Objeck}}==
{{trans|C#|C sharp}}
<langsyntaxhighlight lang="objeck">class Ackermann {
function : Main(args : String[]) ~ Nil {
for(m := 0; m <= 3; ++m;) {
Line 5,952 ⟶ 6,470:
return -1;
}
}</langsyntaxhighlight>
<pre>
Ackermann(0, 0) = 1
Line 5,977 ⟶ 6,495:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let rec a m n =
if m=0 then (n+1) else
if n=0 then (a (m-1) 1) else
(a (m-1) (a m (n-1)))</langsyntaxhighlight>
or:
<langsyntaxhighlight lang="ocaml">let rec a = function
| 0, n -> (n+1)
| m, 0 -> a(m-1, 1)
| m, n -> a(m-1, a(m, n-1))</langsyntaxhighlight>
with memoization using an hash-table:
<langsyntaxhighlight lang="ocaml">let h = Hashtbl.create 4001
 
let a m n =
Line 5,994 ⟶ 6,512:
let res = a (m, n) in
Hashtbl.add h (m, n) res;
(res)</langsyntaxhighlight>
taking advantage of the memoization we start calling small values of '''m''' and '''n''' in order to reduce the recursion call stack:
<langsyntaxhighlight lang="ocaml">let a m n =
for _m = 0 to m do
for _n = 0 to n do
Line 6,002 ⟶ 6,520:
done;
done;
(a m n)</langsyntaxhighlight>
=== Arbitrary precision ===
With arbitrary-precision integers ([http://caml.inria.fr/pub/docs/manual-ocaml/libref/Big_int.html Big_int module]):
<langsyntaxhighlight lang="ocaml">open Big_int
let one = unit_big_int
let zero = zero_big_int
Line 6,015 ⟶ 6,533:
if eq m zero then (succ n) else
if eq n zero then (a (pred m) one) else
(a (pred m) (a m (pred n)))</langsyntaxhighlight>
compile with:
ocamlopt -o acker nums.cmxa acker.ml
=== Tail-Recursive ===
Here is a [[:Category:Recursion|tail-recursive]] version:
<langsyntaxhighlight lang="ocaml">let rec find_option h v =
try Some(Hashtbl.find h v)
with Not_found -> None
Line 6,049 ⟶ 6,567:
a bounds caller todo m (n-1)
 
let a = a (Hashtbl.create 42 (* arbitrary *) ) [] [] ;;</langsyntaxhighlight>
This one uses the arbitrary precision, the tail-recursion, and the optimisation explain on the Wikipedia page about <tt>(m,n) = (3,_)</tt>.
<langsyntaxhighlight lang="ocaml">open Big_int
let one = unit_big_int
let zero = zero_big_int
Line 6,129 ⟶ 6,647:
(string_of_big_int n)
(string_of_big_int r);
;;</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">function r = ackerman(m, n)
if ( m == 0 )
r = n + 1;
Line 6,144 ⟶ 6,662:
for i = 0:3
disp(ackerman(i, 4));
endfor</langsyntaxhighlight>
 
=={{header|Oforth}}==
<langsyntaxhighlight Oforthlang="oforth">: A( m n -- p )
m ifZero: [ n 1+ return ]
m 1- n ifZero: [ 1 ] else: [ A( m, n 1- ) ] A
;</langsyntaxhighlight>
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
; simple version
(define (A m n)
(cond
((= m 0) (+ n 1))
((= n 0) (A (- m 1) 1))
(else (A (- m 1) (A m (- n 1))))))
 
(print "simple version (A 3 6): " (A 3 6))
 
; smart (lazy) version
(define (ints-from n)
(cons* n (delay (ints-from (+ n 1)))))
 
(define (knuth-up-arrow a n b)
(let loop ((n n) (b b))
(cond ((= b 0) 1)
((= n 1) (expt a b))
(else (loop (- n 1) (loop n (- b 1)))))))
 
(define (A+ m n)
(define (A-stream)
(cons*
(ints-from 1) ;; m = 0
(ints-from 2) ;; m = 1
;; m = 2
(lmap (lambda (n)
(+ (* 2 (+ n 1)) 1))
(ints-from 0))
;; m = 3
(lmap (lambda (n)
(- (knuth-up-arrow 2 (- m 2) (+ n 3)) 3))
(ints-from 0))
;; m = 4...
(delay (ldrop (A-stream) 3))))
(llref (llref (A-stream) m) n))
 
(print "extended version (A 3 6): " (A+ 3 6))
</syntaxhighlight>
{{Out}}
<pre>
simple version (A 3 6): 509
extended version (A 3 6): 509
</pre>
 
=={{header|OOC}}==
<langsyntaxhighlight lang="ooc">
ack: func (m: Int, n: Int) -> Int {
if (m == 0) {
Line 6,171 ⟶ 6,735:
}
}
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
loop m = 0 to 3
loop n = 0 to 6
Line 6,188 ⟶ 6,752:
else if n = 0 then return ackermann(m - 1, 1)
else return ackermann(m - 1, ackermann(m, n - 1))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 6,222 ⟶ 6,786:
 
=={{header|Order}}==
<langsyntaxhighlight lang="c">#include <order/interpreter.h>
 
#define ORDER_PP_DEF_8ack ORDER_PP_FN( \
Line 6,230 ⟶ 6,794:
(8else, 8ack(8dec(8X), 8ack(8X, 8dec(8Y)))))))
 
ORDER_PP(8to_lit(8ack(3, 4))) // 125</langsyntaxhighlight>
 
=={{header|Oz}}==
Oz has arbitrary precision integers.
<langsyntaxhighlight lang="oz">declare
 
fun {Ack M N}
Line 6,245 ⟶ 6,809:
in
 
{Show {Ack 3 7}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Naive implementation.
<langsyntaxhighlight lang="parigp">A(m,n)={
if(m,
if(n,
Line 6,259 ⟶ 6,823:
n+1
)
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program Ackerman;
 
function ackermann(m, n: Integer) : Integer;
Line 6,281 ⟶ 6,845:
for m := 0 to 3 do
WriteLn('A(', m, ',', n, ') = ', ackermann(m,n));
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
We memoize calls to ''A'' to make ''A''(2, ''n'') and ''A''(3, ''n'') feasible for larger values of ''n''.
<langsyntaxhighlight lang="perl">{
my @memo;
sub A {
Line 6,297 ⟶ 6,861:
);
}
}</langsyntaxhighlight>
 
An implementation using the conditional statements 'if', 'elsif' and 'else':
<langsyntaxhighlight lang="perl">sub A {
my ($m, $n) = @_;
if ($m == 0) { $n + 1 }
elsif ($n == 0) { A($m - 1, 1) }
else { A($m - 1, A($m, $n - 1)) }
}</langsyntaxhighlight>
 
An implementation using ternary chaining:
<langsyntaxhighlight lang="perl">sub A {
my ($m, $n) = @_;
$m == 0 ? $n + 1 :
$n == 0 ? A($m - 1, 1) :
A($m - 1, A($m, $n - 1))
}</langsyntaxhighlight>
 
Adding memoization and extra terms:
<langsyntaxhighlight lang="perl">use Memoize; memoize('ack2');
use bigint try=>"GMP";
 
Line 6,330 ⟶ 6,894:
print "ack2(3,4) is ", ack2(3,4), "\n";
print "ack2(4,1) is ", ack2(4,1), "\n";
print "ack2(4,2) has ", length(ack2(4,2)), " digits\n";</langsyntaxhighlight>
{{output}}
<pre>ack2(3,4) is 125
Line 6,338 ⟶ 6,902:
An optimized version, which uses <code>@_</code> as a stack,
instead of recursion. Very fast.
<langsyntaxhighlight Perllang="perl">use strict;
use warnings;
use Math::BigInt;
Line 6,373 ⟶ 6,937:
print "ack(4,2) has ", length(ack(4,2)), " digits\n";
 
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
=== native version ===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">ack</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
Line 6,412 ⟶ 6,976:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 6,428 ⟶ 6,992:
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Ackermann.exw</span>
<span style="color: #008080;">include</span> <span style="color: #7060A8;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 6,511 ⟶ 7,075:
<span style="color: #000000;">ackermann_tests</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 6,531 ⟶ 7,095:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">def ack
var n var m
Line 6,545 ⟶ 7,109:
enddef
 
3 6 ack print nl</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">function ackermann( $m , $n )
{
if ( $m==0 )
Line 6,562 ⟶ 7,126:
 
echo ackermann( 3, 4 );
// prints 125</langsyntaxhighlight>
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
foreach(M in 0..3)
println([m=M,[a(M,N) : N in 0..16]])
Line 6,603 ⟶ 7,167:
a2(3,N) = 8*(2**N - 1) + 5.
a2(M,N) = cond(N == 0,a2(M-1, 1), a2(M-1, a2(M, N-1))).
</syntaxhighlight>
</lang>
 
{{out}}
Line 6,624 ⟶ 7,188:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de ack (X Y)
(cond
((=0 X) (inc Y))
((=0 Y) (ack (dec X) 1))
(T (ack (dec X) (ack X (dec Y)))) ) )</langsyntaxhighlight>
 
=={{header|Piet}}==
Line 6,997 ⟶ 7,561:
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">int main(){
write(ackermann(3,4) + "\n");
}
Line 7,009 ⟶ 7,573:
return ackermann(m-1, ackermann(m, n-1));
}
}</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">Ackerman: procedure (m, n) returns (fixed (30)) recursive;
declare (m, n) fixed (30);
if m = 0 then return (n+1);
Line 7,018 ⟶ 7,582:
else if m > 0 & n > 0 then return (Ackerman(m-1, Ackerman(m, n-1)));
return (0);
end Ackerman;</langsyntaxhighlight>
 
=={{header|PL/SQL}}==
<langsyntaxhighlight PLSQLlang="plsql">DECLARE
 
FUNCTION ackermann(pi_m IN NUMBER,
Line 7,042 ⟶ 7,606:
END LOOP;
END;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 7,076 ⟶ 7,640:
 
=={{header|PostScript}}==
<langsyntaxhighlight lang="postscript">/ackermann{
/n exch def
/m exch def %PostScript takes arguments in the reverse order as specified in the function definition
Line 7,089 ⟶ 7,653:
m 1 sub m n 1 sub ackermann ackermann
}if
}def</langsyntaxhighlight>
{{libheader|initlib}}
<langsyntaxhighlight lang="postscript">/A {
[/.m /.n] let
{
Line 7,098 ⟶ 7,662:
{.m 0 gt .n 0 gt and} {.m pred .m .n pred A A} is?
} cond
end}.</langsyntaxhighlight>
 
=={{header|Potion}}==
<langsyntaxhighlight Potionlang="potion">ack = (m, n):
if (m == 0): n + 1
. elsif (n == 0): ack(m - 1, 1)
Line 7,111 ⟶ 7,675:
ack(m, n) print
" " print.
"\n" print.</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
<langsyntaxhighlight lang="powerbasic">FUNCTION PBMAIN () AS LONG
DIM m AS QUAD, n AS QUAD
 
Line 7,131 ⟶ 7,695:
FUNCTION = Ackermann(m - 1, Ackermann(m, n - 1))
END IF
END FUNCTION</langsyntaxhighlight>
 
=={{header|PowerShell}}==
{{trans|PHP}}
<langsyntaxhighlight lang="powershell">function ackermann ([long] $m, [long] $n) {
if ($m -eq 0) {
return $n + 1
Line 7,145 ⟶ 7,709:
return (ackermann ($m - 1) (ackermann $m ($n - 1)))
}</langsyntaxhighlight>
Building an example table (takes a while to compute, though, especially for the last three numbers; also it fails with the last line in Powershell v1 since the maximum recursion depth is only 100 there):
<langsyntaxhighlight lang="powershell">foreach ($m in 0..3) {
foreach ($n in 0..6) {
Write-Host -NoNewline ("{0,5}" -f (ackermann $m $n))
}
Write-Host
}</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7
Line 7,160 ⟶ 7,724:
 
===A More "PowerShelly" Way===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Ackermann ([int64]$m, [int64]$n)
{
Line 7,175 ⟶ 7,739:
return (Get-Ackermann ($m - 1) (Get-Ackermann $m ($n - 1)))
}
</syntaxhighlight>
</lang>
Save the result to an array (for possible future use?), then display it using the <code>Format-Wide</code> cmdlet:
<syntaxhighlight lang="powershell">
<lang PowerShell>
$ackermann = 0..3 | ForEach-Object {$m = $_; 0..6 | ForEach-Object {Get-Ackermann $m $_}}
 
$ackermann | Format-Wide {"{0,3}" -f $_} -Column 7 -Force
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 7,191 ⟶ 7,755:
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">int ackermann(int m, int n) {
if (m == 0)
return n + 1;
Line 7,209 ⟶ 7,773:
println();
}
}</langsyntaxhighlight>
 
{{Out}}
Line 7,223 ⟶ 7,787:
m = 5 it throws 'maximum recursion depth exceeded'
 
<langsyntaxhighlight lang="python">from __future__ import print_function
 
def setup():
Line 7,238 ⟶ 7,802:
return ackermann(m - 1, 1)
else:
return ackermann(m - 1, ackermann(m, n - 1))</langsyntaxhighlight>
 
==={{header|Processing.R}}===
Line 7,244 ⟶ 7,808:
Processing.R may exceed its stack depth at ~n==6 and returns null.
 
<langsyntaxhighlight lang="r">setup <- function() {
for (m in 0:3) {
for (n in 0:4) {
Line 7,261 ⟶ 7,825:
ackermann(m-1, ackermann(m, n-1))
}
}</langsyntaxhighlight>
 
{{out}}
Line 7,271 ⟶ 7,835:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">:- table ack/3. % memoization reduces the execution time of ack(4,1,X) from several
% minutes to about one second on a typical desktop computer.
ack(0, N, Ans) :- Ans is N+1.
ack(M, 0, Ans) :- M>0, X is M-1, ack(X, 1, Ans).
ack(M, N, Ans) :- M>0, N>0, X is M-1, Y is N-1, ack(M, Y, Ans2), ack(X, Ans2, Ans).</langsyntaxhighlight>
 
"Pure" Prolog Version (Uses Peano arithmetic instead of is/2):
<syntaxhighlight lang="prolog">ack(0,N,s(N)).
ack(s(M),0,P):- ack(M,s(0),P).
ack(s(M),s(N),P):- ack(s(M),N,S), ack(M,S,P).
 
% Peano's first axiom in Prolog is that s(0) AND s(s(N)):- s(N)
% Thanks to this we don't need explicit N > 0 checks.
% Nor explicit arithmetic operations like X is M-1.
% Recursion and unification naturally decrement s(N) to N.
% But: Prolog clauses are relations and cannot be replaced by their result, like functions.
% Because of this we do need an extra argument to hold the output of the function.
% And we also need an additional call to the function in the last clause.
 
% Example input/output:
% ?- ack(s(0),s(s(0)),P).
% P = s(s(s(s(0)))) ;
% false.
</syntaxhighlight>
 
=={{header|Pure}}==
<langsyntaxhighlight lang="pure">A 0 n = n+1;
A m 0 = A (m-1) 1 if m > 0;
A m n = A (m-1) (A m (n-1)) if m > 0 && n > 0;</langsyntaxhighlight>
 
=={{header|Pure Data}}==
<syntaxhighlight lang="pure data">
<lang Pure Data>
#N canvas 741 265 450 436 10;
#X obj 83 111 t b l;
Line 7,328 ⟶ 7,911:
#X connect 15 0 10 0;
#X connect 16 0 10 0;
#X connect 17 0 0 0;</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.q Ackermann(m, n)
If m = 0
ProcedureReturn n + 1
Line 7,341 ⟶ 7,924:
EndProcedure
 
Debug Ackermann(3,4)</langsyntaxhighlight>
 
=={{header|Purity}}==
<langsyntaxhighlight Puritylang="purity">data Iter = f => FoldNat <const $f One, $f>
data Ackermann = FoldNat <const Succ, Iter></langsyntaxhighlight>
 
=={{header|Python}}==
===Python: Explicitly recursive===
{{works with|Python|2.5}}
<langsyntaxhighlight lang="python">def ack1(M, N):
return (N + 1) if M == 0 else (
ack1(M-1, 1) if N == 0 else ack1(M-1, ack1(M, N-1)))</langsyntaxhighlight>
Another version:
<langsyntaxhighlight lang="python">from functools import lru_cache
 
@lru_cache(None)
Line 7,363 ⟶ 7,946:
return ack2(M - 1, 1)
else:
return ack2(M - 1, ack2(M, N - 1))</langsyntaxhighlight>
{{out|Example of use}}
<langsyntaxhighlight lang="python">>>> import sys
>>> sys.setrecursionlimit(3000)
>>> ack1(0,0)
Line 7,374 ⟶ 7,957:
1
>>> ack2(3,4)
125</langsyntaxhighlight>
From the Mathematica ack3 example:
<langsyntaxhighlight lang="python">def ack2(M, N):
return (N + 1) if M == 0 else (
(N + 2) if M == 1 else (
(2*N + 3) if M == 2 else (
(8*(2**N - 1) + 5) if M == 3 else (
ack2(M-1, 1) if N == 0 else ack2(M-1, ack2(M, N-1))))))</langsyntaxhighlight>
Results confirm those of Mathematica for ack(4,1) and ack(4,2)
 
Line 7,387 ⟶ 7,970:
The heading is more correct than saying the following is iterative as an explicit stack is used to replace explicit recursive function calls. I don't think this is what Comp. Sci. professors mean by iterative.
 
<langsyntaxhighlight lang="python">from collections import deque
 
def ack_ix(m, n):
Line 7,411 ⟶ 7,994:
stack.extend([m-1, m, n-1])
 
return stack[0]</langsyntaxhighlight>
 
{{out}}
Line 7,436 ⟶ 8,019:
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery"> forward is ackermann ( m n --> r )
[ over 0 = iff
[ nip 1 + ] done
Line 7,445 ⟶ 8,028:
ackermann ackermann ] resolves ackermann ( m n --> r )
 
3 10 ackermann echo</langsyntaxhighlight>
'''Output:'''
<pre>8189</pre>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">ackermann <- function(m, n) {
if ( m == 0 ) {
n+1
Line 7,458 ⟶ 8,041:
ackermann(m-1, ackermann(m, n-1))
}
}</langsyntaxhighlight>
<langsyntaxhighlight Rlang="r">for ( i in 0:3 ) {
print(ackermann(i, 4))
}</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (ackermann m n)
Line 7,470 ⟶ 8,053:
[(zero? n) (ackermann (sub1 m) 1)]
[else (ackermann (sub1 m) (ackermann m (sub1 n)))]))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 7,476 ⟶ 8,059:
{{works with|Rakudo|2018.03}}
 
<syntaxhighlight lang="raku" perl6line>sub A(Int $m, Int $n) {
if $m == 0 { $n + 1 }
elsif $n == 0 { A($m - 1, 1) }
else { A($m - 1, A($m, $n - 1)) }
}</langsyntaxhighlight>
An implementation using multiple dispatch:
<syntaxhighlight lang="raku" perl6line>multi sub A(0, Int $n) { $n + 1 }
multi sub A(Int $m, 0 ) { A($m - 1, 1) }
multi sub A(Int $m, Int $n) { A($m - 1, A($m, $n - 1)) }</langsyntaxhighlight>
Note that in either case, Int is defined to be arbitrary precision in Raku.
 
Here's a caching version of that, written in the sigilless style, with liberal use of Unicode, and the extra optimizing terms to make A(4,2) possible:
<syntaxhighlight lang="raku" perl6line>proto A(Int \𝑚, Int \𝑛) { (state @)[𝑚][𝑛] //= {*} }
 
multi A(0, Int \𝑛) { 𝑛 + 1 }
Line 7,500 ⟶ 8,083:
# Testing:
say A(4,1);
say .chars, " digits starting with ", .substr(0,50), "..." given A(4,2);</langsyntaxhighlight>
{{out}}
<pre>65533
Line 7,513 ⟶ 8,096:
]
]</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout 'A(3,9) = ' <A 3 9>>;
};
 
A {
0 s.N = <+ s.N 1>;
s.M 0 = <A <- s.M 1> 1>;
s.M s.N = <A <- s.M 1> <A s.M <- s.N 1>>>;
};</syntaxhighlight>
{{out}}
<pre>A(3,9) = 4093</pre>
 
=={{header|ReScript}}==
<langsyntaxhighlight ReScriptlang="rescript">let _m = Sys.argv[2]
let _n = Sys.argv[3]
 
Line 7,529 ⟶ 8,125:
 
Js.log("ackermann(" ++ _m ++ ", " ++ _n ++ ") = "
++ string_of_int(a(m, n)))</langsyntaxhighlight>
 
{{out}}
Line 7,542 ⟶ 8,138:
=={{header|REXX}}==
===no optimization===
<langsyntaxhighlight lang="rexx">/*REXX program calculates and displays some values for the Ackermann function. */
/*╔════════════════════════════════════════════════════════════════════════╗
║ Note: the Ackermann function (as implemented here) utilizes deep ║
Line 7,566 ⟶ 8,162:
if m==0 then return n+1
if n==0 then return ackermann(m-1, 1)
return ackermann(m-1, ackermann(m, n-1) )</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre style="height:60ex">
Line 7,647 ⟶ 8,243:
 
===optimized for m ≤ 2===
<langsyntaxhighlight lang="rexx">/*REXX program calculates and displays some values for the Ackermann function. */
high=24
do j=0 to 3; say
Line 7,667 ⟶ 8,263:
if n==0 then return ackermann(m-1, 1)
if m==2 then return n + 3 + n
return ackermann(m-1, ackermann(m, n-1) )</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre style="height:60ex">
Line 7,751 ⟶ 8,347:
<br><br>If the &nbsp; '''numeric digits 100''' &nbsp; were to be increased to &nbsp; '''20000''', &nbsp; then the value of &nbsp; '''Ackermann(4,2)''' &nbsp;
<br>(the last line of output) &nbsp; would be presented with the full &nbsp; '''19,729''' &nbsp; decimal digits.
<langsyntaxhighlight lang="rexx">/*REXX program calculates and displays some values for the Ackermann function. */
numeric digits 100 /*use up to 100 decimal digit integers.*/
/*╔═════════════════════════════════════════════════════════════╗
Line 7,786 ⟶ 8,382:
end
if n==0 then return ackermann(m-1, 1)
return ackermann(m-1, ackermann(m, n-1) )</langsyntaxhighlight>
Output note: &nbsp; <u>none</u> of the numbers shown below use recursion to compute.
 
Line 7,874 ⟶ 8,470:
=={{header|Ring}}==
{{trans|C#}}
<langsyntaxhighlight lang="ring">for m = 0 to 3
for n = 0 to 4
see "Ackermann(" + m + ", " + n + ") = " + Ackermann(m, n) + nl
Line 7,892 ⟶ 8,488:
ok
ok
Raise("Incorrect Numerical input !!!") </langsyntaxhighlight>
{{out}}
<pre>Ackermann(0, 0) = 1
Line 7,915 ⟶ 8,511:
Ackermann(3, 4) = 125</pre>
 
=={{header|RiscRISC-V Assembly}}==
the basic recursive function, because memorization and other improvements would blow the clarity.
<langsyntaxhighlight Risclang="risc-Vv">ackermann: #x: a1, y: a2, return: a0
beqz a1, npe #case m = 0
beqz a2, mme #case m > 0 & n = 0
Line 7,944 ⟶ 8,540:
addi sp, sp, 4
jr t0, 0
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
{{works with|RPL|HP49-C}}
« '''CASE'''
OVER NOT '''THEN''' NIP 1 + '''END'''
DUP NOT '''THEN''' DROP 1 - 1 <span style="color:blue">ACKER</span> '''END'''
OVER 1 - ROT ROT 1 - <span style="color:blue">ACKER</span> <span style="color:blue">ACKER</span>
'''END'''
» '<span style="color:blue">ACKER</span>' STO
 
3 4 <span style="color:blue">ACKER</span>
{{out}}
<pre>
1: 125
</pre>
Runs in 7 min 13 secs on a HP-50g. Speed could be increased by replacing every <code>1</code> by <code>1.</code>, which would force calculations to be made with floating-point numbers, but we would then lose the arbitrary precision.
 
=={{header|Ruby}}==
{{trans|Ada}}
<langsyntaxhighlight lang="ruby">def ack(m, n)
if m == 0
n + 1
Line 7,956 ⟶ 8,568:
ack(m-1, ack(m, n-1))
end
end</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="ruby">(0..3).each do |m|
puts (0..6).map { |n| ack(m, n) }.join(' ')
end</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7
Line 7,968 ⟶ 8,580:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">print ackermann(1, 2)
function ackermann(m, n)
Line 7,974 ⟶ 8,586:
if (m > 0) and (n = 0) then ackermann = ackermann((m - 1), 1)
if (m > 0) and (n > 0) then ackermann = ackermann((m - 1), ackermann(m, (n - 1)))
end function</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn ack(m: isize, n: isize) -> isize {
if m == 0 {
n + 1
Line 7,991 ⟶ 8,603:
println!("{}", a); // 125
}
</syntaxhighlight>
</lang>
 
Or:
 
<langsyntaxhighlight lang="rust">
fn ack(m: u64, n: u64) -> u64 {
match (m, n) {
Line 8,003 ⟶ 8,615:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
 
ackermann(m, n:INT):INT
Line 8,024 ⟶ 8,636:
end;
end;
end;</langsyntaxhighlight>
Instead of <code>INT</code>, the class <code>INTI</code> could be used, even though we need to use a workaround since in the GNU Sather v1.2.3 compiler the INTI literals are not implemented yet.
<langsyntaxhighlight lang="sather">class MAIN is
 
ackermann(m, n:INTI):INTI is
Line 8,044 ⟶ 8,656:
end;
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
 
<langsyntaxhighlight lang="scala">def ack(m: BigInt, n: BigInt): BigInt = {
if (m==0) n+1
else if (n==0) ack(m-1, 1)
else ack(m-1, ack(m, n-1))
}</langsyntaxhighlight>
{{out|Example}}
<pre>
Line 8,059 ⟶ 8,671:
</pre>
Memoized version using a mutable hash map:
<langsyntaxhighlight lang="scala">val ackMap = new mutable.HashMap[(BigInt,BigInt),BigInt]
def ackMemo(m: BigInt, n: BigInt): BigInt = {
ackMap.getOrElseUpdate((m,n), ack(m,n))
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (A m n)
(cond
((= m 0) (+ n 1))
((= n 0) (A (- m 1) 1))
(else (A (- m 1) (A m (- n 1))))))</langsyntaxhighlight>
 
An improved solution that uses a lazy data structure, streams, and defines [[Knuth up-arrow]]s to calculate iterative exponentiation:
 
<langsyntaxhighlight lang="scheme">(define (A m n)
(letrec ((A-stream
(cons-stream
Line 8,101 ⟶ 8,713:
(cond ((= b 0) 1)
((= n 1) (expt a b))
(else (loop (-1+ n) (loop n (-1+ b)))))))</langsyntaxhighlight>
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">clear
function acker=ackermann(m,n)
global calls
Line 8,126 ⟶ 8,738:
printacker(i,j)
end
end</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">ackermann(0,0)=1 calls=1
Line 8,158 ⟶ 8,770:
 
=={{header|Seed7}}==
===Basic version===
<lang seed7>const func integer: ackermann (in integer: m, in integer: n) is func
<syntaxhighlight lang="seed7">const func integer: ackermann (in integer: m, in integer: n) is func
result
var integer: result is 0;
Line 8,169 ⟶ 8,782:
result := ackermann(pred(m), ackermann(m, pred(n)));
end if;
end func;</langsyntaxhighlight>
Original source: [http://seed7.sourceforge.net/algorith/math.htm#ackermann]
===Improved version===
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
const func bigInteger: ackermann (in bigInteger: m, in bigInteger: n) is func
result
var bigInteger: ackermann is 0_;
begin
case m of
when {0_}: ackermann := succ(n);
when {1_}: ackermann := n + 2_;
when {2_}: ackermann := 3_ + 2_ * n;
when {3_}: ackermann := 5_ + 8_ * pred(2_ ** ord(n));
otherwise:
if n = 0_ then
ackermann := ackermann(pred(m), 1_);
else
ackermann := ackermann(pred(m), ackermann(m, pred(n)));
end if;
end case;
end func;
const proc: main is func
local
var bigInteger: m is 0_;
var bigInteger: n is 0_;
var string: stri is "";
begin
for m range 0_ to 3_ do
for n range 0_ to 9_ do
writeln("A(" <& m <& ", " <& n <& ") = " <& ackermann(m, n));
end for;
end for;
writeln("A(4, 0) = " <& ackermann(4_, 0_));
writeln("A(4, 1) = " <& ackermann(4_, 1_));
stri := str(ackermann(4_, 2_));
writeln("A(4, 2) = (" <& length(stri) <& " digits)");
writeln(stri[1 len 80]);
writeln("...");
writeln(stri[length(stri) - 79 ..]);
end func;</syntaxhighlight>
Original source: [https://seed7.sourceforge.net/algorith/math.htm#bigInteger_ackermann]
 
{{out}}
<pre>
A(0, 0) = 1
A(0, 1) = 2
A(0, 2) = 3
A(0, 3) = 4
A(0, 4) = 5
A(0, 5) = 6
A(0, 6) = 7
A(0, 7) = 8
A(0, 8) = 9
A(0, 9) = 10
A(1, 0) = 2
A(1, 1) = 3
A(1, 2) = 4
A(1, 3) = 5
A(1, 4) = 6
A(1, 5) = 7
A(1, 6) = 8
A(1, 7) = 9
A(1, 8) = 10
A(1, 9) = 11
A(2, 0) = 3
A(2, 1) = 5
A(2, 2) = 7
A(2, 3) = 9
A(2, 4) = 11
A(2, 5) = 13
A(2, 6) = 15
A(2, 7) = 17
A(2, 8) = 19
A(2, 9) = 21
A(3, 0) = 5
A(3, 1) = 13
A(3, 2) = 29
A(3, 3) = 61
A(3, 4) = 125
A(3, 5) = 253
A(3, 6) = 509
A(3, 7) = 1021
A(3, 8) = 2045
A(3, 9) = 4093
A(4, 0) = 13
A(4, 1) = 65533
A(4, 2) = (19729 digits)
20035299304068464649790723515602557504478254755697514192650169737108940595563114
...
84717124577965048175856395072895337539755822087777506072339445587895905719156733
</pre>
 
=={{header|SETL}}==
<langsyntaxhighlight SETLlang="setl">program ackermann;
 
(for m in [0..3])
Line 8,183 ⟶ 8,888:
end proc;
 
end program;</langsyntaxhighlight>
 
=={{header|Shen}}==
<langsyntaxhighlight lang="shen">(define ack
0 N -> (+ N 1)
M 0 -> (ack (- M 1) 1)
M N -> (ack (- M 1)
(ack M (- N 1))))</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func A(m, n) {
m == 0 ? (n + 1)
: (n == 0 ? (A(m - 1, 1))
: (A(m - 1, A(m, n - 1))));
}</langsyntaxhighlight>
 
Alternatively, using multiple dispatch:
<langsyntaxhighlight lang="ruby">func A((0), n) { n + 1 }
func A(m, (0)) { A(m - 1, 1) }
func A(m, n) { A(m-1, A(m, n-1)) }</langsyntaxhighlight>
 
Calling the function:
<langsyntaxhighlight lang="ruby">say A(3, 2); # prints: 29</langsyntaxhighlight>
 
=={{header|Simula}}==
as modified by R. Péter and R. Robinson:
<langsyntaxhighlight Simulalang="simula"> BEGIN
INTEGER procedure
Ackermann(g, p); SHORT INTEGER g, p;
Line 8,223 ⟶ 8,928:
outint(Ackermann(g, p), 0); outimage
END
END</langsyntaxhighlight>
{{Output}}
<pre>Ackermann(4, 0) = 13
Line 8,233 ⟶ 8,938:
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">m@(Integer traits) ackermann: n@(Integer traits)
[
m isZero
Line 8,241 ⟶ 8,946:
ifTrue: [m - 1 ackermann: n]
ifFalse: [m - 1 ackermann: (m ackermann: n - 1)]]
].</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">|ackermann|
ackermann := [ :n :m |
(n = 0) ifTrue: [ (m + 1) ]
Line 8,258 ⟶ 8,963:
 
(ackermann value: 0 value: 0) displayNl.
(ackermann value: 3 value: 4) displayNl.</langsyntaxhighlight>
 
=={{header|SmileBASIC}}==
<langsyntaxhighlight lang="smilebasic">DEF ACK(M,N)
IF M==0 THEN
RETURN N+1
Line 8,269 ⟶ 8,974:
RETURN ACK(M-1,ACK(M,N-1))
ENDIF
END</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
{{works with|Macro Spitbol}}
Both Snobol4+ and CSnobol stack overflow, at ack(3,3) and ack(3,4), respectively.
<langsyntaxhighlight SNOBOL4lang="snobol4">define('ack(m,n)') :(ack_end)
ack ack = eq(m,0) n + 1 :s(return)
ack = eq(n,0) ack(m - 1,1) :s(return)
Line 8,285 ⟶ 8,990:
output = str; str = ''
n = 0; m = lt(m,3) m + 1 :s(L1)
end</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7
Line 8,293 ⟶ 8,998:
 
=={{header|SNUSP}}==
<langsyntaxhighlight lang="snusp"> /==!/==atoi=@@@-@-----#
| | Ackermann function
| | /=========\!==\!====\ recursion:
Line 8,304 ⟶ 9,009:
? ? | \<<<+>+>>-/
\>>+<<-/!==========/
# #</langsyntaxhighlight>
One could employ [[:Category:Recursion|tail recursion]] elimination by replacing "@/#" with "/" in two places above.
 
=={{header|SPAD}}==
{{works with|FriCAS, OpenAxiom, Axiom}}
<syntaxhighlight lang="spad">
<lang SPAD>
NNI ==> NonNegativeInteger
 
Line 8,321 ⟶ 9,026:
-- Example
matrix [[A(i,j) for i in 0..3] for j in 0..3]
</syntaxhighlight>
</lang>
 
{{out}}
Line 8,339 ⟶ 9,044:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<langsyntaxhighlight lang="sql pl">
--#SET TERMINATOR @
 
Line 8,379 ⟶ 9,084:
END WHILE;
END @
</syntaxhighlight>
</lang>
Output:
<pre>
Line 8,422 ⟶ 9,127:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun a (0, n) = n+1
| a (m, 0) = a (m-1, 1)
| a (m, n) = a (m-1, a (m, n-1))</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">mata
function ackermann(m,n) {
if (m==0) {
Line 8,443 ⟶ 9,148:
11
125
end</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func ackerman(m:Int, n:Int) -> Int {
if m == 0 {
return n+1
Line 8,454 ⟶ 9,159:
return ackerman(m-1, ackerman(m, n-1))
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
===Simple===
{{trans|Ruby}}
<langsyntaxhighlight lang="tcl">proc ack {m n} {
if {$m == 0} {
expr {$n + 1}
Line 8,467 ⟶ 9,172:
ack [expr {$m - 1}] [ack $m [expr {$n - 1}]]
}
}</langsyntaxhighlight>
===With Tail Recursion===
With Tcl 8.6, this version is preferred (though the language supports tailcall optimization, it does not apply it automatically in order to preserve stack frame semantics):
<langsyntaxhighlight lang="tcl">proc ack {m n} {
if {$m == 0} {
expr {$n + 1}
Line 8,478 ⟶ 9,183:
tailcall ack [expr {$m - 1}] [ack $m [expr {$n - 1}]]
}
}</langsyntaxhighlight>
===To Infinity… and Beyond!===
If we want to explore the higher reaches of the world of Ackermann's function, we need techniques to really cut the amount of computation being done.
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# A memoization engine, from http://wiki.tcl.tk/18152
Line 8,536 ⟶ 9,241:
# Some small tweaks...
interp recursionlimit {} 100000
interp alias {} ack {} cacheable ack</langsyntaxhighlight>
But even with all this, you still run into problems calculating <math>\mathit{ack}(4,3)</math> as that's kind-of large…
 
Line 8,543 ⟶ 9,248:
This program assumes the variables N and M are the arguments of the function, and that the list L1 is empty. It stores the result in the system variable ANS. (Program names can be no longer than 8 characters, so I had to truncate the function's name.)
 
<langsyntaxhighlight lang="ti83b">PROGRAM:ACKERMAN
:If not(M
:Then
Line 8,563 ⟶ 9,268:
:prgmACKERMAN
:End
:End</langsyntaxhighlight>
 
Here is a handler function that makes the previous function easier to use. (You can name it whatever you want.)
 
<langsyntaxhighlight lang="ti83b">PROGRAM:AHANDLER
:0→dim(L1
:Prompt M
:Prompt N
:prgmACKERMAN
:Disp Ans</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
<langsyntaxhighlight lang="ti89b">Define A(m,n) = when(m=0, n+1, when(n=0, A(m-1,1), A(m-1, A(m, n-1))))</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
<langsyntaxhighlight TorqueScriptlang="torquescript">function ackermann(%m,%n)
{
if(%m==0)
Line 8,586 ⟶ 9,291:
if(%m>0&&%n>0)
return ackermann(%m-1,ackermann(%m,%n-1));
}</langsyntaxhighlight>
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule: {
Ack: Lambda<Int Int Int>(λ m Int() n Int()
(if (not m) (ret (+ n 1)))
(if (not n) (ret (exec Ack (- m 1) 1)))
(ret (exec Ack (- m 1) (exec Ack m (- n 1))))
),
_start: (λ (textout (exec Ack 3 1) "\n"
(exec Ack 3 2) "\n"
(exec Ack 3 3)))
}</syntaxhighlight>
{{out}}
<pre>
13
29
61
</pre>
 
=={{header|TSE SAL}}==
<langsyntaxhighlight TSESALlang="tsesal">// library: math: get: ackermann: recursive <description></description> <version>1.0.0.0.5</version> <version control></version control> (filenamemacro=getmaare.s) [kn, ri, tu, 27-12-2011 14:46:59]
INTEGER PROC FNMathGetAckermannRecursiveI( INTEGER mI, INTEGER nI )
IF ( mI == 0 )
Line 8,606 ⟶ 9,331:
IF ( NOT ( Ask( "math: get: ackermann: recursive: n = ", s2, _EDIT_HISTORY_ ) ) AND ( Length( s2 ) > 0 ) ) RETURN() ENDIF
Message( FNMathGetAckermannRecursiveI( Val( s1 ), Val( s2 ) ) ) // gives e.g. 9
END</langsyntaxhighlight>
 
=={{header|TXR}}==
Line 8,613 ⟶ 9,338:
with memoization.
 
<langsyntaxhighlight lang="txrlisp">(defmacro defmemofun (name (. args) . body)
(let ((hash (gensym "hash-"))
(argl (gensym "args-"))
Line 8,634 ⟶ 9,359:
(each ((i (range 0 3)))
(each ((j (range 0 4)))
(format t "ack(~a, ~a) = ~a\n" i j (ack i j))))</langsyntaxhighlight>
 
{{out}}
Line 8,661 ⟶ 9,386:
=={{header|UNIX Shell}}==
{{works with|Bash}}
<langsyntaxhighlight lang="bash">ack() {
local m=$1
local n=$2
Line 8,671 ⟶ 9,396:
ack $((m-1)) $(ack $m $((n-1)))
fi
}</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="bash">for ((m=0;m<=3;m++)); do
for ((n=0;n<=6;n++)); do
ack $m $n
Line 8,679 ⟶ 9,404:
done
echo
done</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7
Line 8,685 ⟶ 9,410:
3 5 7 9 11 13 15
5 13 29 61 125 253 509</pre>
 
=={{header|Ursalang}}==
<syntaxhighlight lang="ursalang">let A = fn(m, n) {
if m == 0 {n + 1}
else if m > 0 and n == 0 {A(m - 1, 1)}
else {A(m - 1, A(m, n - 1))}
}
print(A(0, 0))
print(A(3, 4))
print(A(3, 1))</syntaxhighlight>
 
=={{header|Ursala}}==
Anonymous recursion is the usual way of doing things like this.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 8,695 ⟶ 9,431:
~&al^?\successor@ar ~&ar?(
^R/~&f ^/predecessor@al ^|R/~& ^|/~& predecessor,
^|R/~& ~&\1+ predecessor@l)</langsyntaxhighlight>
test program for the first 4 by 7 numbers:
<langsyntaxhighlight Ursalalang="ursala">#cast %nLL
 
test = block7 ackermann*K0 iota~~/4 7</langsyntaxhighlight>
{{out}}
<pre>
Line 8,710 ⟶ 9,446:
=={{header|V}}==
{{trans|Joy}}
<langsyntaxhighlight lang="v">[ack
[ [pop zero?] [popd succ]
[zero?] [pop pred 1 ack]
[true] [[dup pred swap] dip pred ack ack ]
] when].</langsyntaxhighlight>
using destructuring view
<langsyntaxhighlight lang="v">[ack
[ [pop zero?] [ [m n : [n succ]] view i]
[zero?] [ [m n : [m pred 1 ack]] view i]
[true] [ [m n : [m pred m n pred ack ack]] view i]
] when].</langsyntaxhighlight>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">uint64 ackermann(uint64 m, uint64 n) {
if (m == 0) return n + 1;
if (n == 0) return ackermann(m - 1, 1);
Line 8,735 ⟶ 9,471:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 8,782 ⟶ 9,518:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Private Function Ackermann_function(m As Variant, n As Variant) As Variant
Dim result As Variant
Debug.Assert m >= 0
Line 8,810 ⟶ 9,546:
Debug.Print
Next i
End Sub</langsyntaxhighlight>{{out}}
<pre> n= 0 1 2 3 4 5 6 7
m=0 1 2 3 4 5 6 7 8
Line 8,820 ⟶ 9,556:
Based on BASIC version. Uncomment all the lines referring to <code>depth</code> and see just how deep the recursion goes.
;Implementation
<langsyntaxhighlight lang="vb">option explicit
'~ dim depth
function ack(m, n)
Line 8,839 ⟶ 9,575:
end if
end function</langsyntaxhighlight>
;Invocation
<langsyntaxhighlight lang="vb">wscript.echo ack( 1, 10 )
'~ depth = 0
wscript.echo ack( 2, 1 )
'~ depth = 0
wscript.echo ack( 4, 4 )</langsyntaxhighlight>
{{out}}
<pre>
Line 8,856 ⟶ 9,592:
{{trans|Rexx}}
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
Dim calls As Long
Line 8,885 ⟶ 9,621:
End If
End If
End Function 'ackermann</langsyntaxhighlight>
{{Out}}
<pre style="height:20ex">ackermann( 0 , 0 )= 1 calls= 1
Line 8,930 ⟶ 9,666:
ackermann( 4 , 1 )= out of stack space</pre>
 
=={{header|V (Vlang)}}==
<langsyntaxhighlight lang="go">fn ackermann(m int, n int ) int {
if m == 0 {
return n + 1
Line 8,948 ⟶ 9,684:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 8,974 ⟶ 9,710:
 
=={{header|Wart}}==
<langsyntaxhighlight lang="wart">def (ackermann m n)
(if m=0
n+1
Line 8,980 ⟶ 9,716:
(ackermann m-1 1)
:else
(ackermann m-1 (ackermann m n-1)))</langsyntaxhighlight>
 
=={{header|WDTE}}==
<langsyntaxhighlight WDTElang="wdte">let memo a m n => true {
== m 0 => + n 1;
== n 0 => a (- m 1) 1;
true => a (- m 1) (a m (- n 1));
};</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">// To use recursion definition and declaration must be on separate lines
var Ackermann
Ackermann = Fn.new {|m, n|
Line 9,003 ⟶ 9,739:
var p2 = pair[1]
System.print("A[%(p1), %(p2)] = %(Ackermann.call(p1, p2))")
}</langsyntaxhighlight>
 
{{out}}
Line 9,018 ⟶ 9,754:
{{works with|nasm}}
{{works with|windows}}
<langsyntaxhighlight lang="asm">
section .text
 
Line 9,048 ⟶ 9,784:
call ack1 ;return ack(M-1,1)
ret
</syntaxhighlight>
</lang>
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="lisp">(defun ackermann (m n)
(cond
((= m 0) (+ n 1))
((= n 0) (ackermann (- m 1) 1))
(t (ackermann (- m 1) (ackermann m (- n 1))))))</langsyntaxhighlight>
Test it:
<langsyntaxhighlight lang="lisp">(print (ackermann 3 9))</langsyntaxhighlight>
Output (after a very perceptible pause):
<pre>4093</pre>
That worked well. Test it again:
<langsyntaxhighlight lang="lisp">(print (ackermann 4 1))</langsyntaxhighlight>
Output (after another pause):
<pre>Abort: control stack overflow
Line 9,067 ⟶ 9,803:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
func Ackermann(M, N);
Line 9,082 ⟶ 9,818:
CrLf(0);
];
]</langsyntaxhighlight>
Recursion overflows the stack if either M or N is extended by a single count.
{{out}}
Line 9,095 ⟶ 9,831:
 
The following named template calculates the Ackermann function:
<langsyntaxhighlight lang="xml">
<xsl:template name="ackermann">
<xsl:param name="m"/>
Line 9,125 ⟶ 9,861:
</xsl:choose>
</xsl:template>
</syntaxhighlight>
</lang>
 
Here it is as part of a template
<langsyntaxhighlight lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
Line 9,174 ⟶ 9,910:
</xsl:template>
</xsl:stylesheet>
</syntaxhighlight>
</lang>
 
Which will transform this input
<langsyntaxhighlight lang="xml">
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="ackermann.xslt"?>
Line 9,326 ⟶ 10,062:
</args>
</arguments>
</syntaxhighlight>
</lang>
 
into this output
Line 9,369 ⟶ 10,105:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">sub ack(M,N)
if M = 0 return N + 1
if N = 0 return ack(M-1,1)
Line 9,376 ⟶ 10,112:
 
print ack(3, 4)
</syntaxhighlight>
</lang>
 
What smart code can get. Fast as lightning!
{{trans|Phix}}
<langsyntaxhighlight Yabasiclang="yabasic">sub ack(m, n)
if m=0 then
return n+1
Line 9,407 ⟶ 10,143:
end sub
Ackermann()</langsyntaxhighlight>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">func ack(m, n) {
if(m == 0)
return n + 1;
Line 9,417 ⟶ 10,153:
else
return ack(m - 1, ack(m, n - 1));
}</langsyntaxhighlight>
Example invocation:
<langsyntaxhighlight lang="yorick">for(m = 0; m <= 3; m++) {
for(n = 0; n <= 6; n++)
write, format="%d ", ack(m, n);
write, "";
}</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7
Line 9,430 ⟶ 10,166:
5 13 29 61 125 253 509</pre>
 
{{omit from|LaTeX}}
{{omit from|Make}}
{{omit from|PlainTeX}}
 
=={{header|Z80 Assembly}}==
This function does 16-bit math. Sjasmplus syntax, CP/M executable.
<langsyntaxhighlight lang="z80"> OPT --syntax=abf : OUTPUT "ackerman.com"
ORG $100
jr demo_start
Line 9,543 ⟶ 10,276:
 
txt_m_is: db "m=$"
crlf: db 10,13,'$'</langsyntaxhighlight>
{{out}}
<pre>
Line 9,555 ⟶ 10,288:
Source -> http://ideone.com/53FzPA
Compiled -> http://ideone.com/OlS7zL
<langsyntaxhighlight lang="zed">(A) m n
comment:
(=) m 0
Line 9,583 ⟶ 10,316:
comment:
#true
(003) "=" n1 n2</langsyntaxhighlight>
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">pub fn ack(m: u64, n: u64) u64 {
if (m == 0) return n + 1;
if (n == 0) return ack(m - 1, 1);
Line 9,602 ⟶ 10,335:
try stdout.print("\n", .{});
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9
Line 9,611 ⟶ 10,344:
=={{header|ZX Spectrum Basic}}==
{{trans|BASIC256}}
<langsyntaxhighlight lang="zxbasic">10 DIM s(2000,3)
20 LET s(1,1)=3: REM M
30 LET s(1,2)=7: REM N
Line 9,629 ⟶ 10,362:
190 LET s(lev-1,3)=s(lev,3)
200 LET lev=lev-1
210 RETURN </langsyntaxhighlight>
{{out}}
<pre>A(3,7) = 1021</pre>
{{omit from|LaTeX}}
{{omit from|Make}}
{{omit from|PlainTeX}}
1,150

edits