Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,6 +1,6 @@
|
|||
The '''[[wp:Ackermann function|Ackermann function]]''' is a classic recursive example in computer science.
|
||||
It is a function that grows very quickly (in its value and in the size of its call tree).
|
||||
It is defined as follows:
|
||||
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:
|
||||
|
||||
:<math> A(m, n) =
|
||||
\begin{cases}
|
||||
|
|
@ -13,7 +13,5 @@ It is defined as follows:
|
|||
|
||||
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.
|
||||
|
||||
Interestingly enough, the Ackermann function is one of the very few known examples of function that can ''only'' be implemented recursively. It is impossible to implement it with just for loops and other control flow commands. See this [http://youtube.com/watch?v=i7sm9dzFtEI computerphile episode] for more information.
|
||||
|
||||
;See also:
|
||||
* [[wp:Conway_chained_arrow_notation#Ackermann_function|Conway chained arrow notation]] for the Ackermann function.
|
||||
|
|
|
|||
77
Task/Ackermann-function/360-Assembly/ackermann-function.360
Normal file
77
Task/Ackermann-function/360-Assembly/ackermann-function.360
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
* Ackermann function 07/09/2015
|
||||
ACKERMAN CSECT
|
||||
USING ACKERMAN,R12 r12 : base register
|
||||
LR R12,R15 establish base register
|
||||
ST R14,SAVER14A save r14
|
||||
LA R4,0 m=0
|
||||
LOOPM CH R4,=H'3' do m=0 to 3
|
||||
BH ELOOPM
|
||||
LA R5,0 n=0
|
||||
LOOPN CH R5,=H'8' do n=0 to 8
|
||||
BH ELOOPN
|
||||
LR R1,R4 m
|
||||
LR R2,R5 n
|
||||
BAL R14,ACKER r1=acker(m,n)
|
||||
XDECO R1,PG+19
|
||||
XDECO R4,XD
|
||||
MVC PG+10(2),XD+10
|
||||
XDECO R5,XD
|
||||
MVC PG+13(2),XD+10
|
||||
XPRNT PG,44 print buffer
|
||||
LA R5,1(R5) n=n+1
|
||||
B LOOPN
|
||||
ELOOPN LA R4,1(R4) m=m+1
|
||||
B LOOPM
|
||||
ELOOPM L R14,SAVER14A restore r14
|
||||
BR R14 return to caller
|
||||
SAVER14A DS F static save r14
|
||||
PG DC CL44'Ackermann(xx,xx) = xxxxxxxxxxxx'
|
||||
XD DS CL12
|
||||
ACKER CNOP 0,4 function r1=acker(r1,r2)
|
||||
LR R3,R1 save argument r1 in r3
|
||||
LR R9,R10 save stackptr (r10) in r9 temp
|
||||
LA R1,STACKLEN amount of storage required
|
||||
GETMAIN RU,LV=(R1) allocate storage for stack
|
||||
USING STACK,R10 make storage addressable
|
||||
LR R10,R1 establish stack addressability
|
||||
ST R14,SAVER14B save previous r14
|
||||
ST R9,SAVER10B save previous r10
|
||||
LR R1,R3 restore saved argument r1
|
||||
START ST R1,M stack m
|
||||
ST R2,N stack n
|
||||
IF1 C R1,=F'0' if m<>0
|
||||
BNE IF2 then goto if2
|
||||
LR R11,R2 n
|
||||
LA R11,1(R11) return n+1
|
||||
B EXIT
|
||||
IF2 C R2,=F'0' else if m<>0
|
||||
BNE IF3 then goto if3
|
||||
BCTR R1,0 m=m-1
|
||||
LA R2,1 n=1
|
||||
BAL R14,ACKER r1=acker(m)
|
||||
LR R11,R1 return acker(m-1,1)
|
||||
B EXIT
|
||||
IF3 BCTR R2,0 n=n-1
|
||||
BAL R14,ACKER r1=acker(m,n-1)
|
||||
LR R2,R1 acker(m,n-1)
|
||||
L R1,M m
|
||||
BCTR R1,0 m=m-1
|
||||
BAL R14,ACKER r1=acker(m-1,acker(m,n-1))
|
||||
LR R11,R1 return acker(m-1,1)
|
||||
EXIT L R14,SAVER14B restore r14
|
||||
L R9,SAVER10B restore r10 temp
|
||||
LA R0,STACKLEN amount of storage to free
|
||||
FREEMAIN A=(R10),LV=(R0) free allocated storage
|
||||
LR R1,R11 value returned
|
||||
LR R10,R9 restore r10
|
||||
BR R14 return to caller
|
||||
LTORG
|
||||
DROP R12 base no longer needed
|
||||
STACK DSECT dynamic area
|
||||
SAVER14B DS F saved r14
|
||||
SAVER10B DS F saved r10
|
||||
M DS F m
|
||||
N DS F n
|
||||
STACKLEN EQU *-STACK
|
||||
YREGS
|
||||
END ACKERMAN
|
||||
|
|
@ -0,0 +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))
|
||||
end ackermann
|
||||
3
Task/Ackermann-function/Befunge/ackermann-function-1.bf
Normal file
3
Task/Ackermann-function/Befunge/ackermann-function-1.bf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
&>&>vvg0>#0\#-:#1_1v
|
||||
@v:\<vp0 0:-1<\+<
|
||||
^>00p>:#^_$1+\:#^_$.
|
||||
|
|
@ -1,6 +1,15 @@
|
|||
note
|
||||
description: "Example of Ackerman function"
|
||||
URI: "http://rosettacode.org/wiki/Ackermann_function"
|
||||
synopsis: "[
|
||||
The EIS link below (in Eiffel Studio) will launch in either an in-IDE browser or
|
||||
and external browser (your choice). The protocol informs Eiffel Studio about what
|
||||
program to use to open the `src' reference, which can be URI, PDF, or DOC. See
|
||||
second EIS for more information.
|
||||
]"
|
||||
EIS: "name=Ackermann_function", "protocol=URI", "tag=rosetta_code",
|
||||
"src=http://rosettacode.org/wiki/Ackermann_function"
|
||||
EIS: "name=eis_protocols", "protocol=URI", "tag=eiffel_docs",
|
||||
"src=https://docs.eiffel.com/book/eiffelstudio/protocols"
|
||||
|
||||
class
|
||||
APPLICATION
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@
|
|||
|
||||
#symbol program =
|
||||
[
|
||||
control forrange &int:0 &int:3 &do: (&int:i)
|
||||
0 to:3 &doEach: (:i)
|
||||
[
|
||||
control forrange &int:0 &int:5 &do: (&int:j)
|
||||
0 to:5 &doEach: (:j)
|
||||
[
|
||||
consoleEx writeLine:"A(":i:",":j:")=":(ackermann:i:j).
|
||||
console writeLine:"A(":i:",":j:")=":(ackermann:i:j).
|
||||
].
|
||||
].
|
||||
|
||||
|
|
|
|||
|
|
@ -3,3 +3,7 @@ defmodule Ackermann do
|
|||
def ack(m, 0), do: ack(m - 1, 1)
|
||||
def ack(m, n), do: ack(m - 1, ack(m, n - 1))
|
||||
end
|
||||
|
||||
Enum.each(0..3, fn m ->
|
||||
IO.puts Enum.map_join(0..6, " ", fn n -> Ackermann.ack(m, n) end)
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
-module(ack).
|
||||
-export([main/1, ack/2]).
|
||||
-module(ackermann).
|
||||
-export([ackermann/2]).
|
||||
|
||||
main( [A, B] ) ->
|
||||
io:fwrite( "~p~n",[ack(erlang:list_to_integer(A), erlang:list_to_integer(B))] ).
|
||||
|
||||
ack(0,N) -> N + 1;
|
||||
ack(M,0) -> ack(M-1, 1);
|
||||
ack(M,N) -> ack(M-1,ack(M,N-1)).
|
||||
ackermann(0, N) ->
|
||||
N+1;
|
||||
ackermann(M, 0) ->
|
||||
ackermann(M-1, 1);
|
||||
ackermann(M, N) when M > 0 andalso N > 0 ->
|
||||
ackermann(M-1, ackermann(M, N-1)).
|
||||
|
|
|
|||
33
Task/Ackermann-function/Kotlin/ackermann-function.kotlin
Normal file
33
Task/Ackermann-function/Kotlin/ackermann-function.kotlin
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package ackermann
|
||||
|
||||
fun A(m: Long, n: Long): Long = when {
|
||||
m == 0L -> n + 1
|
||||
m > 0L -> when {
|
||||
n == 0L -> A(m - 1, 1)
|
||||
n > 0L -> A(m - 1, A(m, n - 1))
|
||||
else -> throw IllegalArgumentException("illegal n")
|
||||
}
|
||||
else -> throw IllegalArgumentException("illegal m")
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val M: Long = 4
|
||||
val N: Long = 20
|
||||
val r = 0..N
|
||||
for (m in 0..M) {
|
||||
print("\nA(%d, %s) =".format(m, r))
|
||||
var able = true
|
||||
r forEach {
|
||||
try {
|
||||
if (able) {
|
||||
val a = A(m, it)
|
||||
print(" %6d".format(a))
|
||||
} else
|
||||
print(" %6s".format("?"))
|
||||
} catch(e: Throwable) {
|
||||
print(" %6s".format("?"))
|
||||
able = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Task/Ackermann-function/Nemerle/ackermann-function-1.nemerle
Normal file
20
Task/Ackermann-function/Nemerle/ackermann-function-1.nemerle
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using Nemerle.IO;
|
||||
|
||||
|
||||
def ackermann(m, n) {
|
||||
def A = ackermann;
|
||||
match(m, n) {
|
||||
| (0, n) => n + 1
|
||||
| (m, 0) when m > 0 => A(m - 1, 1)
|
||||
| (m, n) when m > 0 && n > 0 => A(m - 1, A(m, n - 1))
|
||||
| _ => throw Exception("invalid inputs");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(mutable m = 0; m < 4; m++) {
|
||||
for(mutable n = 0; n < 5; n++) {
|
||||
print("ackermann($m, $n) = $(ackermann(m, n))\n");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def ackermann(m, n) {
|
||||
| (0, n) => n + 1
|
||||
| (m, 0) when m > 0 => ackermann(m - 1, 1)
|
||||
| (m, n) when m > 0 && n > 0 => ackermann(m - 1, ackermann(m, n - 1))
|
||||
| _ => throw Exception("invalid inputs");
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def ackermann = {
|
||||
def A(m, n) {
|
||||
| (0, n) => n + 1
|
||||
| (m, 0) when m > 0 => A(m - 1, 1)
|
||||
| (m, n) when m > 0 && n > 0 => A(m - 1, A(m, n - 1))
|
||||
| _ => throw Exception("invalid inputs");
|
||||
}
|
||||
A
|
||||
}
|
||||
30
Task/Ackermann-function/Objeck/ackermann-function.objeck
Normal file
30
Task/Ackermann-function/Objeck/ackermann-function.objeck
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
class Ackermann {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
for(m := 0; m <= 3; ++m;) {
|
||||
for(n := 0; n <= 4; ++n;) {
|
||||
a := Ackermann(m, n);
|
||||
if(a > 0) {
|
||||
"Ackermann({$m}, {$n}) = {$a}"->PrintLine();
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function : Ackermann(m : Int, n : Int) ~ Int {
|
||||
if(m > 0) {
|
||||
if (n > 0) {
|
||||
return Ackermann(m - 1, Ackermann(m, n - 1));
|
||||
}
|
||||
else if (n = 0) {
|
||||
return Ackermann(m - 1, 1);
|
||||
};
|
||||
}
|
||||
else if(m = 0) {
|
||||
if(n >= 0) {
|
||||
return n + 1;
|
||||
};
|
||||
};
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
21
Task/Ackermann-function/PL-SQL/ackermann-function.sql
Normal file
21
Task/Ackermann-function/PL-SQL/ackermann-function.sql
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
DECLARE
|
||||
|
||||
FUNCTION ackermann(pi_m IN NUMBER,
|
||||
pi_n IN NUMBER) RETURN NUMBER IS
|
||||
BEGIN
|
||||
IF pi_m = 0 THEN
|
||||
RETURN pi_n + 1;
|
||||
ELSIF pi_n = 0 THEN
|
||||
RETURN ackermann(pi_m - 1, 1);
|
||||
ELSE
|
||||
RETURN ackermann(pi_m - 1, ackermann(pi_m, pi_n - 1));
|
||||
END IF;
|
||||
END ackermann;
|
||||
|
||||
BEGIN
|
||||
FOR n IN 0 .. 6 LOOP
|
||||
FOR m IN 0 .. 3 LOOP
|
||||
dbms_output.put_line('A(' || m || ',' || n || ') = ' || ackermann(m, n));
|
||||
END LOOP;
|
||||
END LOOP;
|
||||
END;
|
||||
34
Task/Ackermann-function/Perl/ackermann-function-5.pl
Normal file
34
Task/Ackermann-function/Perl/ackermann-function-5.pl
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use Math::BigInt;
|
||||
|
||||
use constant two => Math::BigInt->new(2);
|
||||
|
||||
sub ack {
|
||||
my $n = pop;
|
||||
while( @_ ) {
|
||||
my $m = pop;
|
||||
if( $m > 3 ) {
|
||||
push @_, (--$m) x $n;
|
||||
push @_, reverse 3 .. --$m;
|
||||
$n = 13;
|
||||
} elsif( $m == 3 ) {
|
||||
if( $n < 29 ) {
|
||||
$n = ( 1 << ( $n + 3 ) ) - 3;
|
||||
} else {
|
||||
$n = two ** ( $n + 3 ) - 3;
|
||||
}
|
||||
} elsif( $m == 2 ) {
|
||||
$n = 2 * $n + 3;
|
||||
} elsif( $m >= 0 ) {
|
||||
$n = $n + $m + 1;
|
||||
} else {
|
||||
die "negative m!";
|
||||
}
|
||||
}
|
||||
$n;
|
||||
}
|
||||
|
||||
print "ack(3,4) is ", ack(3,4), "\n";
|
||||
print "ack(4,1) is ", ack(4,1), "\n";
|
||||
print "ack(4,2) has ", length(ack(4,2)), " digits\n";
|
||||
45
Task/Ackermann-function/Pure-Data/ackermann-function.pure
Normal file
45
Task/Ackermann-function/Pure-Data/ackermann-function.pure
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#N canvas 741 265 450 436 10;
|
||||
#X obj 83 111 t b l;
|
||||
#X obj 115 163 route 0;
|
||||
#X obj 115 185 + 1;
|
||||
#X obj 83 380 f;
|
||||
#X obj 161 186 swap;
|
||||
#X obj 161 228 route 0;
|
||||
#X obj 161 250 - 1;
|
||||
#X obj 161 208 pack;
|
||||
#X obj 115 314 t f f;
|
||||
#X msg 161 272 \$1 1;
|
||||
#X obj 115 142 t l;
|
||||
#X obj 207 250 swap;
|
||||
#X obj 273 271 - 1;
|
||||
#X obj 207 272 t f f;
|
||||
#X obj 207 298 - 1;
|
||||
#X obj 207 360 pack;
|
||||
#X obj 239 299 pack;
|
||||
#X obj 83 77 inlet;
|
||||
#X obj 83 402 outlet;
|
||||
#X connect 0 0 3 0;
|
||||
#X connect 0 1 10 0;
|
||||
#X connect 1 0 2 0;
|
||||
#X connect 1 1 4 0;
|
||||
#X connect 2 0 8 0;
|
||||
#X connect 3 0 18 0;
|
||||
#X connect 4 0 7 0;
|
||||
#X connect 4 1 7 1;
|
||||
#X connect 5 0 6 0;
|
||||
#X connect 5 1 11 0;
|
||||
#X connect 6 0 9 0;
|
||||
#X connect 7 0 5 0;
|
||||
#X connect 8 0 3 1;
|
||||
#X connect 8 1 15 1;
|
||||
#X connect 9 0 10 0;
|
||||
#X connect 10 0 1 0;
|
||||
#X connect 11 0 13 0;
|
||||
#X connect 11 1 12 0;
|
||||
#X connect 12 0 16 1;
|
||||
#X connect 13 0 14 0;
|
||||
#X connect 13 1 16 0;
|
||||
#X connect 14 0 15 0;
|
||||
#X connect 15 0 10 0;
|
||||
#X connect 16 0 10 0;
|
||||
#X connect 17 0 0 0;
|
||||
|
|
@ -1,25 +1,25 @@
|
|||
/*REXX program calculates/shows some values for the Ackermann function. */
|
||||
|
||||
/*Note: the Ackermann function (as implemented) is */
|
||||
/* higly recursive and is limited by the */
|
||||
/* biggest number that can have "1" added to */
|
||||
/* a number (successfully, 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
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ACKERMANN_TELL subroutine───────────*/
|
||||
ackermann_tell: parse arg mm,nn; calls=0 /*display an echo message.*/
|
||||
nnn=right(nn,length(high))
|
||||
say 'Ackermann('mm","nnn')='right(ackermann(mm,nn),high),
|
||||
left('',12) 'calls='right(calls,high)
|
||||
do j=0 to 3; say
|
||||
do k=0 to high%(max(1,j))
|
||||
call Ackermann_tell 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 the Ackerman function. */
|
||||
/*──────────────────────────────────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))
|
||||
if m==0 then return n+1
|
||||
if n==0 then return ackermann(m-1,1)
|
||||
return ackermann(m-1,ackermann(m,n-1))
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
/*REXX program calculates/shows 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
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ACKERMANN_TELL subroutine───────────*/
|
||||
ackermann_tell: parse arg mm,nn; calls=0 /*display an echo message.*/
|
||||
nnn=right(nn,length(high))
|
||||
say 'Ackermann('mm","nnn')='right(ackermann(mm,nn),high),
|
||||
left('',12) 'calls='right(calls,10)
|
||||
do j=0 to 3; say
|
||||
do k=0 to high%(max(1,j))
|
||||
call Ackermann_tell 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 the Ackerman function. */
|
||||
/*──────────────────────────────────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)
|
||||
|
|
|
|||
|
|
@ -1,36 +1,36 @@
|
|||
/*REXX program calculates/shows some values for the Ackermann function. */
|
||||
/*REXX program calculates and displays some values for the Ackermann function.*/
|
||||
high=24
|
||||
numeric digits 100 /*have REXX to use up to 100 digit integers.*/
|
||||
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). */
|
||||
/*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 /*no sense in going overboard.*/
|
||||
end /*k*/
|
||||
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 done.*/
|
||||
/*──────────────────────────────────ACKERMANN_TELL subroutine───────────*/
|
||||
ackermann_tell: parse arg mm,nn; calls=0 /*display an echo message.*/
|
||||
nnn=right(nn,length(high))
|
||||
say 'Ackermann('mm","nnn')='right(ackermann(mm,nn),high),
|
||||
left('',12) 'calls='right(calls,10)
|
||||
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 the Ackerman function. */
|
||||
/*──────────────────────────────────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
|
||||
do (n+3)-1 /*ugh!*/
|
||||
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))
|
||||
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))
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
// works for Rust 0.9
|
||||
fn main() {
|
||||
let a: int = ack(3, 4); // 125
|
||||
println!("{}", a.to_str());
|
||||
fn ack(m: isize, n: isize) -> isize {
|
||||
if m == 0 {
|
||||
n + 1
|
||||
} else if n == 0 {
|
||||
ack(m - 1, 1)
|
||||
} else {
|
||||
ack(m - 1, ack(m, n - 1))
|
||||
}
|
||||
}
|
||||
|
||||
fn ack(m: int, n: int) -> int {
|
||||
if m == 0 {
|
||||
n + 1
|
||||
} else if n == 0 {
|
||||
ack(m - 1, 1)
|
||||
} else {
|
||||
ack(m - 1, ack(m, n - 1))
|
||||
}
|
||||
fn main() {
|
||||
let a = ack(3, 4);
|
||||
println!("{}", a); // 125
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,22 @@
|
|||
@(do
|
||||
(defmacro defmemofun (name (. args) . body)
|
||||
(let ((hash (gensym "hash-"))
|
||||
(argl (gensym "args-"))
|
||||
(hent (gensym "hent-"))
|
||||
(uniq (copy-str "uniq")))
|
||||
^(let ((,hash (hash :equal-based)))
|
||||
(defun ,name (,*args)
|
||||
(let* ((,argl (list ,*args))
|
||||
(,hent (inhash ,hash ,argl ,uniq)))
|
||||
(if (eq (cdr ,hent) ,uniq)
|
||||
(set (cdr ,hent) (block ,name (progn ,*body)))
|
||||
(cdr ,hent)))))))
|
||||
(defmacro defmemofun (name (. args) . body)
|
||||
(let ((hash (gensym "hash-"))
|
||||
(argl (gensym "args-"))
|
||||
(hent (gensym "hent-"))
|
||||
(uniq (copy-str "uniq")))
|
||||
^(let ((,hash (hash :equal-based)))
|
||||
(defun ,name (,*args)
|
||||
(let* ((,argl (list ,*args))
|
||||
(,hent (inhash ,hash ,argl ,uniq)))
|
||||
(if (eq (cdr ,hent) ,uniq)
|
||||
(set (cdr ,hent) (block ,name (progn ,*body)))
|
||||
(cdr ,hent)))))))
|
||||
|
||||
(defmemofun ack (m n)
|
||||
(cond
|
||||
((= m 0) (+ n 1))
|
||||
((= n 0) (ack (- m 1) 1))
|
||||
(t (ack (- m 1) (ack m (- n 1))))))
|
||||
(defmemofun ack (m n)
|
||||
(cond
|
||||
((= m 0) (+ n 1))
|
||||
((= n 0) (ack (- m 1) 1))
|
||||
(t (ack (- m 1) (ack m (- n 1))))))
|
||||
|
||||
(each ((i (range 0 3)))
|
||||
(each ((j (range 0 4)))
|
||||
(format t "ack(~a, ~a) = ~a\n" i j (ack i j)))))
|
||||
(each ((i (range 0 3)))
|
||||
(each ((j (range 0 4)))
|
||||
(format t "ack(~a, ~a) = ~a\n" i j (ack i j))))
|
||||
|
|
|
|||
29
Task/Ackermann-function/XSLT/ackermann-function-1.xslt
Normal file
29
Task/Ackermann-function/XSLT/ackermann-function-1.xslt
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<xsl:template name="ackermann">
|
||||
<xsl:param name="m"/>
|
||||
<xsl:param name="n"/>
|
||||
|
||||
<xsl:choose>
|
||||
<xsl:when test="$m = 0">
|
||||
<xsl:value-of select="$n+1"/>
|
||||
</xsl:when>
|
||||
<xsl:when test="$n = 0">
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="$m - 1"/>
|
||||
<xsl:with-param name="n" select="'1'"/>
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
<xsl:otherwise>
|
||||
<xsl:variable name="p">
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="$m"/>
|
||||
<xsl:with-param name="n" select="$n - 1"/>
|
||||
</xsl:call-template>
|
||||
</xsl:variable>
|
||||
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="$m - 1"/>
|
||||
<xsl:with-param name="n" select="$p"/>
|
||||
</xsl:call-template>
|
||||
</xsl:otherwise>
|
||||
</xsl:choose>
|
||||
</xsl:template>
|
||||
45
Task/Ackermann-function/XSLT/ackermann-function-2.xslt
Normal file
45
Task/Ackermann-function/XSLT/ackermann-function-2.xslt
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
|
||||
|
||||
<xsl:template match="arguments">
|
||||
<xsl:for-each select="args">
|
||||
<div>
|
||||
<xsl:value-of select="m"/>, <xsl:value-of select="n"/>:
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="m"/>
|
||||
<xsl:with-param name="n" select="n"/>
|
||||
</xsl:call-template>
|
||||
</div>
|
||||
</xsl:for-each>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template name="ackermann">
|
||||
<xsl:param name="m"/>
|
||||
<xsl:param name="n"/>
|
||||
|
||||
<xsl:choose>
|
||||
<xsl:when test="$m = 0">
|
||||
<xsl:value-of select="$n+1"/>
|
||||
</xsl:when>
|
||||
<xsl:when test="$n = 0">
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="$m - 1"/>
|
||||
<xsl:with-param name="n" select="'1'"/>
|
||||
</xsl:call-template>
|
||||
</xsl:when>
|
||||
<xsl:otherwise>
|
||||
<xsl:variable name="p">
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="$m"/>
|
||||
<xsl:with-param name="n" select="$n - 1"/>
|
||||
</xsl:call-template>
|
||||
</xsl:variable>
|
||||
|
||||
<xsl:call-template name="ackermann">
|
||||
<xsl:with-param name="m" select="$m - 1"/>
|
||||
<xsl:with-param name="n" select="$p"/>
|
||||
</xsl:call-template>
|
||||
</xsl:otherwise>
|
||||
</xsl:choose>
|
||||
</xsl:template>
|
||||
</xsl:stylesheet>
|
||||
148
Task/Ackermann-function/XSLT/ackermann-function-3.xslt
Normal file
148
Task/Ackermann-function/XSLT/ackermann-function-3.xslt
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
<?xml version="1.0" ?>
|
||||
<?xml-stylesheet type="text/xsl" href="ackermann.xslt"?>
|
||||
<arguments>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>0</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>1</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>2</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>3</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>4</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>5</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>6</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>7</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>0</m>
|
||||
<n>8</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>0</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>1</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>2</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>3</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>4</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>5</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>6</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>7</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>1</m>
|
||||
<n>8</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>0</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>1</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>2</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>3</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>4</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>5</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>6</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>7</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>2</m>
|
||||
<n>8</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>0</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>1</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>2</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>3</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>4</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>5</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>6</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>7</n>
|
||||
</args>
|
||||
<args>
|
||||
<m>3</m>
|
||||
<n>8</n>
|
||||
</args>
|
||||
</arguments>
|
||||
Loading…
Add table
Add a link
Reference in a new issue