Ackermann function: Difference between revisions

added RPL
m (syntax highlighting fixup automation)
(added RPL)
(40 intermediate revisions by 23 users not shown)
Line 34:
{{trans|Python}}
 
<syntaxhighlight lang="11l">F ack2(m, n) -> Int
R I m == 0 {(n + 1)
} E I m == 1 {(n + 2)
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.
<syntaxhighlight lang="360asm">* Ackermann function 07/09/2015
&LAB XDECO &REG,&TARGET
.*-----------------------------------------------------------------*
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).
 
<syntaxhighlight lang="68000devpac">;
; Ackermann function for Motorola 68000 under AmigaOs 2+ by Thorham
;
Line 317:
and <code>n ∊ [0,9)</code>, on a real 8080 this takes a little over two minutes.
 
<syntaxhighlight lang="8080asm"> org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 410:
This code does 16-bit math just like the 8080 version.
 
<syntaxhighlight lang="asm"> cpu 8086
bits 16
org 100h
Line 479:
 
=={{header|8th}}==
<syntaxhighlight lang=Forth"forth">
\ Ackermann function, illustrating use of "memoization".
 
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"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program ackermann64.s */
Line 739:
=={{header|ABAP}}==
 
<syntaxhighlight lang=ABAP"abap">
REPORT zhuberv_ackermann.
 
Line 786:
WRITE: / lv_result.
</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang=Action"action!">DEFINE MAXSIZE="1000"
CARD ARRAY stack(MAXSIZE)
CARD stacksize=[0]
Line 872 ⟶ 907:
 
=={{header|ActionScript}}==
<syntaxhighlight lang="actionscript">public function ackermann(m:uint, n:uint):uint
{
if (m == 0)
Line 887 ⟶ 922:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Test_Ackermann is
Line 916 ⟶ 951:
 
=={{header|Agda}}==
{{works with|Agda|2.56.23}}
{{libheader|agda-stdlib v0v1.137}}
<syntaxhighlight 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)))
open import Agda.Builtin.IO using (IO)
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>
 
NoteThe the unicode ℕUnicode characters, they can be inputentered in emacsEmacs agdaAgda modeas usingfollows: "\bN". Running in bash:
* ℕ : \bN
* → : \to
* ⊤ : \top
 
Running in Bash:
 
<syntaxhighlight lang="bash">
agda --compile Ackermann.agda
./Ackermann
Line 947 ⟶ 1,000:
=={{header|ALGOL 60}}==
{{works with|A60}}
<syntaxhighlight lang="algol60">begin
integer procedure ackermann(m,n);value m,n;integer m,n;
ackermann:=if m=0 then n+1
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}}
<syntaxhighlight lang="algol68">PROC test ackermann = VOID:
BEGIN
PROC ackermann = (INT m, n)INT:
Line 1,003 ⟶ 1,056:
=={{header|ALGOL W}}==
{{Trans|ALGOL 60}}
<syntaxhighlight lang="algolw">begin
integer procedure ackermann( integer value m,n ) ;
if m=0 then n+1
Line 1,023 ⟶ 1,076:
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang=APL"apl">ackermann←{
0=1⊃⍵:1+2⊃⍵
0=2⊃⍵:∇(¯1+1⊃⍵)1
Line 1,030 ⟶ 1,083:
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on ackermann(m, n)
 
<syntaxhighlight 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)
Line 1,039 ⟶ 1,091:
=={{header|Argile}}==
{{works with|Argile|1.0.0}}
<syntaxhighlight lang=Argile"argile">use std
 
for each (val nat n) from 0 to 6
Line 1,049 ⟶ 1,101:
return (A (m - 1) 1) if n == 0
A (m - 1) (A m (n - 1))</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI or android 32 bits */
/* program ackermann.s */
Line 1,217 ⟶ 1,270:
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">ackermann: function [m,n][
 
<syntaxhighlight lang=rebol>ackermann: function [m,n][
(m=0)? -> n+1 [
(n=0)? -> ackermann m-1 1
Line 1,230 ⟶ 1,282:
]
]</syntaxhighlight>
 
{{out}}
 
<pre>ackermann 0 0 => 1
ackermann 0 1 => 2
Line 1,255 ⟶ 1,305:
 
=={{header|ATS}}==
<syntaxhighlight lang=ATS"ats">fun ackermann
{m,n:nat} .<m,n>.
(m: int m, n: int n): Nat =
Line 1,265 ⟶ 1,315:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">A(m, n) {
If (m > 0) && (n = 0)
Return A(m-1,1)
Line 1,279 ⟶ 1,329:
=={{header|AutoIt}}==
=== Classical version ===
<syntaxhighlight lang="autoit">Func Ackermann($m, $n)
If ($m = 0) Then
Return $n+1
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.
 
<syntaxhighlight 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,315 ⟶ 1,365:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">function ackermann(m, n)
{
if ( m == 0 ) {
Line 1,335 ⟶ 1,385:
 
=={{header|Babel}}==
<syntaxhighlight lang="babel">main:
{((0 0) (0 1) (0 2)
(0 3) (0 4) (1 0)
Line 1,366 ⟶ 1,416:
if }
 
zero?!: { 0 = }</syntaxhighlight>
</syntaxhighlight>
{{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}}
<syntaxhighlight 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,417 ⟶ 1,466:
 
==={{header|BASIC256}}===
<syntaxhighlight 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,451 ⟶ 1,500:
return</syntaxhighlight>
{{out}}
<pre> A(3,7) = 1021</pre>
<pre>
A(3,7) = 1021
</pre>
 
<syntaxhighlight lang="basic256"># BASIC256 since 0.9.9.1 supports functions
for m = 0 to 3
for n = 0 to 4
Line 1,474 ⟶ 1,521:
end if
end function</syntaxhighlight>
 
{{out}}
<pre>0 0 1
0 0 1
0 1 2
0 2 3
Line 1,499 ⟶ 1,544:
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> PRINT FNackermann(3, 7)
END
Line 1,506 ⟶ 1,551:
IF N% = 0 THEN = FNackermann(M% - 1, 1)
= FNackermann(M% - 1, FNackermann(M%, N%-1))</syntaxhighlight>
 
==={{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.
<syntaxhighlight lang="qbasic">DECLARE FUNCTION ack! (m!, n!)
 
FUNCTION ack (m!, n!)
Line 1,526 ⟶ 1,608:
=={{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.
<syntaxhighlight lang="dos">::Ackermann.cmd
@echo off
set depth=0
Line 1,558 ⟶ 1,640:
if %depth%==0 ( exit %t% ) else ( exit /b %t% )</syntaxhighlight>
Because of the <code>exit</code> statements, running this bare closes one's shell, so this test routine handles the calling of Ackermann.cmd
<syntaxhighlight lang="dos">::Ack.cmd
@echo off
cmd/c ackermann.cmd %1 %2
Line 1,579 ⟶ 1,661:
{{works with|OpenBSD bc}}
{{Works with|GNU bc}}
<syntaxhighlight lang="bc">define ack(m, n) {
if ( m == 0 ) return (n+1);
if ( n == 0 ) return (ack(m-1, 1));
Line 1,593 ⟶ 1,675:
 
=={{header|BCPL}}==
<syntaxhighlight lang=BCPL"bcpl">GET "libhdr"
 
LET ack(m, n) = m=0 -> n+1,
Line 1,609 ⟶ 1,691:
Iterative slow version:
 
<syntaxhighlight 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,625 ⟶ 1,707:
Each block of <code>1FJ</code> or <code>1fFJ</code> in the code is a call of the Ackermann function itself.
 
<syntaxhighlight 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))
Line 1,631 ⟶ 1,713:
 
Highly optimized and fast version, returns A(4,1)/A(5,0) almost instantaneously:
<syntaxhighlight 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,651 ⟶ 1,733:
{{trans|ERRE}}
Since Befunge-93 doesn't have recursive capabilities we need to use an iterative algorithm.
<syntaxhighlight lang="befunge">&>&>vvg0>#0\#-:#1_1v
@v:\<vp0 0:-1<\+<
^>00p>:#^_$1+\:#^_$.
Line 1,658 ⟶ 1,740:
===Befunge-98===
{{works with|CCBI|2.1}}
<syntaxhighlight lang="befunge">r[1&&{0
>v
j
Line 1,668 ⟶ 1,750:
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}}==
<syntaxhighlight lang=BQN"bqn">A ← {
A 0‿n: n+1;
A m‿0: A (m-1)‿1;
Line 1,676 ⟶ 1,765:
}</syntaxhighlight>
Example usage:
<syntaxhighlight lang="text"> A 0‿3
4
A 1‿4
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)
<syntaxhighlight lang="bracmat">( Ack
= m n
. !arg:(?m,?n)
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".
<syntaxhighlight lang="bracmat"> ( A
= m n value key eq chain
, find insert future stack va val
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.
<syntaxhighlight lang="bracmat">( AckFormula
= m n
. !arg:(?m,?n)
Line 1,792 ⟶ 1,881:
 
=={{header|Brat}}==
<syntaxhighlight lang="brat">ackermann = { m, n |
when { m == 0 } { n + 1 }
{ m > 0 && n == 0 } { ackermann(m - 1, 1) }
Line 1,799 ⟶ 1,888:
 
p ackermann 3, 4 #Prints 125</syntaxhighlight>
 
=={{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:
<syntaxhighlight lang=C"c">#include <stdio.h>
 
int ackermann(int m, int n)
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:
<syntaxhighlight lang=C"c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,908 ⟶ 2,014:
 
A couple of alternative approaches...
<syntaxhighlight lang=C"c">/* Thejaka Maldeniya */
 
#include <conio.h>
Line 2,010 ⟶ 2,116:
 
A couple of more iterative techniques...
<syntaxhighlight lang=C"c">/* Thejaka Maldeniya */
 
#include <conio.h>
Line 2,163 ⟶ 2,269:
 
===Basic Version===
<syntaxhighlight lang="csharp">using System;
class Program
{
Line 2,220 ⟶ 2,326:
 
===Efficient Version===
<syntaxhighlight lang="csharp">
using System;
using System.Numerics;
Line 2,354 ⟶ 2,460:
 
===Basic version===
<syntaxhighlight lang="cpp">#include <iostream>
 
unsigned int ackermann(unsigned int m, unsigned int n) {
Line 2,379 ⟶ 2,485:
C++11 with boost's big integer type. Compile with:
g++ -std=c++11 -I /path/to/boost ackermann.cpp.
<syntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
#include <string>
Line 2,483 ⟶ 2,589:
 
=={{header|Chapel}}==
<syntaxhighlight lang="chapel">proc A(m:int, n:int):int {
if m == 0 then
return n + 1;
Line 2,493 ⟶ 2,599:
 
=={{header|Clay}}==
<syntaxhighlight lang=Clay"clay">ackermann(m, n) {
if(m == 0)
return n + 1;
Line 2,504 ⟶ 2,610:
=={{header|CLIPS}}==
'''Functional solution'''
<syntaxhighlight lang="clips">(deffunction ackerman
(?m ?n)
(if (= 0 ?m)
Line 2,525 ⟶ 2,631:
</pre>
'''Fact-based solution'''
<syntaxhighlight lang="clips">(deffacts solve-items
(solve 0 4)
(solve 1 4)
Line 2,620 ⟶ 2,726:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn ackermann [m n]
(cond (zero? m) (inc n)
(zero? n) (ackermann (dec m) 1)
Line 2,626 ⟶ 2,732:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Ackermann function
ack = proc (m, n: int) returns (int)
if m=0 then return(n+1)
Line 2,652 ⟶ 2,758:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Ackermann.
 
Line 2,686 ⟶ 2,792:
 
=={{header|CoffeeScript}}==
<syntaxhighlight 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
Line 2,692 ⟶ 2,798:
 
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 //
0020 // Ackermann function
0030 //
Line 2,716 ⟶ 2,822:
 
=={{header|Common Lisp}}==
<syntaxhighlight 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))))))</syntaxhighlight>
More elaborately:
<syntaxhighlight lang="lisp">(defun ackermann (m n)
(case m ((0) (1+ n))
((1) (+ 2 n))
Line 2,744 ⟶ 2,850:
=={{header|Component Pascal}}==
BlackBox Component Builder
<syntaxhighlight lang="oberon2">
MODULE NpctAckerman;
Line 2,785 ⟶ 2,891:
 
=={{header|Coq}}==
===Standard===
<syntaxhighlight 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.</syntaxhighlight>
end
end.
 
 
(*
Example:
A(3, 2) = 29
*)
 
Eval compute in ack 3 2.
</syntaxhighlight>
 
{{out}}
<pre>
= 29
: nat
</pre>
 
===Using fold===
<syntaxhighlight 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,813 ⟶ 2,938:
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">def ack(m, n)
if m == 0
n + 1
Line 2,839 ⟶ 2,964:
=={{header|D}}==
===Basic version===
<syntaxhighlight lang="d">ulong ackermann(in ulong m, in ulong n) pure nothrow @nogc {
if (m == 0)
return n + 1;
Line 2,853 ⟶ 2,978:
===More Efficient Version===
{{trans|Mathematica}}
<syntaxhighlight lang="d">import std.stdio, std.bigint, std.conv;
 
BigInt ipow(BigInt base, BigInt exp) pure nothrow {
Line 2,930 ⟶ 3,055:
=={{header|Dart}}==
no caching, the implementation takes ages even for A(4,1)
<syntaxhighlight 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,946 ⟶ 3,071:
=={{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.
<syntaxhighlight lang="dc">[ # todo: n 0 -- n+1 and break 2 levels
+ 1 + # n+1
q
Line 2,975 ⟶ 3,100:
 
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">function Ackermann(m,n:Int64):Int64;
begin
if m = 0 then
Line 2,986 ⟶ 3,111:
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Ackermann function */
proc ack(word m, n) word:
if m=0 then n+1
Line 3,011 ⟶ 3,136:
 
=={{header|DWScript}}==
<syntaxhighlight lang="delphi">function Ackermann(m, n : Integer) : Integer;
begin
if m = 0 then
Line 3,021 ⟶ 3,146:
 
=={{header|Dylan}}==
<syntaxhighlight lang="dylan">define method ack(m == 0, n :: <integer>)
n + 1
end;
Line 3,029 ⟶ 3,154:
 
=={{header|E}}==
<syntaxhighlight lang="e">def A(m, n) {
return if (m <=> 0) { n+1 } \
else if (m > 0 && n <=> 0) { A(m-1, 1) } \
Line 3,036 ⟶ 3,161:
 
=={{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
print r</syntaxhighlight>
 
=={{header|Egel}}==
<syntaxhighlight lang=Egel"egel">
def ackermann =
[ 0 N -> N + 1
Line 3,062 ⟶ 3,187:
[https://github.com/ljr1981/rosettacode_answers/blob/main/testing/rc_ackerman/rc_ackerman_test_set.e Test of Example code]
 
<syntaxhighlight lang=Eiffel"eiffel">
class
ACKERMAN_EXAMPLE
Line 3,089 ⟶ 3,214:
 
=={{header|Ela}}==
<syntaxhighlight lang="ela">ack 0 n = n+1
ack m 0 = ack (m - 1) 1
ack m n = ack (m - 1) <| ack m <| n - 1</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight 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()
}</syntaxhighlight>
{{out}}
Line 3,154 ⟶ 3,279:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Ackermann do
def ack(0, n), do: n + 1
def ack(m, 0), do: ack(m - 1, 1)
Line 3,173 ⟶ 3,298:
 
=={{header|Emacs Lisp}}==
<syntaxhighlight 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))))))</syntaxhighlight>
 
=={{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}}==
 
<syntaxhighlight lang="erlang">
-module(ackermann).
-export([ackermann/2]).
Line 3,197 ⟶ 3,369:
substituted with the new ERRE control statements.
 
<syntaxhighlight lang="erre">
PROGRAM ACKERMAN
 
Line 3,270 ⟶ 3,442:
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang=Euler"euler Mathmath Toolboxtoolbox">
>M=zeros(1000,1000);
>function map A(m,n) ...
Line 3,291 ⟶ 3,463:
=={{header|Euphoria}}==
This is based on the [[VBScript]] example.
<syntaxhighlight lang=Euphoria"euphoria">function ack(atom m, atom n)
if m = 0 then
return n + 1
Line 3,309 ⟶ 3,481:
 
=={{header|Ezhil}}==
<syntaxhighlight lang=Ezhil"ezhil">
நிரல்பாகம் அகெர்மன்(முதலெண், இரண்டாமெண்)
 
Line 3,352 ⟶ 3,524:
=={{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.
<syntaxhighlight lang="fsharp">let rec ackermann m n =
match m, n with
| 0, n -> n + 1
Line 3,361 ⟶ 3,533:
printfn "%A" (ackermann (int fsi.CommandLineArgs.[1]) (int fsi.CommandLineArgs.[2]))</syntaxhighlight>
Transforming this into continuation passing style avoids limited stack space by permitting tail-recursion.
<syntaxhighlight lang="fsharp">let ackermann M N =
let rec acker (m, n, k) =
match m,n with
Line 3,370 ⟶ 3,542:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: kernel math locals combinators ;
IN: ackermann
 
Line 3,381 ⟶ 3,553:
 
=={{header|Falcon}}==
<syntaxhighlight lang="falcon">function ackermann( m, n )
if m == 0: return( n + 1 )
if n == 0: return( ackermann( m - 1, 1 ) )
Line 3,404 ⟶ 3,576:
 
=={{header|FALSE}}==
<syntaxhighlight lang="false">[$$[%
\$$[%
1-\$@@a;! { i j -> A(i-1, A(i, j-1)) }
Line 3,418 ⟶ 3,590:
 
=={{header|Fantom}}==
<syntaxhighlight lang="fantom">class Main
{
// assuming m,n are positive
Line 3,476 ⟶ 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:
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
TestAckermann()
Line 3,497 ⟶ 3,669:
END FUNCTION
 
DYNC AckermannC(m AS INTEGER, n AS INTEGER) AS INTEGER</syntaxhighlight><syntaxhighlight lang=C"c">
int Ackermann(int m, int n)
{
Line 3,508 ⟶ 3,680:
{
return Ackermann(m, n);
}</syntaxhighlight><syntaxhighlight lang="qbasic">
END DYNC
 
DYNASM AckermannA(m AS INTEGER, n AS INTEGER) AS INTEGER</syntaxhighlight><syntaxhighlight lang="asm">
ENTER 0, 0
INVOKE Ackermann, m, n
Line 3,547 ⟶ 3,719:
LEAVE
RET 8
</syntaxhighlight><syntaxhighlight lang="qbasic">END DYNASM</syntaxhighlight>
 
{{out}}
Line 3,560 ⟶ 3,732:
 
=={{header|Fermat}}==
<syntaxhighlight 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)</syntaxhighlight>
{{out}}
Line 3,568 ⟶ 3,740:
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: acker ( m n -- u )
over 0= IF nip 1+ EXIT THEN
swap 1- swap ( m-1 n -- )
Line 3,577 ⟶ 3,749:
FORTH> 3 4 acker . 125 ok</pre>
An optimized version:
<syntaxhighlight lang="forth">: ackermann ( m n -- u )
over ( case statement)
0 over = if drop nip 1+ else
Line 3,594 ⟶ 3,766:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<syntaxhighlight lang="fortran">PROGRAM EXAMPLE
IMPLICIT NONE
Line 3,626 ⟶ 3,798:
 
=={{header|FreeBASIC}}==
<syntaxhighlight 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,675 ⟶ 3,847:
 
=={{header|FunL}}==
<syntaxhighlight lang="funl">def
ackermann( 0, n ) = n + 1
ackermann( m, 0 ) = ackermann( m - 1, 1 )
Line 3,710 ⟶ 3,882:
=={{header|Futhark}}==
 
<syntaxhighlight lang=Futhark"futhark">
fun ackermann(m: int, n: int): int =
if m == 0 then n + 1
Line 3,718 ⟶ 3,890:
 
=={{header|FutureBasic}}==
<syntaxhighlight 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,793 ⟶ 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'''
 
[[File:Fōrmulæ - Ackermann function 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Ackermann function 02.png]]
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 03.png]]
In '''[https://formulae.org/?example=Ackermann_function this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
<syntaxhighlight lang="gambas">Public Function Ackermann(m As Float, n As Float) As Float
If m = 0 Then
Return n + 1
Line 3,820 ⟶ 3,998:
 
=={{header|GAP}}==
<syntaxhighlight lang="gap">ack := function(m, n)
if m = 0 then
return n + 1;
Line 3,833 ⟶ 4,011:
 
=={{header|Genyris}}==
<syntaxhighlight lang="genyris">def A (m n)
cond
(equal? m 0)
Line 3,845 ⟶ 4,023:
=={{header|GML}}==
Define a script resource named ackermann and paste this code inside:
<syntaxhighlight lang=GML"gml">///ackermann(m,n)
var m, n;
m = argument0;
Line 3,863 ⟶ 4,041:
 
=={{header|gnuplot}}==
<syntaxhighlight 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)
Line 3,876 ⟶ 4,054:
=={{header|Go}}==
===Classic version===
<syntaxhighlight lang="go">func Ackermann(m, n uint) uint {
switch 0 {
case m:
Line 3,886 ⟶ 4,064:
}</syntaxhighlight>
===Expanded version===
<syntaxhighlight lang="go">func Ackermann2(m, n uint) uint {
switch {
case m == 0:
Line 3,902 ⟶ 4,080:
}</syntaxhighlight>
===Expanded version with arbitrary precision===
<syntaxhighlight lang="go">package main
 
import (
Line 4,005 ⟶ 4,183:
 
=={{header|Golfscript}}==
<syntaxhighlight lang="golfscript">{
:_n; :_m;
_m 0= {_n 1+}
Line 4,015 ⟶ 4,193:
 
=={{header|Groovy}}==
<syntaxhighlight 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))
}</syntaxhighlight>
Test program:
<syntaxhighlight lang="groovy">def ackMatrix = (0..3).collect { m -> (0..8).collect { n -> ack(m, n) } }
ackMatrix.each { it.each { elt -> printf "%7d", elt }; println() }</syntaxhighlight>
{{out}}
Line 4,030 ⟶ 4,208:
 
=={{header|Hare}}==
<syntaxhighlight lang="hare">use fmt;
 
fn ackermann(m: u64, n: u64) u64 = {
Line 4,052 ⟶ 4,230:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">ack :: Int -> Int -> Int
ack 0 n = succ n
ack m 0 = ack (pred m) 1
Line 4,064 ⟶ 4,242:
 
Generating a list instead:
<syntaxhighlight lang="haskell">import Data.List (mapAccumL)
 
-- everything here are [Int] or [[Int]], which would overflow
Line 4,076 ⟶ 4,254:
 
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">class RosettaDemo
{
static public function main()
Line 4,098 ⟶ 4,276:
 
=={{header|Hoon}}==
<syntaxhighlight lang=Hoon"hoon">
|= [m=@ud n=@ud]
?: =(m 0)
Line 4,111 ⟶ 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.
<syntaxhighlight lang=Icon"icon">procedure acker(i, j)
static memory
 
Line 4,144 ⟶ 4,322:
 
=={{header|Idris}}==
<syntaxhighlight lang="idris">A : Nat -> Nat -> Nat
A Z n = S n
A (S m) Z = A m (S Z)
Line 4,151 ⟶ 4,329:
=={{header|Ioke}}==
{{trans|Clojure}}
<syntaxhighlight lang="ioke">ackermann = method(m,n,
cond(
m zero?, n succ,
Line 4,160 ⟶ 4,338:
=={{header|J}}==
As posted at the [[j:Essays/Ackermann%27s%20Function|J wiki]]
<syntaxhighlight 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</syntaxhighlight>
{{out|Example use}}
<syntaxhighlight lang="j"> 0 ack 3
4
1 ack 3
Line 4,178 ⟶ 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.
<syntaxhighlight lang="j"> Ack=. 3 -~ [ ({&(2 4$'>: 2x&+') ::(,&'&1'&'2x&*'@:(-&2))"0@:[ 128!:2 ]) 3 + ]</syntaxhighlight>
{{out|Example use}}
<syntaxhighlight 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 4,199 ⟶ 4,377:
A structured derivation of Ack follows:
 
<syntaxhighlight lang="j"> o=. @: NB. Composition of verbs (functions)
x=. o[ NB. Composing the left noun (argument)
Line 4,229 ⟶ 4,407:
=={{header|Java}}==
[[Category:Arbitrary precision]]
<syntaxhighlight lang="java">import java.math.BigInteger;
 
public static BigInteger ack(BigInteger m, BigInteger n) {
Line 4,239 ⟶ 4,417:
 
{{works with|Java|8+}}
<syntaxhighlight lang="java5">@FunctionalInterface
public interface FunctionalField<FIELD extends Enum<?>> {
public Object untypedField(FIELD field);
Line 4,248 ⟶ 4,426:
}
}</syntaxhighlight>
<syntaxhighlight lang="java5">import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
Line 4,294 ⟶ 4,472:
}
}</syntaxhighlight>
<syntaxhighlight lang="java5">import java.math.BigInteger;
import java.util.Stack;
import java.util.function.BinaryOperator;
Line 4,440 ⟶ 4,618:
}</syntaxhighlight>
{{Iterative version}}
<syntaxhighlight lang="java5">/*
* Source https://stackoverflow.com/a/51092690/5520417
*/
Line 4,834 ⟶ 5,012:
=={{header|JavaScript}}==
===ES5===
<syntaxhighlight lang="javascript">function ack(m, n) {
return m === 0 ? n + 1 : ack(m - 1, n === 0 ? 1 : ack(m, n - 1));
}</syntaxhighlight>
===Eliminating Tail Calls===
<syntaxhighlight lang="javascript">function ack(M,N) {
for (; M > 0; M--) {
N = N === 0 ? 1 : ack(M,N-1);
Line 4,845 ⟶ 5,023:
}</syntaxhighlight>
===Iterative, With Explicit Stack===
<syntaxhighlight lang="javascript">function stackermann(M, N) {
const stack = [];
for (;;) {
Line 4,866 ⟶ 5,044:
}</syntaxhighlight>
===Stackless Iterative===
<syntaxhighlight lang="javascript">#!/usr/bin/env nodejs
function ack(M, N){
const next = new Float64Array(M + 1);
Line 4,901 ⟶ 5,079:
 
===ES6===
<syntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 4,943 ⟶ 5,121:
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE ack == [[[pop null] popd succ]
From [http://www.latrobe.edu.au/phimvt/joy/jp-nestrec.html here]
[[null] pop pred 1 ack]
<syntaxhighlight lang=joy>DEFINE ack == [ [ [pop null] popd succ ]
[[dup pred [ [nullswap] popdip pred 1ack ack ]]
cond.</syntaxhighlight>
[ [dup pred swap] dip pred ack ack ] ]
another using a combinator:
cond.</syntaxhighlight>
<syntaxhighlight lang="joy">DEFINE ack == [[[pop null] [popd succ]]
another using a combinator
[[null] [pop pred 1] []]
<syntaxhighlight lang=joy>DEFINE ack == [ [ [pop null] [popd succ] ]
[ [null] [popdup pred 1swap] dip pred] [] []]]
condnestrec.</syntaxhighlight>
[ [[dup pred swap] dip pred] [] [] ] ]
condnestrec.</syntaxhighlight>
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===
<syntaxhighlight lang="jq"># input: [m,n]
def ack:
.[0] as $m | .[1] as $n
Line 4,966 ⟶ 5,147:
end ;</syntaxhighlight>
'''Example:'''
<syntaxhighlight 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 )"</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh"># jq -n -r -f ackermann.jq
A(0,0) = 1
A(0,1) = 2
Line 4,997 ⟶ 5,178:
A(4,0) = 13</syntaxhighlight>
===With Memoization and Optimization===
<syntaxhighlight lang="jq"># input: [m,n, cache]
# output [value, updatedCache]
def ack:
Line 5,027 ⟶ 5,208:
def A(m;n): [m,n,{}] | ack | .[0];
</syntaxhighlight>
'''Examples:'''
'''Example:'''<syntaxhighlight lang=jq>A(4,1)</syntaxhighlight>
<syntaxhighlight lang="jq">A(4;1)</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">65533</syntaxhighlight>
 
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.
<syntaxhighlight lang="javascript">/* Ackermann function, in Jsish */
 
function ack(m, n) {
Line 5,070 ⟶ 5,260:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">function ack(m,n)
if m == 0
return n + 1
Line 5,081 ⟶ 5,271:
 
'''One-liner:'''
<syntaxhighlight lang="julia">ack2(m::Integer, n::Integer) = m == 0 ? n + 1 : n == 0 ? ack2(m - 1, 1) : ack2(m - 1, ack2(m, n - 1))</syntaxhighlight>
 
'''Using memoization''', [https://github.com/simonster/Memoize.jl source]:
<syntaxhighlight 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))</syntaxhighlight>
 
Line 5,097 ⟶ 5,287:
 
=={{header|K}}==
{{works with|Kona}}
See [https://github.com/kevinlawler/kona/wiki the K wiki]
<syntaxhighlight lang="k"> ack:{:[0=x;y+1;0=y;_f[x-1;1];_f[x-1;_f[x;y-1]]]}
ack[2;2]</syntaxhighlight>
7
ack[2;7]
17</syntaxhighlight>
 
=={{header|Kdf9 Usercode}}==
<syntaxhighlight lang="kdf9 usercode">V6; W0;
YS26000;
RESTART; J999; J999;
Line 5,154 ⟶ 5,347:
 
=={{header|Klingphix}}==
<syntaxhighlight lang="text">:ack
%n !n %m !m
Line 5,173 ⟶ 5,366:
 
=={{header|Klong}}==
<syntaxhighlight lang="k">
ack::{:[0=x;y+1:|0=y;.f(x-1;1);.f(x-1;.f(x;y-1))]}
ack(2;2)</syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">
tailrec fun A(m: Long, n: Long): Long {
require(m >= 0L) { "m must not be negative" }
Line 5,215 ⟶ 5,408:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang=Scheme"scheme">
{def ack
{lambda {:m :n}
Line 5,241 ⟶ 5,434:
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9
define ackermann(m::integer, n::integer) => {
Line 5,275 ⟶ 5,468:
 
=={{header|LFE}}==
<syntaxhighlight lang="lisp">(defun ackermann
((0 n) (+ n 1))
((m 0) (ackermann (- m 1) 1))
Line 5,281 ⟶ 5,474:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">Print Ackermann(1, 2)
 
Function Ackermann(m, n)
Line 5,297 ⟶ 5,490:
 
=={{header|LiveCode}}==
<syntaxhighlight lang=LiveCode"livecode">function ackermann m,n
switch
Case m = 0
Line 5,309 ⟶ 5,502:
 
=={{header|Logo}}==
<syntaxhighlight lang="logo">to ack :i :j
if :i = 0 [output :j+1]
if :j = 0 [output ack :i-1 1]
Line 5,316 ⟶ 5,509:
 
=={{header|Logtalk}}==
<syntaxhighlight lang="logtalk">ack(0, N, V) :-
!,
V is N + 1.
Line 5,331 ⟶ 5,524:
=={{header|LOLCODE}}==
{{trans|C}}
<syntaxhighlight lang=LOLCODE"lolcode">HAI 1.3
 
HOW IZ I ackermann YR m AN YR n
Line 5,355 ⟶ 5,548:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function ack(M,N)
if M == 0 then return N + 1 end
if N == 0 then return ack(M-1,1) end
Line 5,362 ⟶ 5,555:
 
=== Stackless iterative solution with multiple precision, fast ===
<syntaxhighlight lang="lua">
#!/usr/bin/env luajit
local gmp = require 'gmp' ('libgmp')
Line 5,417 ⟶ 5,610:
 
=={{header|Lucid}}==
<syntaxhighlight lang="lucid">ack(m,n)
where
ack(m,n) = if m eq 0 then n+1
Line 5,426 ⟶ 5,619:
 
=={{header|Luck}}==
<syntaxhighlight lang="luck">function ackermann(m: int, n: int): int = (
if m==0 then n+1
else if n==0 then ackermann(m-1,1)
Line 5,433 ⟶ 5,626:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
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,451 ⟶ 5,644:
 
=={{header|M4}}==
<syntaxhighlight lang=M4"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)</syntaxhighlight>
{{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,469 ⟶ 5,737:
the arguments to the recursive function via the stack or the global variables. The following program demonstrates this.
 
<syntaxhighlight lang=MAD"mad"> NORMAL MODE IS INTEGER
DIMENSION LIST(3000)
SET LIST TO LIST
Line 5,547 ⟶ 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"maple">
Ackermann := proc( m :: nonnegint, n :: nonnegint )
option remember; # optional automatic memoization
Line 5,565 ⟶ 5,830:
end proc:
</syntaxhighlight>
In Maple, the keyword <syntaxhighlight lang=Maple"maple">thisproc</syntaxhighlight> 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"maple">
Ackermann := proc( m :: nonnegint, n :: nonnegint )
option remember; # optional automatic memoization
Line 5,589 ⟶ 5,854:
 
To compute Ackermann( 1, i ) for i from 1 to 10 use
<syntaxhighlight lang=Maple"maple">
> map2( Ackermann, 1, [seq]( 1 .. 10 ) );
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
</syntaxhighlight>
To get the first 10 values for m = 2 use
<syntaxhighlight lang=Maple"maple">
> map2( Ackermann, 2, [seq]( 1 .. 10 ) );
[5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
</syntaxhighlight>
For Ackermann( 4, 2 ) we get a very long number with
<syntaxhighlight lang=Maple"maple">
> length( Ackermann( 4, 2 ) );
19729
Line 5,610 ⟶ 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.
 
<syntaxhighlight lang=Mathcad"mathcad">A(m,n):=if(m=0,n+1,if(n=0,A(m-1,1),A(m-1,A(m,n-1))))</syntaxhighlight>
 
The worksheet also contains an explictly-calculated version of Ackermann's function that calls the tetration function n<sub>a</sub>.
Line 5,625 ⟶ 5,890:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Two possible implementations would be:
<syntaxhighlight lang=Mathematica"mathematica">$RecursionLimit=Infinity
Ackermann1[m_,n_]:=
If[m==0,n+1,
Line 5,638 ⟶ 5,903:
Note that the second implementation is quite a bit faster, as doing 'if' comparisons is slower than the built-in pattern matching algorithms.
Examples:
<syntaxhighlight lang=Mathematica"mathematica">Flatten[#,1]&@Table[{"Ackermann2["<>ToString[i]<>","<>ToString[j]<>"] =",Ackermann2[i,j]},{i,3},{j,8}]//Grid</syntaxhighlight>
gives back:
<syntaxhighlight lang=Mathematica"mathematica">Ackermann2[1,1] = 3
Ackermann2[1,2] = 4
Ackermann2[1,3] = 5
Line 5,665 ⟶ 5,930:
Ackermann2[3,8] = 2045</syntaxhighlight>
If we would like to calculate Ackermann[4,1] or Ackermann[4,2] we have to optimize a little bit:
<syntaxhighlight lang=Mathematica"mathematica">Clear[Ackermann3]
$RecursionLimit=Infinity;
Ackermann3[0,n_]:=n+1;
Line 5,675 ⟶ 5,940:
Now computing Ackermann[4,1] and Ackermann[4,2] can be done quickly (<0.01 sec):
Examples 2:
<syntaxhighlight lang=Mathematica"mathematica">Ackermann3[4, 1]
Ackermann3[4, 2]</syntaxhighlight>
gives back:
<div style="width:full;overflow:scroll"><syntaxhighlight lang=Mathematica"mathematica">65533
2003529930406846464979072351560255750447825475569751419265016973710894059556311453089506130880........699146577530041384717124577965048175856395072895337539755822087777506072339445587895905719156733</syntaxhighlight></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.
Line 5,684 ⟶ 5,949:
 
=={{header|MATLAB}}==
<syntaxhighlight lang=MATLAB"matlab">function A = ackermannFunction(m,n)
if m == 0
A = n+1;
Line 5,695 ⟶ 5,960:
 
=={{header|Maxima}}==
<syntaxhighlight 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,713 ⟶ 5,978:
=={{header|MAXScript}}==
Use with caution. Will cause a stack overflow for m > 3.
<syntaxhighlight lang="maxscript">fn ackermann m n =
(
if m == 0 then
Line 5,731 ⟶ 5,996:
=={{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.
<syntaxhighlight lang="mercury">:- func ack(integer, integer) = integer.
ack(M, N) = R :- ack(M, N, R).
 
Line 5,744 ⟶ 6,009:
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang="min">(
:n :m
(
Line 5,755 ⟶ 6,020:
=={{header|MiniScript}}==
 
<syntaxhighlight lang=MiniScript"miniscript">ackermann = function(m, n)
if m == 0 then return n+1
if n == 0 then return ackermann(m - 1, 1)
Line 5,768 ⟶ 6,033:
 
=={{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
Line 5,776 ⟶ 6,041:
ML/I loves recursion, but runs out of its default amount of storage with larger numbers than those tested here!
===Program===
<syntaxhighlight lang=ML"ml/Ii">MCSKIP "WITH" NL
"" Ackermann function
"" Will overflow when it reaches implementation-defined signed integer limit
Line 5,810 ⟶ 6,075:
a(4,0) => ACK(4,0)</syntaxhighlight>
{{out}}
<syntaxhighlight lang=ML"ml/Ii">a(0,0) => 1
a(0,1) => 2
a(0,2) => 3
Line 5,831 ⟶ 6,096:
 
=={{header|mLite}}==
<syntaxhighlight lang="haskell">fun ackermann( 0, n ) = n + 1
| ( m, 0 ) = ackermann( m - 1, 1 )
| ( m, n ) = ackermann( m - 1, ackermann(m, n - 1) )</syntaxhighlight>
Test code providing tuples from (0,0) to (3,8)
<syntaxhighlight 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
Line 5,844 ⟶ 6,109:
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE ackerman;
 
IMPORT ASCII, NumConv, InOut;
Line 5,885 ⟶ 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.
<syntaxhighlight lang="modula3">MODULE Ack EXPORTS Main;
 
FROM IO IMPORT Put;
Line 5,916 ⟶ 6,181:
 
=={{header|MUMPS}}==
<syntaxhighlight lang=MUMPS"mumps">Ackermann(m,n) ;
If m=0 Quit n+1
If m>0,n=0 Quit $$Ackermann(m-1,1)
Line 5,927 ⟶ 6,192:
 
=={{header|Neko}}==
<syntaxhighlight lang=ActionScript"actionscript">/**
Ackermann recursion, in Neko
Tectonics:
Line 5,972 ⟶ 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"nemerle">
using System;
using Nemerle.IO;
Line 5,995 ⟶ 6,260:
</syntaxhighlight>
A terser version using implicit <code>match</code> (which doesn't use the alias <code>A</code> internally):
<syntaxhighlight lang=Nemerle"nemerle">
def ackermann(m, n) {
| (0, n) => n + 1
Line 6,004 ⟶ 6,269:
</syntaxhighlight>
Or, if we were set on using the <code>A</code> notation, we could do this:
<syntaxhighlight lang=Nemerle"nemerle">
def ackermann = {
def A(m, n) {
Line 6,017 ⟶ 6,282:
 
=={{header|NetRexx}}==
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 6,044 ⟶ 6,309:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
#! /usr/local/bin/newlisp
 
Line 6,060 ⟶ 6,325:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">from strutils import parseInt
 
proc ackermann(m, n: int64): int64 =
Line 6,091 ⟶ 6,356:
Source: [https://github.com/nitlang/nit/blob/master/examples/rosettacode/ackermann_function.nit the official Nit’s repository].
 
<syntaxhighlight lang="nit"># Task: Ackermann function
#
# A simple straightforward recursive implementation.
Line 6,147 ⟶ 6,412:
 
=={{header|Oberon-2}}==
<syntaxhighlight lang="oberon2">MODULE ackerman;
 
IMPORT Out;
Line 6,176 ⟶ 6,441:
=={{header|Objeck}}==
{{trans|C#|C sharp}}
<syntaxhighlight lang="objeck">class Ackermann {
function : Main(args : String[]) ~ Nil {
for(m := 0; m <= 3; ++m;) {
Line 6,230 ⟶ 6,495:
 
=={{header|OCaml}}==
<syntaxhighlight 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)))</syntaxhighlight>
or:
<syntaxhighlight 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))</syntaxhighlight>
with memoization using an hash-table:
<syntaxhighlight lang="ocaml">let h = Hashtbl.create 4001
 
let a m n =
Line 6,249 ⟶ 6,514:
(res)</syntaxhighlight>
taking advantage of the memoization we start calling small values of '''m''' and '''n''' in order to reduce the recursion call stack:
<syntaxhighlight lang="ocaml">let a m n =
for _m = 0 to m do
for _n = 0 to n do
Line 6,258 ⟶ 6,523:
=== Arbitrary precision ===
With arbitrary-precision integers ([http://caml.inria.fr/pub/docs/manual-ocaml/libref/Big_int.html Big_int module]):
<syntaxhighlight lang="ocaml">open Big_int
let one = unit_big_int
let zero = zero_big_int
Line 6,273 ⟶ 6,538:
=== Tail-Recursive ===
Here is a [[:Category:Recursion|tail-recursive]] version:
<syntaxhighlight lang="ocaml">let rec find_option h v =
try Some(Hashtbl.find h v)
with Not_found -> None
Line 6,304 ⟶ 6,569:
let a = a (Hashtbl.create 42 (* arbitrary *) ) [] [] ;;</syntaxhighlight>
This one uses the arbitrary precision, the tail-recursion, and the optimisation explain on the Wikipedia page about <tt>(m,n) = (3,_)</tt>.
<syntaxhighlight lang="ocaml">open Big_int
let one = unit_big_int
let zero = zero_big_int
Line 6,385 ⟶ 6,650:
 
=={{header|Octave}}==
<syntaxhighlight lang="octave">function r = ackerman(m, n)
if ( m == 0 )
r = n + 1;
Line 6,400 ⟶ 6,665:
 
=={{header|Oforth}}==
<syntaxhighlight lang=Oforth"oforth">: A( m n -- p )
m ifZero: [ n 1+ return ]
m 1- n ifZero: [ 1 ] else: [ A( m, n 1- ) ] A
Line 6,406 ⟶ 6,671:
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
; simple version
(define (A m n)
Line 6,452 ⟶ 6,717:
 
=={{header|OOC}}==
<syntaxhighlight lang="ooc">
ack: func (m: Int, n: Int) -> Int {
if (m == 0) {
Line 6,473 ⟶ 6,738:
 
=={{header|ooRexx}}==
<syntaxhighlight lang=ooRexx"oorexx">
loop m = 0 to 3
loop n = 0 to 6
Line 6,521 ⟶ 6,786:
 
=={{header|Order}}==
<syntaxhighlight lang="c">#include <order/interpreter.h>
 
#define ORDER_PP_DEF_8ack ORDER_PP_FN( \
Line 6,533 ⟶ 6,798:
=={{header|Oz}}==
Oz has arbitrary precision integers.
<syntaxhighlight lang="oz">declare
 
fun {Ack M N}
Line 6,548 ⟶ 6,813:
=={{header|PARI/GP}}==
Naive implementation.
<syntaxhighlight lang="parigp">A(m,n)={
if(m,
if(n,
Line 6,561 ⟶ 6,826:
 
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">Program Ackerman;
 
function ackermann(m, n: Integer) : Integer;
Line 6,584 ⟶ 6,849:
=={{header|Perl}}==
We memoize calls to ''A'' to make ''A''(2, ''n'') and ''A''(3, ''n'') feasible for larger values of ''n''.
<syntaxhighlight lang="perl">{
my @memo;
sub A {
Line 6,599 ⟶ 6,864:
 
An implementation using the conditional statements 'if', 'elsif' and 'else':
<syntaxhighlight lang="perl">sub A {
my ($m, $n) = @_;
if ($m == 0) { $n + 1 }
Line 6,607 ⟶ 6,872:
 
An implementation using ternary chaining:
<syntaxhighlight lang="perl">sub A {
my ($m, $n) = @_;
$m == 0 ? $n + 1 :
Line 6,615 ⟶ 6,880:
 
Adding memoization and extra terms:
<syntaxhighlight lang="perl">use Memoize; memoize('ack2');
use bigint try=>"GMP";
 
Line 6,637 ⟶ 6,902:
An optimized version, which uses <code>@_</code> as a stack,
instead of recursion. Very fast.
<syntaxhighlight lang=Perl"perl">use strict;
use warnings;
use Math::BigInt;
Line 6,676 ⟶ 6,941:
=={{header|Phix}}==
=== native version ===
<!--<syntaxhighlight lang=Phix"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,727 ⟶ 6,992:
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang=Phix"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,830 ⟶ 7,095:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">def ack
var n var m
Line 6,847 ⟶ 7,112:
 
=={{header|PHP}}==
<syntaxhighlight lang="php">function ackermann( $m , $n )
{
if ( $m==0 )
Line 6,864 ⟶ 7,129:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">go =>
foreach(M in 0..3)
println([m=M,[a(M,N) : N in 0..16]])
Line 6,923 ⟶ 7,188:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de ack (X Y)
(cond
((=0 X) (inc Y))
Line 7,296 ⟶ 7,561:
 
=={{header|Pike}}==
<syntaxhighlight lang="pike">int main(){
write(ackermann(3,4) + "\n");
}
Line 7,311 ⟶ 7,576:
 
=={{header|PL/I}}==
<syntaxhighlight lang=PL"pl/Ii">Ackerman: procedure (m, n) returns (fixed (30)) recursive;
declare (m, n) fixed (30);
if m = 0 then return (n+1);
Line 7,320 ⟶ 7,585:
 
=={{header|PL/SQL}}==
<syntaxhighlight lang=PLSQL"plsql">DECLARE
 
FUNCTION ackermann(pi_m IN NUMBER,
Line 7,375 ⟶ 7,640:
 
=={{header|PostScript}}==
<syntaxhighlight lang="postscript">/ackermann{
/n exch def
/m exch def %PostScript takes arguments in the reverse order as specified in the function definition
Line 7,390 ⟶ 7,655:
}def</syntaxhighlight>
{{libheader|initlib}}
<syntaxhighlight lang="postscript">/A {
[/.m /.n] let
{
Line 7,400 ⟶ 7,665:
 
=={{header|Potion}}==
<syntaxhighlight lang=Potion"potion">ack = (m, n):
if (m == 0): n + 1
. elsif (n == 0): ack(m - 1, 1)
Line 7,413 ⟶ 7,678:
 
=={{header|PowerBASIC}}==
<syntaxhighlight lang="powerbasic">FUNCTION PBMAIN () AS LONG
DIM m AS QUAD, n AS QUAD
 
Line 7,434 ⟶ 7,699:
=={{header|PowerShell}}==
{{trans|PHP}}
<syntaxhighlight lang="powershell">function ackermann ([long] $m, [long] $n) {
if ($m -eq 0) {
return $n + 1
Line 7,446 ⟶ 7,711:
}</syntaxhighlight>
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):
<syntaxhighlight lang="powershell">foreach ($m in 0..3) {
foreach ($n in 0..6) {
Write-Host -NoNewline ("{0,5}" -f (ackermann $m $n))
Line 7,459 ⟶ 7,724:
 
===A More "PowerShelly" Way===
<syntaxhighlight lang=PowerShell"powershell">
function Get-Ackermann ([int64]$m, [int64]$n)
{
Line 7,476 ⟶ 7,741:
</syntaxhighlight>
Save the result to an array (for possible future use?), then display it using the <code>Format-Wide</code> cmdlet:
<syntaxhighlight lang=PowerShell"powershell">
$ackermann = 0..3 | ForEach-Object {$m = $_; 0..6 | ForEach-Object {Get-Ackermann $m $_}}
 
Line 7,490 ⟶ 7,755:
 
=={{header|Processing}}==
<syntaxhighlight lang="java">int ackermann(int m, int n) {
if (m == 0)
return n + 1;
Line 7,522 ⟶ 7,787:
m = 5 it throws 'maximum recursion depth exceeded'
 
<syntaxhighlight lang="python">from __future__ import print_function
 
def setup():
Line 7,543 ⟶ 7,808:
Processing.R may exceed its stack depth at ~n==6 and returns null.
 
<syntaxhighlight lang="r">setup <- function() {
for (m in 0:3) {
for (n in 0:4) {
Line 7,570 ⟶ 7,835:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight 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).</syntaxhighlight>
 
"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}}==
<syntaxhighlight 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;</syntaxhighlight>
 
=={{header|Pure Data}}==
<syntaxhighlight lang=Pure"pure Datadata">
#N canvas 741 265 450 436 10;
#X obj 83 111 t b l;
Line 7,630 ⟶ 7,914:
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">Procedure.q Ackermann(m, n)
If m = 0
ProcedureReturn n + 1
Line 7,643 ⟶ 7,927:
 
=={{header|Purity}}==
<syntaxhighlight lang=Purity"purity">data Iter = f => FoldNat <const $f One, $f>
data Ackermann = FoldNat <const Succ, Iter></syntaxhighlight>
 
Line 7,649 ⟶ 7,933:
===Python: Explicitly recursive===
{{works with|Python|2.5}}
<syntaxhighlight 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)))</syntaxhighlight>
Another version:
<syntaxhighlight lang="python">from functools import lru_cache
 
@lru_cache(None)
Line 7,664 ⟶ 7,948:
return ack2(M - 1, ack2(M, N - 1))</syntaxhighlight>
{{out|Example of use}}
<syntaxhighlight lang="python">>>> import sys
>>> sys.setrecursionlimit(3000)
>>> ack1(0,0)
Line 7,675 ⟶ 7,959:
125</syntaxhighlight>
From the Mathematica ack3 example:
<syntaxhighlight lang="python">def ack2(M, N):
return (N + 1) if M == 0 else (
(N + 2) if M == 1 else (
Line 7,686 ⟶ 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.
 
<syntaxhighlight lang="python">from collections import deque
 
def ack_ix(m, n):
Line 7,735 ⟶ 8,019:
 
=={{header|Quackery}}==
<syntaxhighlight lang=Quackery"quackery"> forward is ackermann ( m n --> r )
[ over 0 = iff
[ nip 1 + ] done
Line 7,749 ⟶ 8,033:
 
=={{header|R}}==
<syntaxhighlight lang=R"r">ackermann <- function(m, n) {
if ( m == 0 ) {
n+1
Line 7,758 ⟶ 8,042:
}
}</syntaxhighlight>
<syntaxhighlight lang=R"r">for ( i in 0:3 ) {
print(ackermann(i, 4))
}</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(define (ackermann m n)
Line 7,775 ⟶ 8,059:
{{works with|Rakudo|2018.03}}
 
<syntaxhighlight lang=perl6"raku" line>sub A(Int $m, Int $n) {
if $m == 0 { $n + 1 }
elsif $n == 0 { A($m - 1, 1) }
Line 7,781 ⟶ 8,065:
}</syntaxhighlight>
An implementation using multiple dispatch:
<syntaxhighlight lang=perl6"raku" line>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)) }</syntaxhighlight>
Line 7,787 ⟶ 8,071:
 
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=perl6"raku" line>proto A(Int \𝑚, Int \𝑛) { (state @)[𝑚][𝑛] //= {*} }
 
multi A(0, Int \𝑛) { 𝑛 + 1 }
Line 7,812 ⟶ 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}}==
<syntaxhighlight lang=ReScript"rescript">let _m = Sys.argv[2]
let _n = Sys.argv[3]
 
Line 7,841 ⟶ 8,138:
=={{header|REXX}}==
===no optimization===
<syntaxhighlight lang="rexx">/*REXX program calculates and displays some values for the Ackermann function. */
/*╔════════════════════════════════════════════════════════════════════════╗
║ Note: the Ackermann function (as implemented here) utilizes deep ║
Line 7,946 ⟶ 8,243:
 
===optimized for m ≤ 2===
<syntaxhighlight lang="rexx">/*REXX program calculates and displays some values for the Ackermann function. */
high=24
do j=0 to 3; say
Line 8,050 ⟶ 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.
<syntaxhighlight lang="rexx">/*REXX program calculates and displays some values for the Ackermann function. */
numeric digits 100 /*use up to 100 decimal digit integers.*/
/*╔═════════════════════════════════════════════════════════════╗
Line 8,173 ⟶ 8,470:
=={{header|Ring}}==
{{trans|C#}}
<syntaxhighlight lang="ring">for m = 0 to 3
for n = 0 to 4
see "Ackermann(" + m + ", " + n + ") = " + Ackermann(m, n) + nl
Line 8,214 ⟶ 8,511:
Ackermann(3, 4) = 125</pre>
 
=={{header|RiscRISC-V Assembly}}==
the basic recursive function, because memorization and other improvements would blow the clarity.
<syntaxhighlight lang=Risc"risc-Vv">ackermann: #x: a1, y: a2, return: a0
beqz a1, npe #case m = 0
beqz a2, mme #case m > 0 & n = 0
Line 8,244 ⟶ 8,541:
jr t0, 0
</syntaxhighlight>
 
=={{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}}
<syntaxhighlight lang="ruby">def ack(m, n)
if m == 0
n + 1
Line 8,257 ⟶ 8,570:
end</syntaxhighlight>
Example:
<syntaxhighlight lang="ruby">(0..3).each do |m|
puts (0..6).map { |n| ack(m, n) }.join(' ')
end</syntaxhighlight>
Line 8,267 ⟶ 8,580:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">print ackermann(1, 2)
function ackermann(m, n)
Line 8,276 ⟶ 8,589:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn ack(m: isize, n: isize) -> isize {
if m == 0 {
n + 1
Line 8,294 ⟶ 8,607:
Or:
 
<syntaxhighlight lang="rust">
fn ack(m: u64, n: u64) -> u64 {
match (m, n) {
Line 8,305 ⟶ 8,618:
 
=={{header|Sather}}==
<syntaxhighlight lang="sather">class MAIN is
 
ackermann(m, n:INT):INT
Line 8,325 ⟶ 8,638:
end;</syntaxhighlight>
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.
<syntaxhighlight lang="sather">class MAIN is
 
ackermann(m, n:INTI):INTI is
Line 8,347 ⟶ 8,660:
=={{header|Scala}}==
 
<syntaxhighlight lang="scala">def ack(m: BigInt, n: BigInt): BigInt = {
if (m==0) n+1
else if (n==0) ack(m-1, 1)
Line 8,358 ⟶ 8,671:
</pre>
Memoized version using a mutable hash map:
<syntaxhighlight lang="scala">val ackMap = new mutable.HashMap[(BigInt,BigInt),BigInt]
def ackMemo(m: BigInt, n: BigInt): BigInt = {
ackMap.getOrElseUpdate((m,n), ack(m,n))
Line 8,364 ⟶ 8,677:
 
=={{header|Scheme}}==
<syntaxhighlight lang="scheme">(define (A m n)
(cond
((= m 0) (+ n 1))
Line 8,372 ⟶ 8,685:
An improved solution that uses a lazy data structure, streams, and defines [[Knuth up-arrow]]s to calculate iterative exponentiation:
 
<syntaxhighlight lang="scheme">(define (A m n)
(letrec ((A-stream
(cons-stream
Line 8,403 ⟶ 8,716:
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">clear
function acker=ackermann(m,n)
global calls
Line 8,457 ⟶ 8,770:
 
=={{header|Seed7}}==
===Basic version===
<syntaxhighlight 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,470 ⟶ 8,784:
end func;</syntaxhighlight>
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}}==
<syntaxhighlight lang=SETL"setl">program ackermann;
 
(for m in [0..3])
Line 8,485 ⟶ 8,891:
 
=={{header|Shen}}==
<syntaxhighlight lang="shen">(define ack
0 N -> (+ N 1)
M 0 -> (ack (- M 1) 1)
Line 8,492 ⟶ 8,898:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func A(m, n) {
m == 0 ? (n + 1)
: (n == 0 ? (A(m - 1, 1))
Line 8,499 ⟶ 8,905:
 
Alternatively, using multiple dispatch:
<syntaxhighlight 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)) }</syntaxhighlight>
 
Calling the function:
<syntaxhighlight lang="ruby">say A(3, 2); # prints: 29</syntaxhighlight>
 
=={{header|Simula}}==
as modified by R. Péter and R. Robinson:
<syntaxhighlight lang=Simula"simula"> BEGIN
INTEGER procedure
Ackermann(g, p); SHORT INTEGER g, p;
Line 8,532 ⟶ 8,938:
 
=={{header|Slate}}==
<syntaxhighlight lang="slate">m@(Integer traits) ackermann: n@(Integer traits)
[
m isZero
Line 8,543 ⟶ 8,949:
 
=={{header|Smalltalk}}==
<syntaxhighlight lang="smalltalk">|ackermann|
ackermann := [ :n :m |
(n = 0) ifTrue: [ (m + 1) ]
Line 8,560 ⟶ 8,966:
 
=={{header|SmileBASIC}}==
<syntaxhighlight lang="smilebasic">DEF ACK(M,N)
IF M==0 THEN
RETURN N+1
Line 8,573 ⟶ 8,979:
{{works with|Macro Spitbol}}
Both Snobol4+ and CSnobol stack overflow, at ack(3,3) and ack(3,4), respectively.
<syntaxhighlight lang=SNOBOL4"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,592 ⟶ 8,998:
 
=={{header|SNUSP}}==
<syntaxhighlight lang="snusp"> /==!/==atoi=@@@-@-----#
| | Ackermann function
| | /=========\!==\!====\ recursion:
Line 8,608 ⟶ 9,014:
=={{header|SPAD}}==
{{works with|FriCAS, OpenAxiom, Axiom}}
<syntaxhighlight lang=SPAD"spad">
NNI ==> NonNegativeInteger
 
Line 8,638 ⟶ 9,044:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<syntaxhighlight lang="sql pl">
--#SET TERMINATOR @
 
Line 8,721 ⟶ 9,127:
 
=={{header|Standard ML}}==
<syntaxhighlight 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))</syntaxhighlight>
 
=={{header|Stata}}==
<syntaxhighlight lang="stata">mata
function ackermann(m,n) {
if (m==0) {
Line 8,745 ⟶ 9,151:
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">func ackerman(m:Int, n:Int) -> Int {
if m == 0 {
return n+1
Line 8,758 ⟶ 9,164:
===Simple===
{{trans|Ruby}}
<syntaxhighlight lang="tcl">proc ack {m n} {
if {$m == 0} {
expr {$n + 1}
Line 8,769 ⟶ 9,175:
===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):
<syntaxhighlight lang="tcl">proc ack {m n} {
if {$m == 0} {
expr {$n + 1}
Line 8,781 ⟶ 9,187:
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}}
<syntaxhighlight lang="tcl">package require Tcl 8.6
 
# A memoization engine, from http://wiki.tcl.tk/18152
Line 8,842 ⟶ 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.)
 
<syntaxhighlight lang="ti83b">PROGRAM:ACKERMAN
:If not(M
:Then
Line 8,866 ⟶ 9,272:
Here is a handler function that makes the previous function easier to use. (You can name it whatever you want.)
 
<syntaxhighlight lang="ti83b">PROGRAM:AHANDLER
:0→dim(L1
:Prompt M
Line 8,874 ⟶ 9,280:
 
=={{header|TI-89 BASIC}}==
<syntaxhighlight 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))))</syntaxhighlight>
 
=={{header|TorqueScript}}==
<syntaxhighlight lang=TorqueScript"torquescript">function ackermann(%m,%n)
{
if(%m==0)
Line 8,886 ⟶ 9,292:
return ackermann(%m-1,ackermann(%m,%n-1));
}</syntaxhighlight>
 
=={{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}}==
<syntaxhighlight lang=TSESAL"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,912 ⟶ 9,338:
with memoization.
 
<syntaxhighlight lang="txrlisp">(defmacro defmemofun (name (. args) . body)
(let ((hash (gensym "hash-"))
(argl (gensym "args-"))
Line 8,960 ⟶ 9,386:
=={{header|UNIX Shell}}==
{{works with|Bash}}
<syntaxhighlight lang="bash">ack() {
local m=$1
local n=$2
Line 8,972 ⟶ 9,398:
}</syntaxhighlight>
Example:
<syntaxhighlight lang="bash">for ((m=0;m<=3;m++)); do
for ((n=0;n<=6;n++)); do
ack $m $n
Line 8,984 ⟶ 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.
<syntaxhighlight lang=Ursala"ursala">#import std
#import nat
 
Line 8,996 ⟶ 9,433:
^|R/~& ~&\1+ predecessor@l)</syntaxhighlight>
test program for the first 4 by 7 numbers:
<syntaxhighlight lang=Ursala"ursala">#cast %nLL
 
test = block7 ackermann*K0 iota~~/4 7</syntaxhighlight>
Line 9,009 ⟶ 9,446:
=={{header|V}}==
{{trans|Joy}}
<syntaxhighlight lang="v">[ack
[ [pop zero?] [popd succ]
[zero?] [pop pred 1 ack]
Line 9,015 ⟶ 9,452:
] when].</syntaxhighlight>
using destructuring view
<syntaxhighlight lang="v">[ack
[ [pop zero?] [ [m n : [n succ]] view i]
[zero?] [ [m n : [m pred 1 ack]] view i]
Line 9,022 ⟶ 9,459:
 
=={{header|Vala}}==
<syntaxhighlight lang="vala">uint64 ackermann(uint64 m, uint64 n) {
if (m == 0) return n + 1;
if (n == 0) return ackermann(m - 1, 1);
Line 9,081 ⟶ 9,518:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Private Function Ackermann_function(m As Variant, n As Variant) As Variant
Dim result As Variant
Debug.Assert m >= 0
Line 9,119 ⟶ 9,556:
Based on BASIC version. Uncomment all the lines referring to <code>depth</code> and see just how deep the recursion goes.
;Implementation
<syntaxhighlight lang="vb">option explicit
'~ dim depth
function ack(m, n)
Line 9,140 ⟶ 9,577:
end function</syntaxhighlight>
;Invocation
<syntaxhighlight lang="vb">wscript.echo ack( 1, 10 )
'~ depth = 0
wscript.echo ack( 2, 1 )
Line 9,155 ⟶ 9,592:
{{trans|Rexx}}
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
Option Explicit
Dim calls As Long
Line 9,229 ⟶ 9,666:
ackermann( 4 , 1 )= out of stack space</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="go">fn ackermann(m int, n int ) int {
if m == 0 {
return n + 1
Line 9,273 ⟶ 9,710:
 
=={{header|Wart}}==
<syntaxhighlight lang="wart">def (ackermann m n)
(if m=0
n+1
Line 9,282 ⟶ 9,719:
 
=={{header|WDTE}}==
<syntaxhighlight lang=WDTE"wdte">let memo a m n => true {
== m 0 => + n 1;
== n 0 => a (- m 1) 1;
Line 9,289 ⟶ 9,726:
 
=={{header|Wren}}==
<syntaxhighlight lang=ecmascript"wren">// To use recursion definition and declaration must be on separate lines
var Ackermann
Ackermann = Fn.new {|m, n|
Line 9,317 ⟶ 9,754:
{{works with|nasm}}
{{works with|windows}}
<syntaxhighlight lang="asm">
section .text
 
Line 9,350 ⟶ 9,787:
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">(defun ackermann (m n)
(cond
((= m 0) (+ n 1))
Line 9,356 ⟶ 9,793:
(t (ackermann (- m 1) (ackermann m (- n 1))))))</syntaxhighlight>
Test it:
<syntaxhighlight lang="lisp">(print (ackermann 3 9))</syntaxhighlight>
Output (after a very perceptible pause):
<pre>4093</pre>
That worked well. Test it again:
<syntaxhighlight lang="lisp">(print (ackermann 4 1))</syntaxhighlight>
Output (after another pause):
<pre>Abort: control stack overflow
Line 9,366 ⟶ 9,803:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes;
 
func Ackermann(M, N);
Line 9,394 ⟶ 9,831:
 
The following named template calculates the Ackermann function:
<syntaxhighlight lang="xml">
<xsl:template name="ackermann">
<xsl:param name="m"/>
Line 9,427 ⟶ 9,864:
 
Here it is as part of a template
<syntaxhighlight 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,476 ⟶ 9,913:
 
Which will transform this input
<syntaxhighlight lang="xml">
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="ackermann.xslt"?>
Line 9,668 ⟶ 10,105:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">sub ack(M,N)
if M = 0 return N + 1
if N = 0 return ack(M-1,1)
Line 9,679 ⟶ 10,116:
What smart code can get. Fast as lightning!
{{trans|Phix}}
<syntaxhighlight lang=Yabasic"yabasic">sub ack(m, n)
if m=0 then
return n+1
Line 9,709 ⟶ 10,146:
 
=={{header|Yorick}}==
<syntaxhighlight lang="yorick">func ack(m, n) {
if(m == 0)
return n + 1;
Line 9,718 ⟶ 10,155:
}</syntaxhighlight>
Example invocation:
<syntaxhighlight lang="yorick">for(m = 0; m <= 3; m++) {
for(n = 0; n <= 6; n++)
write, format="%d ", ack(m, n);
Line 9,729 ⟶ 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.
<syntaxhighlight lang="z80"> OPT --syntax=abf : OUTPUT "ackerman.com"
ORG $100
jr demo_start
Line 9,854 ⟶ 10,288:
Source -> http://ideone.com/53FzPA
Compiled -> http://ideone.com/OlS7zL
<syntaxhighlight lang="zed">(A) m n
comment:
(=) m 0
Line 9,885 ⟶ 10,319:
 
=={{header|Zig}}==
<syntaxhighlight 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,910 ⟶ 10,344:
=={{header|ZX Spectrum Basic}}==
{{trans|BASIC256}}
<syntaxhighlight 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,931 ⟶ 10,365:
{{out}}
<pre>A(3,7) = 1021</pre>
{{omit from|LaTeX}}
{{omit from|Make}}
{{omit from|PlainTeX}}
1,150

edits