Ackermann function: Difference between revisions

added RPL
(Add Ursalang version.)
(added RPL)
(10 intermediate revisions by 7 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}}==
Line 1,733 ⟶ 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,864 ⟶ 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,178 ⟶ 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 8,055 ⟶ 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,487 ⟶ 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,341 ⟶ 9,411:
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,656 ⟶ 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