Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,65 @@
import std.stdio, std.algorithm, std.typecons, std.conv,
std.range, std.traits;
T factorial(T)(in T n) pure nothrow @safe @nogc {
Unqual!T result = 1;
foreach (immutable i; 2 .. n + 1)
result *= i;
return result;
}
T subfact(T)(in T n) pure nothrow @safe @nogc {
if (0 <= n && n <= 2)
return n != 1;
return (n - 1) * (subfact(n - 1) + subfact(n - 2));
}
auto derangements(in size_t n, in bool countOnly=false)
pure nothrow @safe {
size_t[] seq = n.iota.array;
auto ori = seq.idup;
size_t[][] all;
size_t cnt = n == 0;
foreach (immutable tot; 0 .. n.factorial - 1) {
size_t j = n - 2;
while (seq[j] > seq[j + 1])
j--;
size_t k = n - 1;
while (seq[j] > seq[k])
k--;
seq[k].swap(seq[j]);
size_t r = n - 1;
size_t s = j + 1;
while (r > s) {
seq[s].swap(seq[r]);
r--;
s++;
}
j = 0;
while (j < n && seq[j] != ori[j])
j++;
if (j == n) {
if (countOnly)
cnt++;
else
all ~= seq.dup;
}
}
return tuple(all, cnt);
}
void main() @safe {
"Derangements for n = 4:".writeln;
foreach (const d; 4.derangements[0])
d.writeln;
"\nTable of n vs counted vs calculated derangements:".writeln;
foreach (immutable i; 0 .. 10)
writefln("%s %-7s%-7s", i, derangements(i, 1)[1], i.subfact);
writefln("\n!20 = %s", 20L.subfact);
}

View file

@ -0,0 +1,47 @@
import std.stdio, std.algorithm, std.typecons, std.conv, std.range;
T factorial(T)(in T n) pure nothrow {
Unqual!T result = 1;
foreach (immutable i; 2 .. n + 1)
result *= i;
return result;
}
T subfact(T)(in T n) pure nothrow {
if (0 <= n && n <= 2)
return n != 1;
return (n - 1) * (subfact(n - 1) + subfact(n - 2));
}
auto derangementsR(in size_t n, in bool countOnly=false) pure
/*nothrow*/ {
auto seq = n.iota.array;
immutable ori = seq.idup;
const(size_t[])[] res;
size_t cnt;
void perms(in size_t[] s, in size_t[] pre=null) /*nothrow*/ {
if (s.length) {
foreach (immutable i, immutable c; s)
perms(s[0 .. i] ~ s[i + 1 .. $], pre ~ c);
} else if (zip(pre, ori).all!(po => po[0] != po[1])) {
if (countOnly) cnt++;
else res ~= pre;
}
}
perms(seq);
return tuple(res, cnt);
}
void main() {
"Derangements for n = 4:".writeln;
foreach (const d; 4.derangementsR[0])
d.writeln;
"\nTable of n vs counted vs calculated derangements:".writeln;
foreach (immutable i; 0 .. 10)
writefln("%s %-7s%-7s", i, derangementsR(i, 1)[1], i.subfact);
writefln("\n!20 = %s", 20L.subfact);
}