all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,76 @@
import std.stdio, std.algorithm, std.range, std.array;
immutable(long[2])[] vampireNumberFactors(in long n) {
static typeof(return) factorPairs(in long k) pure nothrow {
typeof(return) pairs;
foreach (immutable i; 2 .. cast(long)(k ^^ 0.5 + 1))
if (k % i == 0) {
immutable q = k / i;
if (q > i)
pairs ~= [i, q]; // Heap-allocated pair.
}
return pairs;
}
static long[] getDigits(in long k) pure nothrow {
typeof(return) digits;
long m = k;
while (m > 0) {
digits ~= (m % 10);
m /= 10;
}
digits.reverse();
return digits;
}
if (n < 2)
return null;
auto digits = getDigits(n);
if (digits.length % 2 != 0)
return null;
digits.sort();
typeof(return) result;
immutable half = digits.length / 2;
foreach (immutable pair; factorPairs(n)) {
/*immutable*/ auto f1 = getDigits(pair.front);
if (f1.length != half)
continue;
immutable f2 = getDigits(pair.back);
if (f2.length != half)
continue;
if (f1.back == 0 && f2.back == 0)
continue;
if (!(f1 ~ f2).sort().equal(digits))
continue;
result ~= pair;
}
return result;
}
void main() {
for (long count = 0, n = 0; count < 25; n++) {
immutable factors = vampireNumberFactors(n);
if (!factors.empty) {
writefln("%s : %(%s %)", n, factors);
count++;
}
}
writeln;
foreach (immutable n; [16_758_243_290_880L,
24_959_017_348_650L,
14_593_825_548_650L]) {
immutable factors = vampireNumberFactors(n);
if (!factors.empty)
writefln("%s: %(%s %)", n, vampireNumberFactors(n));
}
}

View file

@ -0,0 +1,92 @@
import std.stdio, std.math, std.algorithm, std.array, std.traits;
T[N] pows(T, size_t N)() {
typeof(return) result;
result[0] = 1;
foreach (i, ref r; result[1 .. $])
r = result[i] * 10;
return result;
}
__gshared immutable tenPowsU = pows!(uint, 10);
__gshared immutable tenPowsUL = pows!(ulong, 20);
size_t nDigits(T)(in T x) pure nothrow {
Unqual!T y = x;
size_t n = 0;
while (y) {
n++;
y /= 10;
}
return n;
}
T dTally(T)(in T x) pure nothrow {
Unqual!T y = x;
T t = 0;
while (y) {
t += 1 << ((y % 10) * 6);
y /= 10;
}
return t;
}
T[] fangs(T)(in T x, T[] f)
pure nothrow if (is(T == uint) || is(T == ulong)) {
alias tenPows = Select!(is(T == ulong), tenPowsUL, tenPowsU);
immutable nd0 = nDigits(x);
if (nd0 & 1)
return null;
immutable nd = nd0 / 2;
immutable lo = max(tenPows[nd - 1],
(x + tenPows[nd] - 2) / (tenPows[nd] - 1));
immutable hi = min(x / lo, cast(T)sqrt(cast(real)x));
immutable t = x.dTally;
size_t n = 0;
foreach (immutable a; lo .. hi + 1) {
immutable b = x / a;
if (a * b == x
&& (a % 10 || b % 10)
&& t == (a.dTally + b.dTally)) {
f[n] = a;
n++;
}
}
return f[0 .. n];
}
void showFangs(T)(in T x, in T[] fs) {
x.write;
foreach (immutable fi; fs)
writef(" = %d x %d", fi, x / fi);
writeln;
}
void main() {
uint[16] fu;
for (uint x = 1, n = 0; n < 25; x++) {
const fs = fangs(x, fu);
if (fs.empty)
continue;
n++;
writef("%2d: ", n);
showFangs(x, fs);
}
writeln;
__gshared static immutable ulong[3] bigs = [16_758_243_290_880UL,
24_959_017_348_650UL,
14_593_825_548_650UL];
ulong[fu.length] ful;
foreach (immutable bi; bigs) {
const fs = fangs(bi, ful);
if (fs.empty)
writeln(bi, " is not vampiric");
else
showFangs(bi, fs);
}
}