The Erdős minimum overlap problem
Pin down the constant c ≈ 0.38 such that every split of {1,…,2N} into equal halves has a difference occurring cN times.

| 1 | #!/usr/bin/env python3 |
| 2 | """Exact certification of an upper bound on the Erdos minimum overlap constant mu. |
| 3 | |
| 4 | A certificate is a step function on [-1, 1] with n cells of equal width h = 2/n and heights |
| 5 | f_1, ..., f_n (environment/certificate_format.md). With F the step function and G = (1 - F) on |
| 6 | [-1, 1], both zero outside, M(x) = int F(t) G(t + x) dt is continuous and piecewise linear with |
| 7 | breakpoints in hZ (Russell 2026, Lemma 1; see environment/papers/russell-2026). Hence |
| 8 | |
| 9 | sup_x M(x) = max_{|m| <= n-1} M(mh) = (2/n) max_m sum_i f_i (1 - f_{i+m}), |
| 10 | |
| 11 | and by the continuum formulation (Swinnerton-Dyer; Haugland 1996) mu <= sup_x M(x) for every |
| 12 | admissible F: heights in [0, 1] and integral 1, i.e. sum_i f_i = n/2. |
| 13 | |
| 14 | Heights may be given with any positive sum: they are rescaled exactly to sum n/2 (as the |
| 15 | EinsteinArena board does), and the certificate is admissible only if every rescaled height lies |
| 16 | in [0, 1]. Everything below is exact integer arithmetic; no floating-point number enters. |
| 17 | Python >= 3.8, standard library only. |
| 18 | """ |
| 19 | |
| 20 | from __future__ import annotations |
| 21 | |
| 22 | import json |
| 23 | import sys |
| 24 | from fractions import Fraction |
| 25 | from math import gcd |
| 26 | from operator import mul |
| 27 | |
| 28 | MAX_CELLS = 16384 |
| 29 | DIGITS = 25 |
| 30 | |
| 31 | |
| 32 | class CertificateError(ValueError): |
| 33 | pass |
| 34 | |
| 35 | |
| 36 | def parse_height(value, denominator: int | None) -> Fraction: |
| 37 | """A height: a JSON number (a float is read as the exact binary value it denotes), an |
| 38 | integer numerator over the certificate's common `denominator`, or a string 'p/q'.""" |
| 39 | if isinstance(value, bool): |
| 40 | raise CertificateError("a height cannot be a boolean") |
| 41 | if isinstance(value, str): |
| 42 | try: |
| 43 | height = Fraction(value.strip()) |
| 44 | except (ValueError, ZeroDivisionError) as exc: |
| 45 | raise CertificateError(f"unreadable height {value!r}") from exc |
| 46 | elif isinstance(value, int): |
| 47 | height = Fraction(value) |
| 48 | elif isinstance(value, float): |
| 49 | if value != value or value in (float("inf"), float("-inf")): |
| 50 | raise CertificateError("a height is not finite") |
| 51 | height = Fraction(value) |
| 52 | else: |
| 53 | raise CertificateError(f"a height must be a number or a string, not {type(value).__name__}") |
| 54 | return height / denominator if denominator else height |
| 55 | |
| 56 | |
| 57 | def heights(certificate: dict) -> list[Fraction]: |
| 58 | values = certificate.get("values") |
| 59 | if not isinstance(values, list) or not values: |
| 60 | raise CertificateError('the certificate needs a nonempty list "values"') |
| 61 | if len(values) > MAX_CELLS: |
| 62 | raise CertificateError(f"at most {MAX_CELLS} cells are checked (got {len(values)})") |
| 63 | denominator = certificate.get("denominator") |
| 64 | if denominator is not None: |
| 65 | if isinstance(denominator, bool) or not isinstance(denominator, int) or denominator <= 0: |
| 66 | raise CertificateError('"denominator" must be a positive integer') |
| 67 | return [parse_height(value, denominator) for value in values] |
| 68 | |
| 69 | |
| 70 | def certify(certificate: dict) -> dict: |
| 71 | """The exact bound a certificate proves. Raises CertificateError if it is not admissible.""" |
| 72 | values = heights(certificate) |
| 73 | n = len(values) |
| 74 | common = 1 |
| 75 | for value in values: |
| 76 | common = common * value.denominator // gcd(common, value.denominator) |
| 77 | A = [int(value * common) for value in values] # integers, f_i = A_i / common |
| 78 | S = sum(A) |
| 79 | if S <= 0: |
| 80 | raise CertificateError("the heights must have a positive sum") |
| 81 | # rescaled heights f_i' = (n/2) A_i / S must lie in [0, 1]: 0 <= n A_i <= 2 S |
| 82 | low = [i for i, a in enumerate(A) if a < 0] |
| 83 | high = [i for i, a in enumerate(A) if n * a > 2 * S] |
| 84 | if low or high: |
| 85 | raise CertificateError(f"not admissible: after rescaling to sum n/2, {len(low)} heights are below 0 " |
| 86 | f"and {len(high)} above 1 (first cells {(low + high)[:5]})") |
| 87 | # f_i' g_{i+m}' = [n A_i / (2S)] [(2S - n A_{i+m}) / (2S)], and M(mh) = (2/n) sum_i f_i' g_{i+m}', |
| 88 | # so M(mh) = C_m / (2 S^2) with the integer C_m = sum_i A_i B_{i+m}, B_j = 2S - n A_j. |
| 89 | B = [2 * S - n * a for a in A] |
| 90 | best, lag = None, None |
| 91 | for m in range(-(n - 1), n): |
| 92 | lo, hi = max(0, -m), min(n, n - m) |
| 93 | c = sum(map(mul, A[lo:hi], B[lo + m:hi + m])) |
| 94 | if best is None or c > best: |
| 95 | best, lag = c, m |
| 96 | bound = Fraction(best, 2 * S * S) |
| 97 | return {"n": n, "bound": bound, "argmax_lag": lag, "x_star": Fraction(2 * lag, n), |
| 98 | "rescaled": S * 2 != n * common} |
| 99 | |
| 100 | |
| 101 | def decimal_up(value: Fraction, digits: int = DIGITS) -> str: |
| 102 | scaled = -((-value.numerator * 10 ** digits) // value.denominator) |
| 103 | text = str(scaled).rjust(digits + 1, "0") |
| 104 | return f"{text[:-digits]}.{text[-digits:]}" |
| 105 | |
| 106 | |
| 107 | def main(argv: list[str]) -> int: |
| 108 | if len(argv) != 2: |
| 109 | print("usage: verify_overlap.py CERTIFICATE.json", file=sys.stderr) |
| 110 | return 2 |
| 111 | with open(argv[1], encoding="utf-8") as handle: |
| 112 | result = certify(json.load(handle)) |
| 113 | print(json.dumps({"n": result["n"], "bound": f"{result['bound'].numerator}/{result['bound'].denominator}", |
| 114 | "bound_decimal_up": decimal_up(result["bound"]), "argmax_lag": result["argmax_lag"], |
| 115 | "x_star": str(result["x_star"]), "rescaled": result["rescaled"]}, indent=2)) |
| 116 | return 0 |
| 117 | |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | raise SystemExit(main(sys.argv)) |