/* loosecensus.c -- job #4523 (route 67 step check): loose-run census of the twin tile. * * Tile: T_x = { r in [0, P) : gcd(r, x#) = gcd(r+2, x#) = 1 }, P = x#. * Slots are r = 30j + c with c in {11, 17, 29} (r and r+2 coprime to 30), j in [0, n), * n = x#/30. The cyclic gap word is the difference sequence between consecutive slots in * increasing r order, the last gap wrapping around P. * * Definitions (verbatim from returns #1244/#971/#1802 and #1802's rl31.c): * gap g is LOOSE-QUALIFYING for the prime q iff g mod q in {0, 2, q-2}; * R_loose(T_x,q) = the longest cyclic run of consecutive qualifying gaps; * loose_runs[h] = the number of maximal qualifying runs of length h. * * ALGORITHM. One bit i(q) per prime q in [QLO,QHI] (at most 64). For a gap value g, * M(g) = OR of 2^{i(q)} over the q qualifying g, * and the walk keeps the MAXL-deep chain * B_k = M(g_k) & M(g_{k-1}) & ... & M(g_{k-MAXL+1}). * Bit i(q) of B_k is set iff the current qualifying run for q has length >= L for the L-th * slice; the run length for q is the largest L with a set bit, so a single 64-bit AND and * ring-buffer update per gap drives ALL primes at once -- the cost is independent of the * number of primes (this is route 67's bit-sliced loose-run counter, made correct). * Bookkeeping per prime: a run starts when its bit enters B, ends when it leaves; the * leaving step records its length in loose_runs. Runs crossing the period boundary are * closed by replaying the first MAXL-1 masks after the walk. * * usage: loosecensus * stdout: one JSON object (deterministic key order); stderr: progress and timing. * Build (MSVC): cl /O2 loosecensus.c */ #include #include #include #include #ifdef _WIN32 #include #include #endif #define SEGW 32768 #define EXTW 8 #define PATW 17017 #define MAXQ 64 #define MAXL 64 #define MAXG 2048 #define HB (MAXG / 6 + 4) static const int CLS[3] = {11, 17, 29}; static int X, QLO, QHI, nstrike, sq[16], nq; static int qlist[MAXQ]; static uint64_t n, nwords, nseg; static uint64_t *pat[3]; static int64_t sres[3][16][2]; static int err = 0; static uint64_t mask_of[MAXG / 6 + 2]; static long long cur_run[MAXQ]; static unsigned long long hist[MAXQ][HB]; static long long Rloose[MAXQ]; static uint64_t qual_prev; static double now(void) { #ifdef _WIN32 LARGE_INTEGER f, c; QueryPerformanceFrequency(&f); QueryPerformanceCounter(&c); return (double)c.QuadPart / (double)f.QuadPart; #else struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + 1e-9 * t.tv_nsec; #endif } static inline int ctz64(uint64_t v) { #ifdef _WIN32 unsigned long i; _BitScanForward64(&i, v); return (int)i; #else return __builtin_ctzll(v); #endif } static int64_t inv30(int q) { for (int a = 1; a < q; a++) if ((30 * a) % q == 1) return a; return -1; } static int member_slow(uint64_t j, int ci) { static const int wq[4] = {7, 11, 13, 17}; uint64_t r = 30 * j + CLS[ci]; for (int k = 0; k < 4; k++) { uint64_t m = r % wq[k]; if (m == 0 || m == (uint64_t)(wq[k] - 2)) return 0; } return 1; } static uint64_t ring[MAXL]; static int rpos; static uint64_t bexp; static inline void book_mask(uint64_t m) { /* exact per-prime run bookkeeping: a run ends at this gap for every prime for which the previous gap qualified and this one does not; it starts where this gap qualifies and the previous did not; it advances where both qualify. */ uint64_t endb = qual_prev & ~m; uint64_t startb = m & ~qual_prev; uint64_t stayb = m & qual_prev; int i; while (endb) { i = ctz64(endb); endb &= endb - 1; long long h = cur_run[i]; if (h < 1) h = 1; if (h >= HB) h = HB - 1; hist[i][h]++; if (cur_run[i] > Rloose[i]) Rloose[i] = cur_run[i]; cur_run[i] = 0; } while (startb) { i = ctz64(startb); startb &= startb - 1; cur_run[i] = 1; } while (stayb) { i = ctz64(stayb); stayb &= stayb - 1; if (cur_run[i] == 0) cur_run[i] = 1; cur_run[i]++; } qual_prev = m; } static inline void step(int g) { book_mask(mask_of[g / 6]); } int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "usage: loosecensus \n"); return 2; } X = atoi(argv[1]); QLO = atoi(argv[2]); QHI = atoi(argv[3]); if (X < 17) return 2; n = 1; for (int q = 7; q <= X; q++) { int pr = 1; for (int d = 2; d * d <= q; d++) if (q % d == 0) pr = 0; if (pr) { n *= q; if (q >= 19) sq[nstrike++] = q; } } nwords = (n + 63) / 64; nseg = (nwords + SEGW - 1) / SEGW; nq = 0; for (int q = QLO; q <= QHI; q++) { int pr = 1; for (int d = 2; d * d <= q; d++) if (q % d == 0) pr = 0; if (pr && nq < MAXQ) qlist[nq++] = q; } for (int c = 0; c < 3; c++) { pat[c] = calloc(PATW, 8); for (uint64_t j = 0; j < 64ULL * PATW; j++) if (member_slow(j, c)) pat[c][j >> 6] |= 1ULL << (j & 63); for (int t = 0; t < nstrike; t++) { int64_t q = sq[t], iv = inv30((int)q); sres[c][t][0] = (((-(int64_t)CLS[c]) % q + q) % q) * iv % q; sres[c][t][1] = (((-(int64_t)CLS[c] - 2) % q + q) % q) * iv % q; } } for (int j = 0; j <= MAXG / 6; j++) { int g = 6 * j; uint64_t m = 0; for (int i = 0; i < nq; i++) { int q = qlist[i], r = g % q; if (r == 0 || r == 2 || r == q - 2) m |= (uint64_t)1 << i; } mask_of[j] = m; } fprintf(stderr, "x=%d n=%llu segs=%llu primes=%d [%d..%d]\n", X, (unsigned long long)n, (unsigned long long)nseg, nq, nq ? qlist[0] : -1, nq ? qlist[nq - 1] : -1); double t0 = now(); uint64_t D = 0; int G2 = 0; uint64_t G2r = 0; uint64_t *B[3]; for (int c = 0; c < 3; c++) B[c] = malloc(8 * (SEGW + EXTW)); memset(ring, 0, sizeof(ring)); rpos = 0; bexp = 0; qual_prev = 0; const int PRE = MAXL - 1; uint64_t first_mask[MAXL]; int nfirst = 0; uint64_t qual_total[MAXQ]; memset(qual_total, 0, sizeof(qual_total)); for (int64_t s = 0; s < (int64_t)nseg; s++) { uint64_t w0 = (uint64_t)s * SEGW, j0 = w0 * 64, jend = j0 + 64ULL * SEGW; if (jend > n) jend = n; uint64_t nw = SEGW + EXTW; for (int c = 0; c < 3; c++) { uint64_t po = w0 % PATW; for (uint64_t k = 0; k < nw; k++) { B[c][k] = pat[c][po]; if (++po == PATW) po = 0; } for (int t = 0; t < nstrike; t++) { int64_t q = sq[t]; for (int e = 0; e < 2; e++) { int64_t a = sres[c][t][e]; uint64_t first = j0 + (uint64_t)(((a - (int64_t)(j0 % q)) % q + q) % q); for (uint64_t j = first - j0; j < nw * 64; j += q) B[c][j >> 6] &= ~(1ULL << (j & 63)); } } } int64_t prev = -1; int prevorigin = 0; uint64_t norig = 0; for (uint64_t k = 0; k < nw; k++) { uint64_t u = B[0][k] | B[1][k] | B[2][k]; while (u) { int b = ctz64(u); u &= u - 1; uint64_t j = k * 64 + b; for (int c = 0; c < 3; c++) if ((B[c][k] >> b) & 1) { int64_t r = (int64_t)(30 * j + CLS[c]); if (prev >= 0 && prevorigin) { int64_t g = r - prev; if (g > MAXG) err = 2; else if (g % 6) err = 3; if (g > G2) { G2 = (int)g; G2r = 30 * j0 + (uint64_t)prev; } for (int i = 0; i < nq; i++) { int q = qlist[i], rr = (int)(g % q); if (rr == 0 || rr == 2 || rr == q - 2) qual_total[i]++; } if (nfirst < PRE) { first_mask[nfirst] = mask_of[(int)(g / 6)]; nfirst++; } /* cyclic carry: before the first step, set the "previous gap" mask from the code of this first gap, so a run that continues into this gap is not broken at the period boundary */ if (nfirst == 1) { uint64_t carry = 0; for (int i = 0; i < nq; i++) { int q = qlist[i], rr = (int)(g % q); int c = (rr == 0) ? 0 : (rr == 2 ? 1 : (rr == q - 2 ? 2 : 3)); if (c != 3) carry |= (uint64_t)1 << i; } qual_prev = carry; } step((int)g); } prev = r; prevorigin = (j0 + j < jend); if (prevorigin) norig++; else goto done; } } } done: D += norig; if ((s & 8191) == 0) fprintf(stderr, " seg %lld/%llu\n", (long long)s, (unsigned long long)nseg); } /* the cyclic carry was set before the first step, so every maximal cyclic run has already been counted exactly once; nothing to stitch here */ for (int i = 0; i < nq; i++) { if (cur_run[i] > 0) { long long h = cur_run[i]; if (h >= HB) h = HB - 1; hist[i][h]++; if (cur_run[i] > Rloose[i]) Rloose[i] = cur_run[i]; cur_run[i] = 0; } } for (int i = 0; i < nq; i++) while (Rloose[i] >= 1 && hist[i][Rloose[i]] == 0) Rloose[i]--; double t1 = now(); printf("{\"x\": %d, \"P\": %llu, \"D\": %llu, \"G2\": %d, \"G2_first_r\": %llu,\n", X, (unsigned long long)(30 * n), (unsigned long long)D, G2, (unsigned long long)G2r); printf(" \"QLO\": %d, \"QHI\": %d,\n", QLO, QHI); printf(" \"per_q\": {"); for (int i = 0; i < nq; i++) { unsigned long long s_len = 0, s_cnt = 0; for (int k = 1; k < HB; k++) { s_len += (unsigned long long)k * hist[i][k]; s_cnt += hist[i][k]; } printf("%s\"%d\": {\"R_loose\": %lld, \"qual_total\": %llu, \"sum_run_len\": %llu, \"nruns\": %llu, \"loose_runs\": {", i ? ", " : "", qlist[i], Rloose[i], qual_total[i], s_len, s_cnt); { int f = 1; for (int k = 1; k < HB; k++) if (hist[i][k]) { printf("%s\"%d\": %llu", f ? "" : ", ", k, hist[i][k]); f = 0; } } printf("}}"); } printf("}}\n"); fprintf(stderr, "done in %.1f s; err=%d\n", t1 - t0, err); return err ? 1 : 0; }