Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Taxicab-numbers/D/taxicab-numbers-1.d
Normal file
19
Task/Taxicab-numbers/D/taxicab-numbers-1.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
void main() /*@safe*/ {
|
||||
import std.stdio, std.range, std.algorithm, std.typecons, std.string;
|
||||
|
||||
auto iCubes = iota(1u, 1201u).map!(x => tuple(x, x ^^ 3));
|
||||
bool[Tuple!(uint, uint)][uint] sum2cubes;
|
||||
foreach (i, immutable i3; iCubes)
|
||||
foreach (j, immutable j3; iCubes[i .. $])
|
||||
sum2cubes[i3 + j3][tuple(i, j)] = true;
|
||||
|
||||
const taxis = sum2cubes.byKeyValue.filter!(p => p.value.length > 1)
|
||||
.array.schwartzSort!(p => p.key).release;
|
||||
|
||||
foreach (/*immutable*/ const r; [[0, 25], [2000 - 1, 2000 + 6]]) {
|
||||
foreach (immutable i, const t; taxis[r[0] .. r[1]])
|
||||
writefln("%4d: %10d =%-(%s =%)", i + r[0] + 1, t.key,
|
||||
t.value.keys.sort().map!q{"%4d^3 + %4d^3".format(a[])});
|
||||
writeln;
|
||||
}
|
||||
}
|
||||
65
Task/Taxicab-numbers/D/taxicab-numbers-2.d
Normal file
65
Task/Taxicab-numbers/D/taxicab-numbers-2.d
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import std.stdio, std.string, std.container;
|
||||
|
||||
struct CubeSum {
|
||||
ulong x, y, value;
|
||||
|
||||
this(in ulong x_, in ulong y_) pure nothrow @safe @nogc {
|
||||
this.x = x_;
|
||||
this.y = y_;
|
||||
this.value = x_ ^^ 3 + y_ ^^ 3;
|
||||
}
|
||||
}
|
||||
|
||||
final class Taxi {
|
||||
BinaryHeap!(Array!CubeSum, "a.value > b.value") pq;
|
||||
CubeSum last;
|
||||
ulong n = 0;
|
||||
|
||||
this() {
|
||||
last = nextSum();
|
||||
}
|
||||
|
||||
CubeSum nextSum() {
|
||||
while (pq.empty || pq.front.value >= n ^^ 3)
|
||||
pq.insert(CubeSum(++n, 1));
|
||||
|
||||
auto s = pq.front;
|
||||
pq.removeFront;
|
||||
if (s.x > s.y + 1)
|
||||
pq.insert(CubeSum(s.x, s.y + 1));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
CubeSum[] nextTaxi() {
|
||||
CubeSum s;
|
||||
typeof(return) train;
|
||||
|
||||
while ((s = nextSum).value != last.value)
|
||||
last = s;
|
||||
|
||||
train ~= last;
|
||||
|
||||
do {
|
||||
train ~= s;
|
||||
} while ((s = nextSum).value == last.value);
|
||||
last = s;
|
||||
|
||||
return train;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto taxi = new Taxi;
|
||||
|
||||
foreach (immutable i; 1 .. 2007) {
|
||||
const t = taxi.nextTaxi;
|
||||
if (i > 25 && i < 2000)
|
||||
continue;
|
||||
|
||||
writef("%4d: %10d", i, t[0].value);
|
||||
foreach (const s; t)
|
||||
writef(" = %4d^3 + %4d^3", s.x, s.y);
|
||||
writeln;
|
||||
}
|
||||
}
|
||||
101
Task/Taxicab-numbers/D/taxicab-numbers-3.d
Normal file
101
Task/Taxicab-numbers/D/taxicab-numbers-3.d
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
struct Taxicabs {
|
||||
alias CubesSumT = uint; // Or ulong.
|
||||
|
||||
static struct Sum {
|
||||
CubesSumT value;
|
||||
uint x, y;
|
||||
}
|
||||
|
||||
// The cubes can be pre-computed if CubesSumT is a BigInt.
|
||||
private uint nCubes;
|
||||
private Sum[] pq;
|
||||
private uint pq_len;
|
||||
|
||||
private void addCube() pure nothrow @safe {
|
||||
nCubes = nCubes ? nCubes + 1 : 2;
|
||||
if (nCubes < 2)
|
||||
return; // 0 or 1 is useless.
|
||||
|
||||
pq_len++;
|
||||
if (pq_len >= pq.length)
|
||||
pq.length = (pq.length == 0) ? 2 : (pq.length * 2);
|
||||
|
||||
immutable tmp = Sum(CubesSumT(nCubes - 2) ^^ 3 + 1,
|
||||
nCubes - 2, 1);
|
||||
|
||||
// Upheap.
|
||||
uint i = pq_len;
|
||||
for (; i >= 1 && pq[i >> 1].value > tmp.value; i >>= 1)
|
||||
pq[i] = pq[i >> 1];
|
||||
|
||||
pq[i] = tmp;
|
||||
}
|
||||
|
||||
|
||||
private void nextSum() pure nothrow @safe {
|
||||
while (!pq_len || pq[1].value >= (nCubes - 1) ^^ 3)
|
||||
addCube();
|
||||
|
||||
Sum tmp = pq[0] = pq[1]; //pq[0] always stores last seen value.
|
||||
tmp.y++;
|
||||
if (tmp.y >= tmp.x) { // Done with this x; throw it away.
|
||||
tmp = pq[pq_len];
|
||||
pq_len--;
|
||||
if (!pq_len)
|
||||
return nextSum(); // Refill empty heap.
|
||||
} else
|
||||
tmp.value += tmp.y ^^ 3 - (tmp.y - 1) ^^ 3;
|
||||
|
||||
// Downheap.
|
||||
uint i = 1;
|
||||
while (true) {
|
||||
uint j = i << 1;
|
||||
if (j > pq_len)
|
||||
break;
|
||||
if (j < pq_len && pq[j + 1].value < pq[j].value)
|
||||
j++;
|
||||
if (pq[j].value >= tmp.value)
|
||||
break;
|
||||
pq[i] = pq[j];
|
||||
i = j;
|
||||
}
|
||||
|
||||
pq[i] = tmp;
|
||||
}
|
||||
|
||||
|
||||
Sum[] nextTaxi(size_t N)(ref Sum[N] hist)
|
||||
pure nothrow @safe {
|
||||
do {
|
||||
nextSum();
|
||||
} while (pq[0].value != pq[1].value);
|
||||
|
||||
uint len = 1;
|
||||
hist[0] = pq[0];
|
||||
do {
|
||||
hist[len] = pq[1];
|
||||
len++;
|
||||
nextSum();
|
||||
} while (pq[0].value == pq[1].value);
|
||||
|
||||
return hist[0 .. len];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void main() nothrow {
|
||||
import core.stdc.stdio;
|
||||
|
||||
Taxicabs t;
|
||||
Taxicabs.Sum[3] x;
|
||||
|
||||
foreach (immutable uint i; 1 .. 2007) {
|
||||
const triples = t.nextTaxi(x);
|
||||
if (i > 25 && i < 2000)
|
||||
continue;
|
||||
printf("%4u: %10lu", i, triples[0].value);
|
||||
foreach_reverse (const s; triples)
|
||||
printf(" = %4u^3 + %4u^3", s.x, s.y);
|
||||
'\n'.putchar;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue