# compare.py -- job #5 cross-checks over zone-conv.log (method A, sieve), zone-trial.log # (method B, trial division) and the served script's stdout. from math import isqrt def rows(path): for ln in open(path): if ln.startswith('#'): continue yield ln.split() def is_prime(n): return n >= 2 and all(n % d for d in range(3, isqrt(n) + 1, 2)) and (n == 2 or n % 2) A = {} for f in rows('zone-conv.log'): p = int(f[0]); bm = [tuple(map(int, x.split(','))) for x in f[9:16]] A[p] = dict(pn=int(f[1]), Sc=int(f[3]), Sa=int(f[4]), Zc=int(f[6]), Za=int(f[7]), bm=bm, fb=int(f[17]), cls=f[18]) W = ['S', 'S<=', 'Z', 'Z<=', 'Zs', 'Z[', 'Z0'] # 1. method B against method A, p <= 2000 n = mism = 0; flag_yes = 0 for f in rows('zone-trial.log'): p = int(f[0]); a = A[p]; n += 1 zc, za, zb, lc, la, lb = map(int, (f[3], f[4], f[5], f[7], f[8], f[9])) flag_yes += f[11] == 'yes' ok = (zc, za, zb) == (a['Zc'], a['Za'], a['bm'][2][0]) and lb == a['bm'][3][0] and lc - zc == lb and la == za if not ok: mism += 1; print('B/A MISMATCH', p, f, a) print(f'1. trial division (B) vs lpf sieve (A): {n} primes p <= 2000, mismatches {mism}; slot at r = p\'^2-2 in B: {flag_yes}, in A: {sum(1 for p in A if p <= 2000 and A[p]["bm"][3][0])}') # 2. served script's eight primes got = {} for ln in open('script-stdout.txt'): kv = dict(x.split('=') for x in ln.split() if '=' in x) got[int(kv['p'])] = (int(kv['cand']), int(kv['act'])) ok = all(got[p] == (A[p]['Sc'], A[p]['Sa']) for p in got) print(f'2. script window (p, p^2) at {sorted(got)}: method A cand/act equal to script output: {ok}') for p in sorted(got): print(f' p={p}: script cand=act={got[p][0]}; zone (p, p\'^2) pairs {A[p]["Za"]} (+{A[p]["Za"] - got[p][1]} in [p^2, p\'^2))') # 3. the bad slots of the closed ceiling: is r = p'^2 - 2 always prime? bad = [p for p in A if A[p]['bm'][3][0]] allprime = all(is_prime(A[p]['pn'] ** 2 - 2) for p in bad) print(f'3. closed ceiling r+2 <= p\'^2: bad slot at {len(bad)}/{len(A)} primes, each is (p\'^2-2, p\'^2) with p\'^2-2 prime: {allprime}') print(f' first: {[(p, A[p]["pn"]**2 - 2) for p in sorted(bad)[:6]]} last: {[(p, A[p]["pn"]**2 - 2) for p in sorted(bad)[-2:]]}') TOP = "top=p'^2"; n_top = sum(1 for p in A if A[p]['cls'] == TOP); oth = [p for p in A if A[p]['cls'] == 'other'] ratio = min((A[p]['fb'] + 2) / A[p]['pn'] ** 2 for p in oth) print(f" first bad slot above p equals that slot at {n_top} primes; at the other {len(oth)} its top member exceeds p'^2: min (fb+2)/p'^2 = {ratio:.6f}") zc = sorted(p for p in A if A[p]['bm'][5][1]); tw = [p for p in A if is_prime(p) and is_prime(p + 2)] print(f'4. zone closed at p: missed twin (p, p+2) at {len(zc)} primes; lesser twin primes <= 9973: {len(tw)}; same set: {set(zc) == set(tw)}') print(f' whole head [1, p\'^2): the one bad slot is at p = {[p for p in A if A[p]["bm"][6][0]]}') # 5. OEIS A273257 (b-file, n = 1..10000) against zone counts b = {} for ln in open('b273257.txt'): if ln.strip() and not ln.startswith('#'): k, v = ln.split(); b[int(k)] = int(v) ps = sorted(A); idx = {p: i + 1 for i, p in enumerate(ps)} # p = prime(idx) eq_shift = [p for p in ps if idx[p] + 1 in b and A[p]['Za'] != b[idx[p] + 1]] eq_open = [p for p in ps if p in A and A[p]['pn'] in A and A[A[p]['pn']]['Sa'] != b[idx[p] + 1]] eq_same = [p for p in ps if A[p]['Sa'] != b[idx[p]]] print(f'5. A273257 over n = 1..{len(ps)}:') print(f' zone(prime(n)) = pairs in (p, p\'^2) vs a(n+1): mismatches at p = {eq_shift}') print(f' pairs in open (p\', p\'^2) vs a(n+1): mismatches at {len(eq_open)} primes, e.g. p\' = {[A[p]["pn"] for p in eq_open[:8]]}') print(f' pairs in open (p, p^2) (script) vs a(n): mismatches at {len(eq_same)} primes; lesser twin primes among them: {sum(1 for p in eq_same if p in tw)}; others {[p for p in eq_same if p not in tw]}')