Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -34,7 +34,7 @@ int main()
memset(cache, 0, sizeof(int) * (1 << (m_bits + n_bits)));
for (m = 0; m <= 4; m++)
for (n = 0; n < 6 - m; n++) {
for (n = 0; n < 6 - m; n++)
printf("A(%d, %d) = %d\n", m, n, ackermann(m, n));
return 0;

View file

@ -1,9 +1,8 @@
import std.stdio, std.bigint, std.conv;
/*pure nothrow*/ BigInt ipow(/*in*/ BigInt base, /*in*/ BigInt exp){
auto result = BigInt(1);
//while (exp) {
while (exp != 0) {
BigInt ipow(BigInt base, BigInt exp) pure /*nothrow*/ {
auto result = 1.BigInt;
while (exp) {
//if (exp & 1)
if (exp % 2)
result *= base;
@ -14,37 +13,36 @@ import std.stdio, std.bigint, std.conv;
return result;
}
/*pure nothrow*/ BigInt ackermann(in int m, in int n)
BigInt ackermann(in int m, in int n) pure /*nothrow*/
in {
assert(m >= 0 && n >= 0);
} out(result) {
//assert(result >= 0);
//assert(cast()result >= 0);
assert(result >= 0);
} body {
/*pure nothrow*/ static BigInt ack(in int m, /*in*/ BigInt n) {
static BigInt ack(in int m, in BigInt n) pure /*nothrow*/ {
switch (m) {
case 0: return n + 1;
case 1: return n + 2;
case 2: return 3 + 2 * n;
//case 3: return 5 + 8 * (2 ^^ n - 1);
case 3: return 5 + 8 * (ipow(BigInt(2), n) - 1);
case 3: return 5 + 8 * (ipow(2.BigInt, n) - 1);
default: if (n == 0)
return ack(m - 1, BigInt(1));
return ack(m - 1, 1.BigInt);
else
return ack(m - 1, ack(m, n - 1));
}
}
return ack(m, BigInt(n));
return ack(m, n.BigInt);
}
void main() {
foreach (m; 1 .. 4)
foreach (n; 1 .. 9)
foreach (immutable m; 1 .. 4)
foreach (immutable n; 1 .. 9)
writefln("ackermann(%d, %d): %s", m, n, ackermann(m, n));
writefln("ackermann(4, 1): %s", ackermann(4, 1));
auto a = text(ackermann(4, 2));
immutable a = ackermann(4, 2).text;
writefln("ackermann(4, 2)) (%d digits):\n%s...\n%s",
a.length, a[0 .. 94], a[$-96 .. $]);
a.length, a[0 .. 94], a[$ - 96 .. $]);
}

View file

@ -2,23 +2,21 @@
// --- Ackermann function ---
#symbol ackermann = &&:m:n
#symbol ackermann = (:m:n)
[
m =>
0 ? [ n + 1 ]
> 0 ? [
n => 0 ? [ $self:(m - 1):1 ]
> 0 ? [ $self:(m - 1):($self:m:(n-1)) ]
n => 0 ? [ ackermann:(m - 1):1 ]
> 0 ? [ ackermann:(m - 1):(ackermann:m:(n-1)) ]
]
].
#symbol program =
[
#var n := ackermann:3:5.
control from:0 &to:3 &do: &&:i
control from:0 &to:3 &do: i
[
control from:0 &to:5 &do: &&:j
control from:0 &to:5 &do: j
[
console << "A(" << i << "," << j << ")=" << (ackermann:i:j).

View file

@ -0,0 +1,9 @@
function ackermann(%m,%n)
{
if(%m==0)
return %n+1;
if(%m>0&&%n==0)
return ackermann(%m-1,1);
if(%m>0&&%n>0)
return ackermann(%m-1,ackermann(%m,%n-1));
}