Ackermann function: Difference between revisions

added RPL
(added RPL)
(21 intermediate revisions by 13 users not shown)
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!}}==
Line 1,048 ⟶ 1,083:
 
=={{header|AppleScript}}==
 
<syntaxhighlight lang="applescript">on ackermann(m, n)
if m is equal to 0 then return n + 1
Line 1,067 ⟶ 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}}
Line 1,235 ⟶ 1,270:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">ackermann: function [m,n][
(m=0)? -> n+1 [
Line 1,248 ⟶ 1,282:
]
]</syntaxhighlight>
 
{{out}}
 
<pre>ackermann 0 0 => 1
ackermann 0 1 => 2
Line 1,384 ⟶ 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,409 ⟶ 1,439:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="basic">100 DIM R%(2900),M(2900),N(2900)
110 FOR M = 0 TO 3
Line 1,469 ⟶ 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
Line 1,492 ⟶ 1,521:
end if
end function</syntaxhighlight>
 
{{out}}
<pre>0 0 1
0 0 1
0 1 2
0 2 3
Line 1,524 ⟶ 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,686 ⟶ 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}}==
Line 1,817 ⟶ 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}}==
Line 3,074 ⟶ 3,162:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
procfunc ackerm m n . r .
if m = 0
r = return n + 1
elif n = 0
call return ackerm (m - 1) 1 r
else
call return ackerm (m - 1) ackerm m (n - 1 h)
.
call ackerm m - 1 h r
.
.
callprint ackerm 3 6 r
print r
</syntaxhighlight>
 
Line 3,133 ⟶ 3,219:
 
=={{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,233 ⟶ 3,319:
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}}==
Line 3,848 ⟶ 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}}==
Line 5,164 ⟶ 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}}==
Line 7,970 ⟶ 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}}==
Line 8,402 ⟶ 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}}==
Line 9,255 ⟶ 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}}==
Line 9,560 ⟶ 9,726:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">// To use recursion definition and declaration must be on separate lines
var Ackermann
Ackermann = Fn.new {|m, n|
1,150

edits