"""Compare the stated finite cutoffs and the two mop-up rules; no dependencies.""" import json import math Y = 200000 sieve = bytearray(b'\x01') * (Y + 1) sieve[:2] = b'\0\0' for p in range(2, math.isqrt(Y) + 1): if sieve[p]: sieve[p * p::p] = bytes(len(range(p * p, Y + 1, p))) primes = [p for p in range(2, Y + 1) if sieve[p]] def strike(marked, p, a): for residue in {a % p, (a - 2) % p}: start = residue if residue else p marked[start::p] = b'\x01' * len(range(start, Y + 1, p)) def construction(z, greedy): fixed = [p for p in primes if p <= z] available = iter(p for p in primes if p > z) marked = bytearray(Y + 1) for p in fixed: strike(marked, p, 0) survivors = [n for n in range(1, Y + 1) if not marked[n]] spent = [] for n in survivors: if greedy and marked[n]: continue p = next(available) spent.append((p, n % p)) strike(marked, p, n % p) maximum = spent[-1][0] assert len(set(fixed + [p for p, a in spent])) == len(fixed) + len(spent) assert all(marked[1:]) return {'cutoff': round(z, 10), 'largest_fixed_prime': fixed[-1], 'fixed_primes': len(fixed), 'stage1_survivors': len(survivors), 'rule': 'skip already covered survivors' if greedy else 'one distinct prime per original survivor', 'mop_up_primes': len(spent), 'maximum_prime': maximum, 'ratio': round(Y / (maximum * math.log(maximum)), 7), 'uncovered': 0} results = [construction(z, greedy) for z in [13, math.sqrt(Y), math.sqrt(Y) / math.log(Y)] for greedy in [True, False]] assert results[0]['maximum_prime'] == 10301 assert results[2]['maximum_prime'] == 10711 print(json.dumps({'y': Y, 'cases': results}, indent=2, sort_keys=True))