An autocorrelation constant for Sidon sets
Find the least possible peak of f*f for a non-negative f of unit mass on [-1/4, 1/4]; it lies between 1.2802 and 1.50286.
| 1 | #!/usr/bin/env python3 |
| 2 | """Checks that the candidates in a folder follow the submission format in README.md: file names, JSON shape, types, |
| 3 | lengths and ranges, and the size limit. It computes no ratio. |
| 4 | |
| 5 | usage: python3 tools/check_format.py routes/<branch> (or one .json file) |
| 6 | """ |
| 7 | import json |
| 8 | import math |
| 9 | import pathlib |
| 10 | import sys |
| 11 | from fractions import Fraction |
| 12 | |
| 13 | MAX_STEPS = 131072 |
| 14 | MAX_BITS = 1 << 24 |
| 15 | |
| 16 | |
| 17 | def errors(candidate): |
| 18 | values = candidate.get("heights") |
| 19 | if not isinstance(values, list) or not 1 <= len(values) <= MAX_STEPS: |
| 20 | return [f'"heights" must be a list of 1 to {MAX_STEPS} heights'] |
| 21 | out, hs = [], [] |
| 22 | for i, v in enumerate(values): |
| 23 | if isinstance(v, bool) or not isinstance(v, (int, float, str)) or (isinstance(v, float) and not math.isfinite(v)): |
| 24 | out.append(f"height {i}: {v!r} is not a finite number or a string") |
| 25 | continue |
| 26 | try: |
| 27 | x = Fraction(v) |
| 28 | except (ValueError, ZeroDivisionError): |
| 29 | out.append(f"height {i}: unreadable {v!r}") |
| 30 | continue |
| 31 | if x < 0: |
| 32 | out.append(f"height {i}: {v} is negative") |
| 33 | hs.append(x) |
| 34 | if out: |
| 35 | return out |
| 36 | if not any(hs): |
| 37 | return ["all heights are 0"] |
| 38 | q = math.lcm(*(x.denominator for x in hs)) |
| 39 | bits = max(x.numerator * (q // x.denominator) for x in hs).bit_length() |
| 40 | if len(hs) * bits > MAX_BITS: |
| 41 | return [f"too fine: {len(hs)} heights of {bits} bits over their common denominator; N times the bits may be at most {MAX_BITS}"] |
| 42 | return [] |
| 43 | |
| 44 | |
| 45 | if len(sys.argv) != 2: |
| 46 | sys.exit(__doc__) |
| 47 | target = pathlib.Path(sys.argv[1]) |
| 48 | if target.is_file() and target.suffix != ".json": |
| 49 | sys.exit(f"{target}: candidates are .json files") |
| 50 | files = [target] if target.is_file() else sorted(target.rglob("*.json")) |
| 51 | found = bad = 0 |
| 52 | for path in files: |
| 53 | if path.stat().st_size > 1_000_000: |
| 54 | print(f"{path}: over 1 MB") |
| 55 | bad += 1 |
| 56 | continue |
| 57 | try: |
| 58 | candidate = json.loads(path.read_text()) |
| 59 | except Exception as exc: |
| 60 | print(f"{path}: not valid JSON ({exc})") |
| 61 | bad += 1 |
| 62 | continue |
| 63 | if not isinstance(candidate, dict) or "heights" not in candidate: |
| 64 | print(f'{path}: not a candidate (no "heights" key); ignored') |
| 65 | continue |
| 66 | found += 1 |
| 67 | problems = errors(candidate) |
| 68 | print(f"{path}: {'; '.join(problems[:5])}" if problems else f"{path}: ok ({len(candidate['heights'])} steps)") |
| 69 | bad += bool(problems) |
| 70 | print(f"{found} candidates, {bad} files with format errors") |
| 71 | sys.exit(1 if bad else 0) |
