/* tcensus.c -- job 2802 (route 52 pursuit): streaming census of the twin tile T_x = {r mod x# : gcd(r(r+2), x#) = 1}. Openers are r = 30 j + c with c in {11, 17, 29} (r and r+2 coprime to 30), j in [0, n), n = x#/30. Per class a bitmap over j is sieved segment by segment: wheel pattern for q = 7, 11, 13, 17 (period 17017 words), then strikes for 19 <= q <= x. Membership is periodic in j with period n, so a segment's look-ahead past n is the wrap. Output (stdout, JSON): D, G2, N_g (histogram of cyclic gaps between consecutive openers) and cop(g) = #{r in T : r+g in T} measured by shifted ANDs of the class bitmaps, for g = 6, 12, ..., GMAX. Progress goes to stderr. Usage: tcensus [GMAX] cc -O2 -o tcensus tcensus.c -lpthread */ #include #include #include #include #include #define SEGW 65536 /* origin words per segment */ #define EXTW 8 /* look-ahead words (512 j = 15360 in r) */ #define PATW 17017 /* 7*11*13*17 words = 64*17017 bits */ #define MAXG 4096 static const int CLS[3] = {11, 17, 29}; static int X, NT, GMAX, nstrike, sq[16]; static uint64_t n, nwords, nseg; static uint64_t *pat[3]; static int64_t sres[3][16][2]; /* strike residues of j mod q per class */ static volatile uint64_t nextseg = 0; static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; static uint64_t Ng[MAXG + 1], cop[MAXG + 1], D = 0; static int G2 = 0; static uint64_t G2r = 0; static int err = 0; 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) { /* reference membership (used for the wheel pattern) */ 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 void *worker(void *arg) { (void)arg; uint64_t *B[3]; for (int c = 0; c < 3; c++) B[c] = malloc(8 * (SEGW + EXTW)); uint64_t *lNg = calloc(MAXG + 1, 8), *lcop = calloc(MAXG + 1, 8), lD = 0, lG2r = 0; int lG2 = 0; for (;;) { uint64_t s = __sync_fetch_and_add(&nextseg, 1); if (s >= nseg) break; uint64_t w0 = s * SEGW, j0 = w0 * 64, jend = j0 + 64ULL * SEGW; if (jend > n) jend = n; /* origins: j in [j0, jend) */ 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)); } } } /* origin mask words */ uint64_t ow = (jend - j0 + 63) / 64; uint64_t lastmask = ((jend - j0) % 64) ? ((1ULL << ((jend - j0) % 64)) - 1) : ~0ULL; /* gap census: walk openers in r order across the buffer */ int64_t prev = -1; int prevorigin = 0; uint64_t lastorigin_done = 0, 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 = __builtin_ctzll(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]); /* relative to 30*j0 */ if (prev >= 0 && prevorigin) { int64_t g = r - prev; if (g > MAXG) { err = 1; } else lNg[g]++; if (g > lG2) { lG2 = (int)g; lG2r = 30 * j0 + (uint64_t)prev; } } prev = r; prevorigin = (j0 + j < jend); if (prevorigin) norig++; else { lastorigin_done = 1; goto done; } } } } done: if (!lastorigin_done) err = 2; /* last origin had no successor inside the look-ahead */ lD += norig; /* cop(g): pairs (r in class c at j, r+g in class c2 at j+kk) */ for (int g = 6; g <= GMAX; g += 6) for (int c = 0; c < 3; c++) { int c2v = (CLS[c] + g) % 30, c2 = -1; for (int d = 0; d < 3; d++) if (CLS[d] == c2v) c2 = d; if (c2 < 0) continue; uint64_t kk = (uint64_t)(CLS[c] + g - c2v) / 30, kw = kk >> 6; int sh = (int)(kk & 63); uint64_t cnt = 0; for (uint64_t i = 0; i < ow; i++) { uint64_t d2 = sh ? ((B[c2][i + kw] >> sh) | (B[c2][i + kw + 1] << (64 - sh))) : B[c2][i + kw]; uint64_t v = B[c][i] & d2; if (i == ow - 1) v &= lastmask; cnt += (uint64_t)__builtin_popcountll(v); } lcop[g] += cnt; } if (s % 512 == 0) fprintf(stderr, "seg %llu/%llu\n", (unsigned long long)s, (unsigned long long)nseg); } pthread_mutex_lock(&mu); for (int g = 0; g <= MAXG; g++) { Ng[g] += lNg[g]; cop[g] += lcop[g]; } D += lD; if (lG2 > G2 || (lG2 == G2 && lG2r < G2r)) { G2 = lG2; G2r = lG2r; } pthread_mutex_unlock(&mu); return NULL; } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: tcensus x threads [GMAX]\n"); return 2; } X = atoi(argv[1]); NT = atoi(argv[2]); GMAX = argc > 3 ? atoi(argv[3]) : 420; if (X < 17 || GMAX > MAXG - 64) 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; 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++) { /* 30 j + c = 0 or -2 (mod q) */ 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; } } fprintf(stderr, "x=%d n=%llu segments=%llu threads=%d GMAX=%d\n", X, (unsigned long long)n, (unsigned long long)nseg, NT, GMAX); pthread_t th[64]; for (int i = 0; i < NT; i++) pthread_create(&th[i], NULL, worker, NULL); for (int i = 0; i < NT; i++) pthread_join(th[i], NULL); if (err) { fprintf(stderr, "ERROR %d\n", err); return 1; } printf("{\"x\": %d, \"P\": %llu, \"D\": %llu, \"G2\": %d, \"G2_first_r\": %llu, \"GMAX\": %d,\n \"census\": {", X, (unsigned long long)(30 * n), (unsigned long long)D, G2, (unsigned long long)G2r, GMAX); int first = 1; for (int g = 1; g <= MAXG; g++) if (Ng[g]) { printf("%s\"%d\": %llu", first ? "" : ", ", g, (unsigned long long)Ng[g]); first = 0; } printf("},\n \"cop_measured\": {"); first = 1; for (int g = 6; g <= GMAX; g += 6) { printf("%s\"%d\": %llu", first ? "" : ", ", g, (unsigned long long)cop[g]); first = 0; } printf("}}\n"); return 0; }