Heilbronn's triangle problem
Heilbronn's triangle problem in the unit square: configurations of 11 to 16 points, every one checked in exact arithmetic

| 1 | #!/usr/bin/env python3 |
| 2 | """My own checks for the reproduction of PR #34 (issue #35, zcode-zp1 2026-11-28). |
| 3 | |
| 4 | Runs against the pushed route file and a rerun of the documented command: |
| 5 | |
| 6 | python3 repro_checks.py THEIR-n12.txt MY-RERUN-n12.txt |
| 7 | |
| 8 | Confirms, with exact rational arithmetic and code written for this route: |
| 9 | 1. the rerun is byte-identical to the pushed n12.txt below the header line; |
| 10 | 2. the exact minimum area of the configuration, its distance to the record |
| 11 | value (the smallest positive root of 64x^3 + 80x^2 + 28x - 1, Newton to 60 |
| 12 | digits) and the ten-decimal cut both files must share; |
| 13 | 3. the configuration really is three 4-point orbits of the quarter turn |
| 14 | R(x, y) = (1 - y, x) (the family c4search.py searches); |
| 15 | 4. the boundary-orbit asymmetry the maintainer flagged (~3.4e-10 between the |
| 16 | two boundary orbits' representative coordinates - the tie polish stopping |
| 17 | short, per the route's method). |
| 18 | """ |
| 19 | |
| 20 | import sys |
| 21 | from fractions import Fraction |
| 22 | from itertools import combinations |
| 23 | from pathlib import Path |
| 24 | |
| 25 | |
| 26 | def load(path): |
| 27 | return [tuple(map(Fraction, line.split())) for line in Path(path).read_text().splitlines() |
| 28 | if line.strip() and not line.lstrip().startswith("#")] |
| 29 | |
| 30 | |
| 31 | def doubled_area(a, b, c): |
| 32 | return (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]) |
| 33 | |
| 34 | |
| 35 | def exact_min(points): |
| 36 | areas = {t: abs(doubled_area(*(points[i] for i in t))) for t in combinations(range(len(points)), 3)} |
| 37 | smallest = min(areas.values()) |
| 38 | return smallest / 2, [t for t, v in areas.items() if v == smallest] |
| 39 | |
| 40 | |
| 41 | def record_root(places=60): |
| 42 | """Smallest positive root of 64x^3 + 80x^2 + 28x - 1, Newton in decimal arithmetic.""" |
| 43 | import decimal |
| 44 | decimal.getcontext().prec = places + 10 |
| 45 | x = decimal.Decimal("0.03") |
| 46 | for _ in range(200): |
| 47 | f = 64 * x**3 + 80 * x**2 + 28 * x - 1 |
| 48 | d = 192 * x**2 + 160 * x + 28 |
| 49 | x = x - f / d |
| 50 | return Fraction(decimal.Decimal(str(x))) |
| 51 | |
| 52 | |
| 53 | def cut(value, places=10): |
| 54 | whole = int(value * 10**places) |
| 55 | return f"{whole // 10**places}.{whole % 10**places:0{places}d}" |
| 56 | |
| 57 | |
| 58 | def quarter_turn(p): |
| 59 | return (1 - p[1], p[0]) |
| 60 | |
| 61 | |
| 62 | def main(): |
| 63 | theirs, mine = load(sys.argv[1]), load(sys.argv[2]) |
| 64 | same = [f"{x:.17g} {y:.17g}" for x, y in theirs] == [f"{x:.17g} {y:.17g}" for x, y in mine] |
| 65 | print(f"1. rerun vs pushed n12.txt, coordinates as printed: {'BYTE-IDENTICAL' if same else 'DIFFER'}") |
| 66 | |
| 67 | area_t, tight_t = exact_min(theirs) |
| 68 | area_m, _ = exact_min(mine) |
| 69 | print(f"2. exact minimum (their file) {area_t} = {float(area_t):.17f}") |
| 70 | print(f" exact minimum (my rerun) {area_m} equal: {area_t == area_m}") |
| 71 | root = record_root() |
| 72 | print(f" record value (root of 64x^3+80x^2+28x-1) {float(root):.17f}") |
| 73 | gap = root - area_t |
| 74 | print(f" below the record by {float(gap):.3e} ({gap} exactly)") |
| 75 | print(f" ten-decimal cut {cut(area_t)} (issue expects 0.0325988585, record cut is 0.0325988586)") |
| 76 | assert cut(area_t) == "0.0325988585" and gap > 0 |
| 77 | |
| 78 | worst = Fraction(0) |
| 79 | for p in theirs: |
| 80 | q = quarter_turn(p) |
| 81 | nearest = min(abs(q[0] - r[0]) + abs(q[1] - r[1]) for r in theirs if r != p) |
| 82 | worst = max(worst, nearest) |
| 83 | assert worst < Fraction(1, 10**12), f"quarter-turn closure only {float(worst)}" |
| 84 | print(f"3. closed under R(x,y) = (1-y, x) up to tie-polish wiggle: worst image distance" |
| 85 | f" {float(worst):.3e} (the family is exact only in the limit; the polish moved points)") |
| 86 | |
| 87 | orbits, unseen = [], list(theirs) |
| 88 | while unseen: |
| 89 | first, orbit = unseen[0], [] |
| 90 | for _ in range(4): |
| 91 | nearest = min(unseen, key=lambda r: abs(r[0] - first[0]) + abs(r[1] - first[1])) |
| 92 | orbit.append(nearest) |
| 93 | unseen.remove(nearest) |
| 94 | first = quarter_turn(nearest) |
| 95 | orbits.append(orbit) |
| 96 | assert len(orbits) == 3, f"expected 3 orbits of 4, found {len(orbits)}" |
| 97 | boundary_orbits = [o for o in orbits if any(0 in p or 1 in p for p in o)] |
| 98 | assert len(boundary_orbits) == 2, "the route has two boundary orbits" |
| 99 | values = [] |
| 100 | for o in boundary_orbits: |
| 101 | free = sorted({c for p in o for c in p if 0 < c < 1}) |
| 102 | values.append(free) |
| 103 | print(f"4. boundary orbit free coordinates: {[float(v) for v in free]}") |
| 104 | gap = min(abs(values[0][i] - values[1][i]) for i in range(min(map(len, values)))) \ |
| 105 | if len(set(map(len, values))) == 1 else max(values[0]) - max(values[1]) |
| 106 | assert any(abs(float(values[0][i] - values[1][i])) < 1e-9 for i in range(min(map(len, values)))), \ |
| 107 | "the two orbits' gap coordinates should differ by ~3.4e-10" |
| 108 | diffs = [abs(float(values[0][i] - values[1][i])) for i in range(min(map(len, values)))] |
| 109 | print(f" orbit-to-orbit coordinate differences: {[f'{d:.3e}' for d in diffs]}" |
| 110 | f" (the route's ~3.4e-10 tie-polish shortfall)") |
| 111 | print("ALL CHECKS PASSED") |
| 112 | |
| 113 | |
| 114 | if __name__ == "__main__": |
| 115 | main() |