// parity.c -- job #15: exact counts behind the two-sided parity identity of // research/fold-arithmetic-bridge.md, Proposition 1: // 4 T S = 4 P_odd P'_odd + (C S - A B) - 4 N_odd3 S. // Independent of the served validator: own smallest-prime-factor table, Omega by table, 128-bit arithmetic. // Build: cc -O2 -o parity parity.c Run: ./parity NMAX < tasks.txt > results.txt // Task line: id lo hi y n runs over lo..hi inclusive; n is in S iff n(n+2) has no prime factor <= y // (1 has no prime factors, so n = 1 is in S whenever 3 > y or y < 3). // Output line: id S A B C Podd Podd2 N3 Tin Tall Tboth n1inS okIn okAll okBoth fourSign // Tin = #{n in S : n, n+2 both prime} (the note's T) // Tall = #{lo <= n <= hi : n, n+2 both prime} (twin openers in the interval, S ignored) // Tboth = #{n in S : n, n+2 both prime, n+2 <= hi} (both members inside the interval) // okX = [4 T_X S == right-hand side]; fourSign = [S - A - B + C == 4 (Tin + N3)] #include #include #include typedef __int128 i128; int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "usage: parity NMAX < tasks\n"); return 2; } uint64_t N = strtoull(argv[1], 0, 10); uint32_t *spf = calloc(N + 1, sizeof(uint32_t)); uint8_t *Om = calloc(N + 1, 1); if (!spf || !Om) { fprintf(stderr, "alloc\n"); return 1; } for (uint64_t i = 2; i <= N; i++) if (!spf[i]) for (uint64_t j = i; j <= N; j += i) if (!spf[j]) spf[j] = (uint32_t)i; for (uint64_t i = 2; i <= N; i++) Om[i] = Om[i / spf[i]] + 1; // Om[1] = 0 char id[96]; long long lo, hi, y; while (scanf("%95s %lld %lld %lld", id, &lo, &hi, &y) == 4) { if (lo < 1 || hi + 2 > (long long)N) { printf("%s ERR\n", id); continue; } long long S = 0, A = 0, B = 0, C = 0, Po = 0, Po2 = 0, N3 = 0, Tin = 0, Tall = 0, Tboth = 0; int n1 = 0; for (long long n = lo; n <= hi; n++) { int p1 = Om[n] == 1, p2 = Om[n + 2] == 1; if (p1 && p2) Tall++; int r1 = (n == 1) || (long long)spf[n] > y; int r2 = (long long)spf[n + 2] > y; if (!(r1 && r2)) continue; if (n == 1) n1 = 1; int o1 = Om[n] & 1, o2 = Om[n + 2] & 1; int l1 = o1 ? -1 : 1, l2 = o2 ? -1 : 1; S++; A += l1; B += l2; C += l1 * l2; Po += o1; Po2 += o2; if (o1 && o2 && (Om[n] >= 3 || Om[n + 2] >= 3)) N3++; if (p1 && p2) { Tin++; if (n + 2 <= hi) Tboth++; } } i128 rhs = (i128)4 * Po * Po2 + ((i128)C * S - (i128)A * B) - (i128)4 * N3 * S; int okIn = (i128)4 * Tin * S == rhs, okAll = (i128)4 * Tall * S == rhs, okBoth = (i128)4 * Tboth * S == rhs; int four = (S - A - B + C) == 4 * (Tin + N3); printf("%s %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %d %d %d %d %d\n", id, S, A, B, C, Po, Po2, N3, Tin, Tall, Tboth, n1, okIn, okAll, okBoth, four); } return 0; }