Anonymous user
Ramsey's theorem: Difference between revisions
Updated and hopefully fixed the D entry
(→{{header|C}}: add checking) |
(Updated and hopefully fixed the D entry) |
||
Line 89:
=={{header|D}}==
<lang d>import std.stdio, std.string, std.algorithm, std.range;
▲{{trans|C}}
/// Generate the connectivity matrix.
void main() {▼
immutable(char)[][] generateMatrix() {
return r.length.iota.map!(i => r[$-i .. $] ~ r[0 .. $-i]).array;
}
/**Check that every clique of four has at least one pair connected
foreach (immutable i, ref row; mat)▼
and one pair unconnected. It requires a symmetric matrix.*/
row[i] = '-';▼
string ramseyCheck(in char[][] mat) pure @safe
in {
assert(row.length == mat.length);
foreach (immutable c, immutable x; row)
assert(x == mat[c][r]);
}
} body {
immutable N = mat.length;
char[6] connectivity = '-';
foreach (immutable
foreach (immutable
foreach (immutable c; 0 .. N) {
if (a == c || b == c) continue;
connectivity[1] = mat[a][c];
connectivity[2] = mat[b][c];
foreach (immutable d; 0 .. N) {
if (a == d || b == d || c == d) continue;
connectivity[3] = mat[a][d];
connectivity[4] = mat[b][d];
connectivity[5] = mat[c][d];
// We've extracted a meaningful subgraph,
// check its connectivity.
if (!connectivity[].canFind('0'))
return format("Fail, found wholly connected: ",
a, " ", b," ", c, " ", d);
else if (!connectivity[].canFind('1'))
return format("Fail, found wholly " ~
"unconnected: ",
a, " ", b," ", c, " ", d);
}
}
}
return "Satisfies Ramsey condition.";
writefln("%(%(%c %)\n%)", mat);▼
}
▲void main() {
const mat = generateMatrix;
▲ writefln("%-(%(%c %)\n%)", mat);
mat.ramseyCheck.writeln;
}</lang>
{{out}}
Line 126 ⟶ 164:
0 1 0 0 0 1 1 0 0 0 1 0 1 1 - 1 1
1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 - 1
1 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 -
Satisfies Ramsey condition.</pre>
=={{header|Erlang}}==
|