// g2sieve.c -- G2(x#) by direct sieve. solveathome job #7, written from the definition; // shares no code with the primeoire repo and none with g2fold.c. // // A twin slot mod P = x# is r in [0,P) with r mod p != 0 and (r+2) mod p != 0 for every prime p <= x. // For each x this prints D (number of slots), G2 (largest cyclic gap between consecutive slots), // least (least slot r whose next slot, cyclically, is r+G2) and nmax (how many slots r have that). // // ./g2sieve naive x1 x2 ... trial division of every r in [0,P), one thread // ./g2sieve lattice T x1 x2 ... x >= 3 only: every slot is 5 mod 6 (odd, and 2 mod 3), so // r = 6k+5; segmented byte sieve over k with T threads // // stdout is deterministic (hash it); timings go to stderr. #include #include #include #include #include #include typedef uint64_t u64; typedef unsigned long long ull; static const int PR[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43}; static const int NPR = 14; typedef struct { u64 first, last, max, least, nmax, count; } Stats; static void st_init(Stats *s) { s->first = UINT64_MAX; s->last = 0; s->max = 0; s->least = UINT64_MAX; s->nmax = 0; s->count = 0; } static void st_gap(Stats *s, u64 gap, u64 lower) { if (gap > s->max) { s->max = gap; s->least = lower; s->nmax = 1; } else if (gap == s->max) { s->nmax++; if (lower < s->least) s->least = lower; } } // slots must be offered in increasing order static inline void st_slot(Stats *s, u64 r) { if (s->count == 0) s->first = r; else st_gap(s, r - s->last, s->last); s->last = r; s->count++; } static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + 1e-9 * t.tv_nsec; } static u64 primorial(int x) { u64 P = 1; for (int i = 0; i < NPR && PR[i] <= x; i++) P *= PR[i]; return P; } static u64 dformula(int x) { u64 D = 1; for (int i = 0; i < NPR && PR[i] <= x; i++) if (PR[i] >= 3) D *= PR[i] - 2; return D; } static void report(const char *mode, int x, u64 P, const Stats *s) { printf("x=%-3d P=%-16llu D=%-14llu G2=%-5llu least=%-18llu nmax=%-3llu D==prod(p-2):%s [%s]\n", x, (ull)P, (ull)s->count, (ull)s->max, (ull)s->least, (ull)s->nmax, s->count == dformula(x) ? "yes" : "NO", mode); fflush(stdout); } // ---------------------------------------------------------------- naive static void run_naive(int x) { u64 P = primorial(x); int ps[16], n = 0; for (int i = 0; i < NPR && PR[i] <= x; i++) ps[n++] = PR[i]; double t0 = now(); Stats s; st_init(&s); for (u64 r = 0; r < P; r++) { int ok = 1; for (int j = 0; j < n; j++) { u64 p = ps[j]; if (r % p == 0 || (r + 2) % p == 0) { ok = 0; break; } } if (ok) st_slot(&s, r); } st_gap(&s, s.first + P - s.last, s.last); // the wrap gap report("naive", x, P, &s); fprintf(stderr, "naive x=%d %.2f s\n", x, now() - t0); } // ---------------------------------------------------------------- lattice #define SEG (1u << 20) typedef struct { int x; u64 ka, kb; Stats s; } Job; static void *lattice_worker(void *arg) { Job *J = arg; int ps[16], n = 0; for (int i = 0; i < NPR && PR[i] <= J->x; i++) if (PR[i] >= 5) ps[n++] = PR[i]; u64 a0[16], a1[16]; // k mod p with p | 6k+5, and with p | 6k+7 for (int j = 0; j < n; j++) { u64 p = ps[j]; for (u64 k = 0; k < p; k++) { if ((6 * k + 5) % p == 0) a0[j] = k; if ((6 * k + 7) % p == 0) a1[j] = k; } } unsigned char *buf = malloc(SEG + 8); st_init(&J->s); for (u64 K0 = J->ka; K0 < J->kb; K0 += SEG) { u64 len = J->kb - K0; if (len > SEG) len = SEG; u64 len8 = (len + 7) & ~7ULL; memset(buf, 0, len); for (u64 i = len; i < len8; i++) buf[i] = 0x80; // padding is never a slot for (int j = 0; j < n; j++) { u64 p = ps[j], m = K0 % p; for (u64 i = (a0[j] + p - m) % p; i < len; i += p) buf[i] = 0x80; for (u64 i = (a1[j] + p - m) % p; i < len; i += p) buf[i] = 0x80; } for (u64 w = 0; w < len8; w += 8) { // bytes are 0 (slot) or 0x80 (dead) u64 v; memcpy(&v, buf + w, 8); u64 z = ~v & 0x8080808080808080ULL; while (z) { u64 b = (u64)__builtin_ctzll(z) >> 3; z &= z - 1; st_slot(&J->s, 6 * (K0 + w + b) + 5); } } } free(buf); return NULL; } static void run_lattice(int x, int T) { if (x < 3) { fprintf(stderr, "lattice needs x >= 3\n"); exit(2); } u64 P = primorial(x), N = P / 6; double t0 = now(); Job *J = calloc(T, sizeof(Job)); pthread_t *th = calloc(T, sizeof(pthread_t)); for (int i = 0; i < T; i++) { J[i].x = x; J[i].ka = N / T * i; J[i].kb = (i == T - 1) ? N : N / T * (i + 1); pthread_create(&th[i], NULL, lattice_worker, &J[i]); } for (int i = 0; i < T; i++) pthread_join(th[i], NULL); Stats g; st_init(&g); // merge the contiguous ranges in order, then close the cycle for (int i = 0; i < T; i++) { Stats *c = &J[i].s; if (c->count == 0) continue; if (g.count == 0) g.first = c->first; else st_gap(&g, c->first - g.last, g.last); if (c->nmax) { if (c->max > g.max) { g.max = c->max; g.least = c->least; g.nmax = c->nmax; } else if (c->max == g.max) { g.nmax += c->nmax; if (c->least < g.least) g.least = c->least; } } g.last = c->last; g.count += c->count; } st_gap(&g, g.first + P - g.last, g.last); report("lattice", x, P, &g); fprintf(stderr, "lattice x=%d T=%d %.2f s\n", x, T, now() - t0); free(J); free(th); } int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: g2sieve naive x... | g2sieve lattice T x...\n"); return 2; } if (!strcmp(argv[1], "naive")) { for (int i = 2; i < argc; i++) run_naive(atoi(argv[i])); return 0; } if (!strcmp(argv[1], "lattice")) { int T = atoi(argv[2]); for (int i = 3; i < argc; i++) run_lattice(atoi(argv[i]), T); return 0; } return 2; }