2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,7 +1,9 @@
The '''[[wp:Ackermann function|Ackermann function]]''' is a classic example of a recursive function, notable especially because it is not a [[wp:Primitive_recursive_function|primitive recursive function]]. It grows very quickly in value, as does the size of its call tree.
The '''[[wp:Ackermann function|Ackermann function]]''' is a classic example of a recursive function, notable especially because it is not a [[wp:Primitive_recursive_function|primitive recursive function]]. It grows very quickly in value, as does the size of its call tree.
The Ackermann function is usually defined as follows:
<big>
:<math> A(m, n) =
\begin{cases}
n+1 & \mbox{if } m = 0 \\
@ -9,9 +11,13 @@ The Ackermann function is usually defined as follows:
A(m-1, A(m, n-1)) & \mbox{if } m > 0 \mbox{ and } n > 0.
\end{cases}
</math>
</big>
<!-- <table><tr><td width=12><td><td><math>n+1</math><td>if <math>m=0</math> <tr><td> <td><math>A(m, n) =</math> <td><math>A(m-1, 1)</math> <td>if <math>m>0</math> and <math>n=0</math> <tr><td><td><td><math>A(m-1, A(m, n-1))</math>&nbsp;&nbsp;<td> if <math>m>0</math> and <math>n>0</math></table> -->
Its arguments are never negative and it always terminates. Write a function which returns the value of <math>A(m, n)</math>. Arbitrary precision is preferred (since the function grows so quickly), but not required.
;See also:
* [[wp:Conway_chained_arrow_notation#Ackermann_function|Conway chained arrow notation]] for the Ackermann function.
<br><br>

View file

@ -1,5 +1,5 @@
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))
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

View file

@ -0,0 +1,13 @@
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' => f (fold n')
end.
End FOLD.
Definition ackermann : nat → nat → nat :=
fold (λ g, fold g (g (S O))) S.

View file

@ -8,8 +8,8 @@
m =>
0 ? [ n + 1 ]
> 0 ? [
n => 0 ? [ ackermann:(m - 1):1 ]
> 0 ? [ ackermann:(m - 1):(ackermann:m:(n-1)) ]
n => 0 ? [ ackermann eval:(m - 1):1 ]
> 0 ? [ ackermann eval:(m - 1):(ackermann eval:m:(n-1)) ]
]
].
@ -19,7 +19,7 @@
[
0 to:5 &doEach: (:j)
[
console writeLine:"A(":i:",":j:")=":(ackermann:i:j).
console writeLine:"A(":i:",":j:")=":(ackermann eval:i:j).
].
].

View file

@ -1,9 +1,9 @@
func Ackermann(m, n uint) uint {
switch {
case m == 0:
return n + 1
case n == 0:
return Ackermann(m - 1, 1)
}
return Ackermann(m - 1, Ackermann(m, n - 1))
switch 0 {
case m:
return n + 1
case n:
return Ackermann(m - 1, 1)
}
return Ackermann(m - 1, Ackermann(m, n - 1))
}

View file

@ -1,9 +1,10 @@
import Data.List (mapAccumL)
-- everything here are [Int] or [[Int]], which would overflow
-- * had it not overrun the stack first *
ackermann = iterate ack [1..] where
ack a = s where
s = a!!1 : f (tail a) (zipWith (-) s (1:s))
f a (b:bs) = (head aa) : f aa bs where
aa = drop b a
s = snd $ mapAccumL f (tail a) (1 : zipWith (-) s (1:s))
f a b = (aa, head aa) where aa = drop b a
main = mapM_ print $ map (\n -> take (6 - n) $ ackermann !! n) [0..5]

View file

@ -15,17 +15,17 @@ fun main(args: Array<String>) {
val N: Long = 20
val r = 0..N
for (m in 0..M) {
print("\nA(%d, %s) =".format(m, r))
print("\nA($m, $r) =")
var able = true
r forEach {
r.forEach {
try {
if (able) {
val a = A(m, it)
print(" %6d".format(a))
} else
print(" %6s".format("?"))
print(" ?")
} catch(e: Throwable) {
print(" %6s".format("?"))
print(" ?")
able = false
}
}

View file

@ -0,0 +1,14 @@
function Get-Ackermann ([int64]$m, [int64]$n)
{
if ($m -eq 0)
{
return $n + 1
}
if ($n -eq 0)
{
return Get-Ackermann ($m - 1) 1
}
return (Get-Ackermann ($m - 1) (Get-Ackermann $m ($n - 1)))
}

View file

@ -0,0 +1,3 @@
$ackermann = 0..3 | ForEach-Object {$m = $_; 0..6 | ForEach-Object {Get-Ackermann $m $_}}
$ackermann | Format-Wide {"{0,3}" -f $_} -Column 7 -Force

View file

@ -1,25 +1,25 @@
/*REXX program calculates and displays some values for the Ackermann function.*/
/* ╔════════════════════════════════════════════════════════════════════════╗
Note: the Ackermann function (as implemented here) utilizes deep
recursive and is limited by the largest number that can have
"1" (unity) added to a number (successfully and accurately).
*/
/*REXX program calculates and displays some values for the Ackermann function. */
/*╔════════════════════════════════════════════════════════════════════════╗
Note: the Ackermann function (as implemented here) utilizes deep
recursive and is limited by the largest number that can have
"1" (unity) added to a number (successfully and accurately).
*/
high=24
do j=0 to 3; say
do k=0 to high%(max(1,j))
call Ackermann_tell j,k
do j=0 to 3; say
do k=0 to high % (max(1, j))
call tell_Ack j, k
end /*k*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────ACKERMANN_TELL subroutine─────────────────*/
ackermann_tell: parse arg mm,nn; calls=0 /*display an echo message. */
#=right(nn,length(high))
say 'Ackermann('mm","#')='right(ackermann(mm,nn),high),
left('',12) 'calls='right(calls,high)
return
/*──────────────────────────────────ACKERMANN subroutine──────────────────────*/
ackermann: procedure expose calls /*compute value of Ackermann function.*/
parse arg m,n; calls=calls+1
if m==0 then return n+1
if n==0 then return ackermann(m-1,1)
return ackermann(m-1,ackermann(m,n-1))
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell_Ack: parse arg mm,nn; calls=0 /*display an echo message to terminal. */
#=right(nn,length(high))
say 'Ackermann('mm", "#')='right(ackermann(mm, nn), high),
left('', 12) 'calls='right(calls, high)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
ackermann: procedure expose calls /*compute value of Ackermann function. */
parse arg m,n; calls=calls+1
if m==0 then return n+1
if n==0 then return ackermann(m-1, 1)
return ackermann(m-1, ackermann(m, n-1) )

View file

@ -1,21 +1,21 @@
/*REXX program calculates and displays some values for the Ackermann function.*/
/*REXX program calculates and displays some values for the Ackermann function. */
high=24
do j=0 to 3; say
do k=0 to high%(max(1,j))
call Ackermann_tell j,k
do j=0 to 3; say
do k=0 to high % (max(1, j))
call tell_Ack j, k
end /*k*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────ACKERMANN_TELL subroutine─────────────────*/
ackermann_tell: parse arg mm,nn; calls=0 /*display an echo message.*/
#=right(nn,length(high))
say 'Ackermann('mm","#')='right(ackermann(mm,nn),high),
left('',12) 'calls='right(calls,high)
return
/*──────────────────────────────────ACKERMANN subroutine──────────────────────*/
ackermann: procedure expose calls /*compute value of Ackermann function.*/
parse arg m,n; calls=calls+1
if m==0 then return n+1
if n==0 then return ackermann(m-1,1)
if m==2 then return n*2+3
return ackermann(m-1,ackermann(m,n-1))
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell_Ack: parse arg mm,nn; calls=0 /*display an echo message to terminal. */
#=right(nn,length(high))
say 'Ackermann('mm", "#')='right(ackermann(mm, nn), high),
left('', 12) 'calls='right(calls, high)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
ackermann: procedure expose calls /*compute value of Ackermann function. */
parse arg m,n; calls=calls+1
if m==0 then return n + 1
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) )

View file

@ -1,36 +1,36 @@
/*REXX program calculates and displays some values for the Ackermann function.*/
/*REXX program calculates and displays some values for the Ackermann function. */
numeric digits 100 /*use up to 100 decimal digit integers.*/
/*╔═════════════════════════════════════════════════════════════╗
When REXX raises a number to an integer power (via the **
operator, the power can be positive, zero, or negative).
Ackermann(5,1) is a bit impractical to calculate.
*/
high=24
numeric digits 100 /*have REXX to use up to 100 digit integers.*/
/*When REXX raises a number to a power (via */
/* the ** operator), the power must be an */
/* integer (positive, zero, or negative). */
do j=0 to 4; say /*Ackermann(5,1) is a bit impractical to calc.*/
do k=0 to high%(max(1,j))
call Ackermann_tell j,k
if j==4 & k==2 then leave /*there's no sense in going overboard. */
end /*k*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────ACKERMANN_TELL subroutine─────────────────*/
ackermann_tell: parse arg mm,nn; calls=0 /*display an echo message.*/
#=right(nn,length(high))
say 'Ackermann('mm","#')='right(ackermann(mm,nn),high),
left('',12) 'calls='right(calls,high)
return
/*──────────────────────────────────ACKERMANN subroutine──────────────────────*/
ackermann: procedure expose calls /*compute value of Ackermann function.*/
parse arg m,n; calls=calls+1
if m==0 then return n+1
if m==1 then return n+2
if m==2 then return n+n+3
if m==3 then return 2**(n+3)-3
if m==4 then do; a=2 /* [↓] Ugh! ··· and more ughs. */
do (n+3)-1 /*This is where the heavy lifting is. */
a=2**a
end
return a-3
end
if n==0 then return ackermann(m-1,1)
return ackermann(m-1,ackermann(m,n-1))
do j=0 to 4; say
do k=0 to high % (max(1, j))
call tell_Ack j, k
if j==4 & k==2 then leave /*there's no sense in going overboard. */
end /*k*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell_Ack: parse arg mm,nn; calls=0 /*display an echo message to terminal. */
#=right(nn,length(high))
say 'Ackermann('mm", "#')='right(ackermann(mm, nn), high),
left('', 12) 'calls='right(calls, high)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
ackermann: procedure expose calls /*compute value of Ackermann function. */
parse arg m,n; calls=calls+1
if m==0 then return n + 1
if m==1 then return n + 2
if m==2 then return n + 3 + n
if m==3 then return 2**(n+3) - 3
if m==4 then do; #=2 /* [↓] Ugh! ··· and still more ughs.*/
do (n+3)-1 /*This is where the heavy lifting is. */
#=2**#
end
return #-3
end
if n==0 then return ackermann(m-1, 1)
return ackermann(m-1, ackermann(m, n-1) )

View file

@ -1,9 +1,7 @@
print ackermann(1, 2)
function ackermann(m, n)
if (m < 0) or (n < 0) then goto [exitFunction]
if (m = 0) then ackermann = (n + 1)
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)))
[exitFunction]
end function

View file

@ -0,0 +1,19 @@
10 DIM s(2000,3)
20 LET s(1,1)=3: REM M
30 LET s(1,2)=7: REM N
40 LET lev=1
50 GO SUB 100
60 PRINT "A(";s(1,1);",";s(1,2);") = ";s(1,3)
70 STOP
100 IF s(lev,1)=0 THEN LET s(lev,3)=s(lev,2)+1: RETURN
110 IF s(lev,2)=0 THEN LET lev=lev+1: LET s(lev,1)=s(lev-1,1)-1: LET s(lev,2)=1: GO SUB 100: LET s(lev-1,3)=s(lev,3): LET lev=lev-1: RETURN
120 LET lev=lev+1
130 LET s(lev,1)=s(lev-1,1)
140 LET s(lev,2)=s(lev-1,2)-1
150 GO SUB 100
160 LET s(lev,1)=s(lev-1,1)-1
170 LET s(lev,2)=s(lev,3)
180 GO SUB 100
190 LET s(lev-1,3)=s(lev,3)
200 LET lev=lev-1
210 RETURN