tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,37 @@
|
|||
import std.stdio, std.algorithm, std.conv;
|
||||
|
||||
string[] selfReferentialSeq(string n, string[] seen=[]) {
|
||||
static string[][string] cache;
|
||||
if (n in cache) return cache[n];
|
||||
if (canFind(seen, n)) return [];
|
||||
|
||||
int[10] digit_count;
|
||||
foreach (d; n)
|
||||
digit_count[d - '0']++;
|
||||
string term;
|
||||
foreach_reverse (d; 0 .. 10)
|
||||
if (digit_count[d] > 0)
|
||||
term ~= text(digit_count[d], d);
|
||||
return cache[n] = [n] ~ selfReferentialSeq(term, [n] ~ seen);
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum int limit = 1_000_000;
|
||||
int max_len;
|
||||
int[] max_vals;
|
||||
|
||||
foreach (n; 1 .. limit) {
|
||||
const seq = n.text().selfReferentialSeq();
|
||||
if (seq.length > max_len) {
|
||||
max_len = seq.length;
|
||||
max_vals = [n];
|
||||
} else if (seq.length == max_len)
|
||||
max_vals ~= n;
|
||||
}
|
||||
|
||||
writeln("values: ", max_vals);
|
||||
writeln("iterations: ", max_len);
|
||||
writeln("sequence:");
|
||||
foreach (idx, val; max_vals[0].text().selfReferentialSeq())
|
||||
writefln("%2d %s", idx + 1, val);
|
||||
}
|
||||
176
Task/Self-referential-sequence/D/self-referential-sequence-2.d
Normal file
176
Task/Self-referential-sequence/D/self-referential-sequence-2.d
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
import std.range, std.algorithm;
|
||||
|
||||
struct Permutations(bool doCopy=true, T) {
|
||||
T[] items;
|
||||
int r;
|
||||
bool stopped;
|
||||
int[] indices, cycles;
|
||||
static if (!doCopy)
|
||||
T[] result;
|
||||
|
||||
this(T)(T[] items, int r=-1) /*pure nothrow*/ {
|
||||
this.items = items;
|
||||
immutable int n = items.length;
|
||||
if (r < 0)
|
||||
r = n;
|
||||
this.r = r;
|
||||
immutable n_minus_r = n - r;
|
||||
if (n_minus_r < 0) {
|
||||
this.stopped = true;
|
||||
} else {
|
||||
this.stopped = false;
|
||||
this.indices = iota(n).array(); // not pure nothrow
|
||||
this.cycles = iota(n, n_minus_r, -1).array();
|
||||
}
|
||||
|
||||
static if (!doCopy)
|
||||
result = new T[r];
|
||||
}
|
||||
|
||||
@property bool empty() const pure nothrow {
|
||||
return this.stopped;
|
||||
}
|
||||
|
||||
static if (doCopy) {
|
||||
@property T[] front() const pure nothrow {
|
||||
assert(!this.stopped);
|
||||
auto result = new T[r];
|
||||
foreach (i, ref re; result)
|
||||
re = items[indices[i]];
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
@property T[] front() pure nothrow {
|
||||
assert(!this.stopped);
|
||||
foreach (i, ref re; this.result)
|
||||
re = items[indices[i]];
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
||||
void popFront() pure nothrow {
|
||||
assert(!this.stopped);
|
||||
int i = r - 1;
|
||||
while (i >= 0) {
|
||||
immutable int j = cycles[i] - 1;
|
||||
if (j > 0) {
|
||||
cycles[i] = j;
|
||||
swap(indices[i], indices[$ - j]);
|
||||
return;
|
||||
}
|
||||
cycles[i] = indices.length - i;
|
||||
immutable int n1 = indices.length - 1;
|
||||
assert(n1 >= 0);
|
||||
immutable int num = indices[i];
|
||||
foreach (k; i .. n1)
|
||||
indices[k] = indices[k + 1];
|
||||
indices[n1] = num;
|
||||
i--;
|
||||
}
|
||||
|
||||
this.stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
Permutations!(doCopy, T) permutations(bool doCopy=true, T)
|
||||
(T[] items, int r=-1)
|
||||
/*pure nothrow*/ {
|
||||
return Permutations!(doCopy, T)(items, r);
|
||||
}
|
||||
|
||||
// ---------------------------------
|
||||
|
||||
import std.stdio, std.typecons, std.conv, std.algorithm, std.array;
|
||||
|
||||
enum maxIters = 1_000_000;
|
||||
|
||||
string A036058(string ns) {
|
||||
return group(ns).map!(t => text(t[1]) ~ cast(char)t[0])().join();
|
||||
}
|
||||
|
||||
int A036058_length(bool doPrint=false)(string numberString="0") {
|
||||
int iterations = 1;
|
||||
int queue_index;
|
||||
string[3] last_three;
|
||||
|
||||
while (true) {
|
||||
static if (doPrint)
|
||||
writefln(" %2d %s", iterations, numberString);
|
||||
|
||||
//numberString = cast(string)(cast(ubyte[])numberString.dup).sort().release();
|
||||
// this is a workaround --------
|
||||
int[10] digitsCounts;
|
||||
foreach (char digit; numberString)
|
||||
digitsCounts[digit - '0']++;
|
||||
auto numb = new char[numberString.length];
|
||||
int count = 0;
|
||||
foreach (i, d; digitsCounts)
|
||||
foreach (n; 0 .. d) {
|
||||
numb[count] = cast(char)(i + '0');
|
||||
count++;
|
||||
}
|
||||
numberString = cast(string)numb;
|
||||
// end work-around --------
|
||||
|
||||
if (last_three[].canFind(numberString))
|
||||
break;
|
||||
assert(iterations < maxIters);
|
||||
last_three[queue_index] = numberString;
|
||||
numberString = A036058(numberString);
|
||||
iterations++;
|
||||
queue_index++;
|
||||
queue_index %= 3;
|
||||
}
|
||||
|
||||
return iterations;
|
||||
}
|
||||
|
||||
Tuple!(int,int[]) max_A036058_length(R)(R start_range=iota(11)) {
|
||||
bool[string] already_done;
|
||||
auto max_len = tuple(-1, (int[]).init);
|
||||
|
||||
foreach (n; start_range) {
|
||||
string sns = cast(string)(cast(ubyte[])to!(char[])(n)).sort().release();
|
||||
if (sns !in already_done) {
|
||||
already_done[sns] = true;
|
||||
int size = A036058_length(sns);
|
||||
if (size > max_len[0])
|
||||
max_len = tuple(size, [n]);
|
||||
else if (size == max_len[0])
|
||||
max_len[1] ~= n;
|
||||
}
|
||||
}
|
||||
return max_len;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto lenMax_starts = max_A036058_length(iota(maxIters));
|
||||
int lenMax = lenMax_starts[0];
|
||||
int[] starts = lenMax_starts[1];
|
||||
|
||||
// Expand
|
||||
int[] allStarts;
|
||||
foreach (n; starts) {
|
||||
bool[string] set;
|
||||
foreach (k; permutations!false(to!(char[])(n), 4))
|
||||
if (k[0] != '0')
|
||||
set[k.idup] = true;
|
||||
allStarts ~= set.byKey().map!(to!int)().array();
|
||||
}
|
||||
|
||||
allStarts = allStarts.sort().release().filter!(x => x < maxIters)().array();
|
||||
|
||||
writefln("The longest length, followed by the number(s) with the
|
||||
longest sequence length for starting sequence numbers below maxIters
|
||||
are:
|
||||
Iterations = %d and sequence-starts = %s.", lenMax, allStarts);
|
||||
|
||||
writeln("Note that only the first of any sequences with the same
|
||||
digits is printed below. (The others will differ only in their first
|
||||
term).");
|
||||
|
||||
foreach (n; starts) {
|
||||
writeln();
|
||||
A036058_length!true(to!string(n));
|
||||
}
|
||||
}
|
||||
139
Task/Self-referential-sequence/D/self-referential-sequence-3.d
Normal file
139
Task/Self-referential-sequence/D/self-referential-sequence-3.d
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
import core.stdc.stdio, core.stdc.stdlib;
|
||||
|
||||
struct MemoryPool(T, int MAX_BLOCK_BYTES=1 << 17) {
|
||||
static assert(!is(T == class),
|
||||
"MemoryPool is designed for native data.");
|
||||
static assert(MAX_BLOCK_BYTES >= 1,
|
||||
"MemoryPool: MAX_BLOCK_BYTES must be >= 1 bytes.");
|
||||
|
||||
static struct Block {
|
||||
static assert(MAX_BLOCK_BYTES >= T.sizeof,
|
||||
"MemoryPool: MAX_BLOCK_BYTES must be" ~
|
||||
" bigger than a T.");
|
||||
static if ((T.sizeof * 5) > MAX_BLOCK_BYTES)
|
||||
pragma(msg, "MemoryPool: Block is very small.");
|
||||
|
||||
T[(MAX_BLOCK_BYTES / T.sizeof)] items;
|
||||
}
|
||||
|
||||
__gshared static Block*[] blocks;
|
||||
|
||||
__gshared static T* nextFree, lastFree;
|
||||
|
||||
static T* newItem() nothrow {
|
||||
if (nextFree >= lastFree) {
|
||||
blocks ~= cast(Block*)calloc(1, Block.sizeof);
|
||||
if (blocks[$ - 1] == null)
|
||||
exit(1);
|
||||
nextFree = blocks[$ - 1].items.ptr;
|
||||
lastFree = nextFree + Block.items.length;
|
||||
}
|
||||
|
||||
return nextFree++;
|
||||
}
|
||||
|
||||
static void freeAll() nothrow {
|
||||
foreach (block_ptr; blocks)
|
||||
free(block_ptr);
|
||||
blocks.length = 0;
|
||||
nextFree = null;
|
||||
lastFree = null;
|
||||
}
|
||||
}
|
||||
|
||||
struct Rec { // Tree node
|
||||
int length;
|
||||
Rec*[10] p;
|
||||
}
|
||||
|
||||
__gshared int nNodes;
|
||||
__gshared Rec* rec_root;
|
||||
__gshared MemoryPool!Rec recPool;
|
||||
|
||||
Rec* findRec(char* s, Rec* root) nothrow {
|
||||
int c;
|
||||
Rec* next;
|
||||
|
||||
while (true) {
|
||||
c = *s;
|
||||
s++;
|
||||
if (!c)
|
||||
break;
|
||||
c -= '0';
|
||||
next = root.p[c];
|
||||
if (!next) {
|
||||
nNodes++;
|
||||
next = recPool.newItem();
|
||||
root.p[c] = next;
|
||||
}
|
||||
root = next;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void nextNum(char* s) nothrow {
|
||||
int[10] cnt;
|
||||
for (int i = 0; s[i]; i++)
|
||||
cnt[s[i] - '0']++;
|
||||
|
||||
foreach_reverse (i; 0 .. 10) {
|
||||
if (!cnt[i])
|
||||
continue;
|
||||
s += sprintf(s, "%d%c", cnt[i], i + '0');
|
||||
}
|
||||
}
|
||||
|
||||
int getLen(char* s, int depth) nothrow {
|
||||
auto r = findRec(s, rec_root);
|
||||
if (r.length > 0)
|
||||
return r.length;
|
||||
|
||||
depth++;
|
||||
if (!r.length)
|
||||
r.length = -depth;
|
||||
else
|
||||
r.length += depth;
|
||||
|
||||
nextNum(s);
|
||||
depth = 1 + getLen(s, depth);
|
||||
|
||||
if (r.length <= 0)
|
||||
r.length = depth;
|
||||
return r.length;
|
||||
}
|
||||
|
||||
void main() nothrow {
|
||||
enum MAXN = 1_000_000;
|
||||
|
||||
int[100] longest;
|
||||
int nLongest, ml;
|
||||
char[32] buf;
|
||||
rec_root = recPool.newItem();
|
||||
|
||||
foreach (i; 0 .. MAXN) {
|
||||
sprintf(buf.ptr, "%d", i);
|
||||
int l = getLen(buf.ptr, 0);
|
||||
if (l < ml)
|
||||
continue;
|
||||
if (l > ml) {
|
||||
nLongest = 0;
|
||||
ml = l;
|
||||
}
|
||||
longest[nLongest] = i;
|
||||
nLongest++;
|
||||
}
|
||||
|
||||
printf("seq leng: %d\n\n", ml);
|
||||
foreach (i; 0 .. nLongest) {
|
||||
sprintf(buf.ptr, "%d", longest[i]);
|
||||
// print len+1 so we know repeating starts from when
|
||||
foreach (l; 0 .. ml + 1) {
|
||||
printf("%2d: %s\n", getLen(buf.ptr, 0), buf.ptr);
|
||||
nextNum(buf.ptr);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("Allocated %d Rec tree nodes.\n", nNodes);
|
||||
//recPool.freeAll();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue