Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -15,8 +15,10 @@ The Ackermann function is usually defined as follows:
|
|||
|
||||
<!-- <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> <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.
|
||||
Its arguments are never negative and it always terminates.
|
||||
|
||||
;Task:
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
function ack(M,N) {
|
||||
for (; M > 0; M--) {
|
||||
N = N === 0 ? 1 : ack(M,N-1);
|
||||
}
|
||||
return N+1;
|
||||
}
|
||||
20
Task/Ackermann-function/JavaScript/ackermann-function-3.js
Normal file
20
Task/Ackermann-function/JavaScript/ackermann-function-3.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
function stackermann(M, N) {
|
||||
const stack = [];
|
||||
for (;;) {
|
||||
if (M === 0) {
|
||||
N++;
|
||||
if (stack.length === 0) return N;
|
||||
const r = stack[stack.length-1];
|
||||
if (r[1] === 1) stack.length--;
|
||||
else r[1]--;
|
||||
M = r[0];
|
||||
} else if (N === 0) {
|
||||
M--;
|
||||
N = 1;
|
||||
} else {
|
||||
M--
|
||||
stack.push([M, N]);
|
||||
N = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Task/Ackermann-function/JavaScript/ackermann-function-4.js
Normal file
26
Task/Ackermann-function/JavaScript/ackermann-function-4.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env nodejs
|
||||
function ack(M, N){
|
||||
const next = new Float64Array(M + 1);
|
||||
const goal = new Float64Array(M + 1).fill(1, 0, M);
|
||||
const n = N + 1;
|
||||
|
||||
// This serves as a sentinel value;
|
||||
// next[M] never equals goal[M] == -1,
|
||||
// so we don't need an extra check for
|
||||
// loop termination below.
|
||||
goal[M] = -1;
|
||||
|
||||
let v;
|
||||
do {
|
||||
v = next[0] + 1;
|
||||
let m = 0;
|
||||
while (next[m] === goal[m]) {
|
||||
goal[m] = v;
|
||||
next[m++]++;
|
||||
}
|
||||
next[m]++;
|
||||
} while (next[M] !== n);
|
||||
return v;
|
||||
}
|
||||
var args = process.argv;
|
||||
console.log(ack(parseInt(args[2]), parseInt(args[3])));
|
||||
38
Task/Ackermann-function/JavaScript/ackermann-function-5.js
Normal file
38
Task/Ackermann-function/JavaScript/ackermann-function-5.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// ackermann :: Int -> Int -> Int
|
||||
const ackermann = m => n => {
|
||||
const go = (m, n) =>
|
||||
0 === m ? (
|
||||
succ(n)
|
||||
) : go(pred(m), 0 === n ? (
|
||||
1
|
||||
) : go(m, pred(n)));
|
||||
return go(m, n);
|
||||
};
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
const main = () => console.log(JSON.stringify(
|
||||
[0, 1, 2, 3].map(
|
||||
flip(ackermann)(3)
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
// GENERAL FUNCTIONS ----------------------------------
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f =>
|
||||
x => y => f(y)(x);
|
||||
|
||||
// pred :: Enum a => a -> a
|
||||
const pred = x => x - 1;
|
||||
|
||||
// succ :: Enum a => a -> a
|
||||
const succ = x => 1 + x;
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
38
Task/Ackermann-function/Lua/ackermann-function-2.lua
Normal file
38
Task/Ackermann-function/Lua/ackermann-function-2.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env luajit
|
||||
local gmp = require 'gmp' ('libgmp')
|
||||
local mpz, z_mul, z_add, z_add_ui, z_set_d =
|
||||
gmp.types.z, gmp.z_mul, gmp.z_add, gmp.z_add_ui, gmp.z_set_d
|
||||
local z_cmp, z_cmp_ui, z_init_d, z_set=
|
||||
gmp.z_cmp, gmp.z_cmp_ui, gmp.z_init_set_d, gmp.z_set
|
||||
local printf = gmp.printf
|
||||
|
||||
local function ack(i,n)
|
||||
local nxt=setmetatable({}, {__index=function(t,k) local z=mpz() z_init_d(z, 0) t[k]=z return z end})
|
||||
local goal=setmetatable({}, {__index=function(t,k) local o=mpz() z_init_d(o, 1) t[k]=o return o end})
|
||||
goal[i]=mpz() z_init_d(goal[i], -1)
|
||||
local v=mpz() z_init_d(v, 0)
|
||||
local ic
|
||||
local END=n+1
|
||||
local ntmp,gtmp
|
||||
repeat
|
||||
ic=0
|
||||
ntmp,gtmp=nxt[ic], goal[ic]
|
||||
z_add_ui(v, ntmp, 1)
|
||||
while z_cmp(ntmp, gtmp) == 0 do
|
||||
z_set(gtmp,v)
|
||||
z_add_ui(ntmp, ntmp, 1)
|
||||
nxt[ic], goal[ic]=ntmp, gtmp
|
||||
ic=ic+1
|
||||
ntmp,gtmp=nxt[ic], goal[ic]
|
||||
end
|
||||
z_add_ui(ntmp, ntmp, 1)
|
||||
nxt[ic]=ntmp
|
||||
until z_cmp_ui(nxt[i], END) == 0
|
||||
return v
|
||||
end
|
||||
|
||||
if #arg<1 then
|
||||
print("Ackermann: "..arg[0].." <num1> [num2]")
|
||||
else
|
||||
printf("%Zd\n", ack(tonumber(arg[1]), arg[2] and tonumber(arg[2]) or 0))
|
||||
end
|
||||
|
|
@ -1,9 +1,19 @@
|
|||
int ackermann(int m, n)
|
||||
{
|
||||
if (m == 0)
|
||||
return n + 1;
|
||||
else if (m > 0 && n == 0)
|
||||
return ackermann(m - 1, 1);
|
||||
else
|
||||
return ackermann( m - 1, ackermann(m, n - 1) );
|
||||
int ackermann(int m, int n) {
|
||||
if (m == 0)
|
||||
return n + 1;
|
||||
else if (m > 0 && n == 0)
|
||||
return ackermann(m - 1, 1);
|
||||
else
|
||||
return ackermann( m - 1, ackermann(m, n - 1) );
|
||||
}
|
||||
|
||||
// Call function to produce output:
|
||||
// the first 4x7 Ackermann numbers
|
||||
void setup() {
|
||||
for (int m=0; m<4; m++) {
|
||||
for (int n=0; n<7; n++) {
|
||||
print(ackermann(m, n), " ");
|
||||
}
|
||||
println();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
Task/Ackermann-function/Vala/ackermann-function.vala
Normal file
13
Task/Ackermann-function/Vala/ackermann-function.vala
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
uint64 ackermann(uint64 m, uint64 n) {
|
||||
if (m == 0) return n + 1;
|
||||
if (n == 0) return ackermann(m - 1, 1);
|
||||
return ackermann(m - 1, ackermann(m, n - 1));
|
||||
}
|
||||
|
||||
void main () {
|
||||
for (uint64 m = 0; m < 4; ++m) {
|
||||
for (uint64 n = 0; n < 10; ++n) {
|
||||
print(@"A($m,$n) = $(ackermann(m,n))\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Task/Ackermann-function/X86-Assembly/ackermann-function.x86
Normal file
29
Task/Ackermann-function/X86-Assembly/ackermann-function.x86
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
section .text
|
||||
|
||||
global _main
|
||||
_main:
|
||||
mov eax, 3 ;m
|
||||
mov ebx, 4 ;n
|
||||
call ack ;returns number in ebx
|
||||
ret
|
||||
|
||||
ack:
|
||||
cmp eax, 0
|
||||
je M0 ;if M == 0
|
||||
cmp ebx, 0
|
||||
je N0 ;if N == 0
|
||||
dec ebx ;else N-1
|
||||
push eax ;save M
|
||||
call ack1 ;ack(m,n) -> returned in ebx so no further instructions needed
|
||||
pop eax ;restore M
|
||||
dec eax ;M - 1
|
||||
call ack1 ;return ack(m-1,ack(m,n-1))
|
||||
ret
|
||||
M0:
|
||||
inc ebx ;return n + 1
|
||||
ret
|
||||
N0:
|
||||
dec eax
|
||||
inc ebx ;ebx always 0: inc -> ebx = 1
|
||||
call ack1 ;return ack(M-1,1)
|
||||
ret
|
||||
Loading…
Add table
Add a link
Reference in a new issue