/* rl31.c -- job #2709 / route 67: loose and refined run statistics on the twin-slot tile T31, streamed. * * T_x = { r in [0,P_x) : gcd(r,P_x) = gcd(r+2,P_x) = 1 }, P_x = prod_{q<=x} q. Cyclic gap word g_0..g_{D-1} * (the last gap wraps: P - r_last + r_first), i.e. the true periodic continuation. * For each prime q in [QLO, QHI] every gap gets a code: g mod q == 0 -> 0, == 2 -> 1, == q-2 -> 2, else 3 (other). * R_loose(q): longest cyclic run of consecutive gaps with code != 3 (#1244 / #971 definition). * refined spectrum: components = maximal runs of consecutive slots whose residues mod q lie in one 2-set {a,a+2} * (refined2588.py's first-slot rule; every slot lies in two sets, so sum L*count = 2D). Driven by gap codes only: * x = open run of the set {s-2,s}, y = open run of {s,s+2} (s = residue of the current slot): * code 0: x++, y++; code 1 (+2): close x, x = y+1, y = 1; code 2 (-2): close y, y = x+1, x = 1; * code 3: close x and y, x = y = 1. * Cyclic start per prime: counting begins after the first code-3 gap and the prefix is re-fed at the end. * Tile build: T23 positions by recursive folding from T3 = {5} mod 6, then T29 as a uint16 gap word in memory, * then T31 streamed through the fold by 31 in chunks; worker threads take disjoint prime subsets per chunk. * * usage: rl31 QLO QHI NTHREADS [29] (29: stream T29 itself, a control level) stdout: one JSON object (deterministic); stderr: progress and timing. */ #include #include #include #include #include #include static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + 1e-9 * t.tv_nsec; } #define MAXQ 128 #define HM 64 #define CHUNK (1u << 26) #define PREFIX (1u << 20) static int nq, qs[MAXQ]; static uint8_t code[MAXQ][256]; /* indexed by gap/6 */ typedef struct { int started; uint64_t skipped; /* gaps consumed before the first code-3 gap (inclusive) */ uint32_t x, y, c; uint64_t href[HM + 1], hloose[HM + 1]; } St; static St st[MAXQ]; static uint8_t *buf; static uint64_t buflen; static uint8_t prefix[PREFIX]; static uint64_t nprefix; static int nthreads; static inline void rec(uint64_t *h, uint32_t L) { h[L > HM ? HM : L]++; } static void step(St *s, int c) { if (!s->started) { s->skipped++; if (c == 3) { s->started = 1; s->x = s->y = 1; s->c = 0; } return; } switch (c) { case 0: s->x++; s->y++; s->c++; break; case 1: rec(s->href, s->x); s->x = s->y + 1; s->y = 1; s->c++; break; case 2: rec(s->href, s->y); s->y = s->x + 1; s->x = 1; s->c++; break; default: rec(s->href, s->x); rec(s->href, s->y); s->x = s->y = 1; rec(s->hloose, s->c); s->c = 0; } } static void *worker(void *arg) { long t = (long)arg; for (int k = (int)t; k < nq; k += nthreads) { St *s = &st[k]; const uint8_t *cd = code[k]; for (uint64_t i = 0; i < buflen; i++) step(s, cd[buf[i]]); } return NULL; } static void flush_chunk(void) { pthread_t th[64]; for (long t = 0; t < nthreads; t++) pthread_create(&th[t], NULL, worker, (void *)t); for (int t = 0; t < nthreads; t++) pthread_join(th[t], NULL); buflen = 0; } int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "usage: rl31 QLO QHI NTHREADS\n"); return 2; } int QLO = atoi(argv[1]), QHI = atoi(argv[2]); nthreads = atoi(argv[3]); int FOLD = (argc >= 5 && atoi(argv[4]) == 29) ? 1 : 31; if (nthreads < 1 || nthreads > 64) return 2; for (int q = QLO; q <= QHI; q++) { int pr = q > 1; for (int d = 2; d * d <= q; d++) if (q % d == 0) { pr = 0; break; } if (pr) { if (nq == MAXQ) return 2; qs[nq++] = q; } } for (int k = 0; k < nq; k++) for (int g6 = 0; g6 < 256; g6++) { int d = (g6 * 6) % qs[k]; code[k][g6] = d == 0 ? 0 : d == 2 ? 1 : d == qs[k] - 2 ? 2 : 3; } double t0 = now(); static const int PR[] = {5, 7, 11, 13, 17, 19, 23}; uint64_t P = 6, D = 1; uint64_t *pos = malloc(8); pos[0] = 5; for (int ii = 0; ii < 7; ii++) { int f = PR[ii]; uint64_t Dn = D * (uint64_t)(f - 2), c = 0; uint64_t *np = malloc(Dn * 8); for (int k = 0; k < f; k++) for (uint64_t i = 0; i < D; i++) { uint64_t n = (uint64_t)k * P + pos[i]; int r = (int)(n % f); if (r == 0 || r == f - 2) continue; np[c++] = n; } if (c != Dn) { fprintf(stderr, "count mismatch T%d\n", f); return 3; } free(pos); pos = np; P *= f; D = Dn; } /* T29 gap word */ uint64_t D29 = D * 27, P29 = P * 29, c = 0, prev = 0, first29 = 0; uint16_t *g29 = malloc(D29 * 2); for (int k = 0; k < 29; k++) for (uint64_t i = 0; i < D; i++) { uint64_t n = (uint64_t)k * P + pos[i]; int r = (int)(n % 29); if (r == 0 || r == 27) continue; if (c == 0) first29 = n; else g29[c - 1] = (uint16_t)(n - prev); prev = n; c++; } if (c != D29) { fprintf(stderr, "count mismatch T29\n"); return 3; } g29[D29 - 1] = (uint16_t)(P29 - prev + first29); free(pos); fprintf(stderr, "# T29 built: D=%llu P=%llu first=%llu (%.1fs)\n", (unsigned long long)D29, (unsigned long long)P29, (unsigned long long)first29, now() - t0); /* stream T31 = fold of T29 by 31, starting at the first T31 slot >= first29 */ uint64_t P31 = P29 * 31, Dexp = D29 * 29; uint8_t m31[65536]; for (int g = 0; g < 65536; g++) m31[g] = (uint8_t)(g % 31); if (FOLD == 1) { P31 = P29; Dexp = D29; memset(m31, 0, sizeof m31); } /* residue stays 1: nothing is removed */ buf = malloc(CHUNK); uint64_t n = first29; int r = (int)(n % 31); uint64_t i29 = 0; if (FOLD == 1) r = 1; while (r == 0 || r == 29) { uint16_t g = g29[i29]; n += g; r += m31[g]; if (r >= 31) r -= 31; i29 = (i29 + 1) % D29; } uint64_t first31 = n, cnt = 1, sum = 0, acc = 0, gmax = 0, not6 = 0, big = 0; uint64_t ghist[256]; memset(ghist, 0, sizeof ghist); uint64_t total_steps = D29 * (uint64_t)FOLD; /* one full period of the fold, back to first31 + P31 */ for (uint64_t s = 0; s < total_steps; s++) { uint16_t g = g29[i29]; i29 = i29 + 1 == D29 ? 0 : i29 + 1; n += g; acc += g; r += m31[g]; if (r >= 31) r -= 31; if (r == 0 || r == 29) continue; if (acc % 6) not6++; if (acc / 6 > 255) { big++; acc = 0; continue; } uint8_t g6 = (uint8_t)(acc / 6); ghist[g6]++; sum += acc; if (acc > gmax) gmax = acc; if (nprefix < PREFIX) prefix[nprefix++] = g6; buf[buflen++] = g6; acc = 0; cnt++; if (buflen == CHUNK) flush_chunk(); } cnt--; /* the last kept slot is first31 + P31, the start again */ flush_chunk(); int wrap_ok = (n == first31 + P31); /* close the cycle: re-feed each prime's skipped prefix */ for (int k = 0; k < nq; k++) { St *s = &st[k]; uint64_t sk = s->skipped; if (!s->started || sk > nprefix) { fprintf(stderr, "prefix too short for q=%d\n", qs[k]); return 4; } s->started = 1; /* already started; feed the prefix through the normal step */ for (uint64_t i = 0; i < sk; i++) step(s, code[k][prefix[i]]); } free(buf); free(g29); fprintf(stderr, "# T31 streamed: D=%llu (%.1fs)\n", (unsigned long long)cnt, now() - t0); int ndist = 0; for (int g6 = 0; g6 < 256; g6++) if (ghist[g6]) ndist++; printf("{\"tile\":{\"P\":%llu,\"D\":%llu,\"D_expected\":%llu,\"first_slot\":%llu,\"sum_gaps\":%llu,\"max_gap\":%llu," "\"gaps_not_mult6\":%llu,\"gaps_over_1530\":%llu,\"wrap_ok\":%d,\"distinct_gaps\":%d,\"gap_hist\":{", (unsigned long long)P31, (unsigned long long)cnt, (unsigned long long)Dexp, (unsigned long long)first31, (unsigned long long)sum, (unsigned long long)gmax, (unsigned long long)not6, (unsigned long long)big, wrap_ok, ndist); int f1 = 1; for (int g6 = 0; g6 < 256; g6++) if (ghist[g6]) { printf("%s\"%d\":%llu", f1 ? "" : ",", g6 * 6, (unsigned long long)ghist[g6]); f1 = 0; } printf("}},\"per_q\":{"); for (int k = 0; k < nq; k++) { St *s = &st[k]; int R = 0, L = 0; uint64_t sLc = 0; for (int j = 0; j <= HM; j++) { if (s->hloose[j]) R = j; if (s->href[j]) { L = j; sLc += (uint64_t)j * s->href[j]; } } printf("%s\"%d\":{\"R_loose\":%d,\"L_refined\":%d,\"sum_L_count\":%llu,\"spectrum\":{", k ? "," : "", qs[k], R, L, (unsigned long long)sLc); int f = 1; for (int j = 1; j <= HM; j++) if (s->href[j]) { printf("%s\"%d\":%llu", f ? "" : ",", j, (unsigned long long)s->href[j]); f = 0; } printf("},\"loose_runs\":{"); f = 1; for (int j = 1; j <= HM; j++) if (s->hloose[j]) { printf("%s\"%d\":%llu", f ? "" : ",", j, (unsigned long long)s->hloose[j]); f = 0; } printf("}}"); } printf("}}\n"); fprintf(stderr, "# done (%.1fs)\n", now() - t0); return 0; }