// census.c -- independent twin-slot census of T_p = { r mod p# : gcd(r(r+2), p#) = 1 }. // // Plain segmented bitset sieve over EVERY residue 0 .. p#-1. No mod-6 or mod-30 lattice, // no CRT stride, no fold recursion, no primorial hard-coded: primes come from trial division. // For each prime q <= p it strikes r = 0 and r = q-2 (mod q) (one class when q = 2). // // Per level it prints // D the counted census, against prod_{2 [threads] stdout is deterministic; timings go to stderr. #include #include #include #include #include #include #define SEG_BITS (1ULL << 24) // 16M residues = 2 MB of words per segment #define HIST_MAX 300000000ULL // lift histogram costs one byte per old residue static int primes[64], np; static int lvl; // index of p in primes[] static uint64_t P, Pprev; static uint8_t *hist; // size Pprev, or NULL static uint64_t nseg, next_seg, totD, totNew; static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; static void *worker(void *arg) { (void)arg; uint64_t *bits = malloc((SEG_BITS / 64 + 1) * 8); uint64_t D = 0, nw = 0; for (;;) { pthread_mutex_lock(&mu); uint64_t s = next_seg++; pthread_mutex_unlock(&mu); if (s >= nseg) break; uint64_t lo = s * SEG_BITS, hi = lo + SEG_BITS; if (hi > P) hi = P; uint64_t len = hi - lo, words = (len + 63) / 64; memset(bits, 0, words * 8); for (int i = 0; i <= lvl; i++) { uint64_t q = primes[i]; uint64_t cls[2] = {0, (q - 2) % q}; int nc = cls[1] == 0 ? 1 : 2; for (int c = 0; c < nc; c++) for (uint64_t j = (cls[c] + q - lo % q) % q; j < len; j += q) bits[j >> 6] |= 1ULL << (j & 63); } if (len % 64) bits[words - 1] |= ~0ULL << (len % 64); // positions past the tile count as struck for (uint64_t w = 0; w < words; w++) D += 64 - __builtin_popcountll(bits[w]); for (uint64_t j = (Pprev - 1 + Pprev - lo % Pprev) % Pprev; j < len; j += Pprev) if (!((bits[j >> 6] >> (j & 63)) & 1)) nw++; if (hist) for (uint64_t w = 0; w < words; w++) for (uint64_t x = ~bits[w]; x; x &= x - 1) hist[(lo + w * 64 + __builtin_ctzll(x)) % Pprev]++; } pthread_mutex_lock(&mu); totD += D; totNew += nw; pthread_mutex_unlock(&mu); free(bits); return NULL; } static int in_prev(uint64_t s) { // s mod P_prev in T_prev: s, s+2 coprime to every prime before p for (int i = 0; i < lvl; i++) { uint64_t q = primes[i], r = s % q; if (r == 0 || r == (q - 2) % q) return 0; } return 1; } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: census [threads]\n"); return 2; } int maxp = atoi(argv[1]), th = argc > 2 ? atoi(argv[2]) : 1; if (maxp > 41 || th < 1 || th > 16) { fprintf(stderr, "maxp <= 41, 1 <= threads <= 16\n"); return 2; } for (int n = 2; n <= maxp; n++) { int ok = 1; for (int d = 2; d * d <= n; d++) if (n % d == 0) { ok = 0; break; } if (ok) primes[np++] = n; } printf("tile | width | D counted | prod(q-2), 2 2) prod *= primes[i] - 2; uint64_t surv = totD - totNew, psurv = (p - 2) * (Dprev - 1); printf("T%llu | %llu | %llu | %llu %s | %llu vs %llu %s | %llu vs %llu %s", (unsigned long long)p, (unsigned long long)P, (unsigned long long)totD, (unsigned long long)prod, totD == prod ? "MATCH" : "MISMATCH", (unsigned long long)totNew, (unsigned long long)(p - 2), totNew == p - 2 ? "MATCH" : "DIFFERS", (unsigned long long)surv, (unsigned long long)psurv, surv == psurv ? "MATCH" : "DIFFERS"); if (hist) { uint64_t slots = 0, slot_pm2 = 0, others = 0, other_0 = 0; int hmin = 255, hmax = 0; for (uint64_t s = 0; s < Pprev; s++) { int h = hist[s]; if (in_prev(s)) { slots++; if ((uint64_t)h == p - 2) slot_pm2++; if (h < hmin) hmin = h; if (h > hmax) hmax = h; } else { others++; if (h == 0) other_0++; } } printf(" | T_prev slots %llu (D_prev %llu), lifts per slot min %d max %d, slots with p-2: %llu %s; non-slots %llu, with 0: %llu %s", (unsigned long long)slots, (unsigned long long)Dprev, hmin, hmax, (unsigned long long)slot_pm2, slot_pm2 == slots ? "ALL" : "NOT ALL", (unsigned long long)others, (unsigned long long)other_0, other_0 == others ? "ALL" : "NOT ALL"); free(hist); } else { printf(" | (histogram skipped: P_prev > %llu)", (unsigned long long)HIST_MAX); } printf("\n"); fflush(stdout); fprintf(stderr, "T%llu: %.2f s on %d thread(s)\n", (unsigned long long)p, (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9, t); Dprev = totD; } return 0; }