#!/usr/bin/env python3 """Construct and check the dyadic phase grid in Appendix A of Xuyang Peng's 'A density-one approximation to the Erdős–Graham two-set permutation problem'. Run with Python 3.8 or later. All arithmetic is integral or rational. """ import argparse from fractions import Fraction from itertools import combinations from math import ceil, floor def odd_part(n): """Remove all factors of two from a positive integer.""" return n // (n & -n) def has_progression(numerators): """Test the entire union of dyadic rays on a common denominator D. All input numerators are in [D, 2D). Normalize the largest term into this layer. For two phases with numerators a < b, the (middle, largest) numerators can be (a, b) or (b/2, a), forcing smallest numerator 2a-b or b-a. Both are positive. Equal middle and largest phases cannot give a strict progression. A forced numerator belongs to one of the rays exactly when its odd part matches that of a phase numerator. """ odd_parts = {odd_part(n) for n in numerators} for a, b in combinations(sorted(numerators), 2): if odd_part(2 * a - b) in odd_parts or odd_part(b - a) in odd_parts: return True return False def choose_cut(target, radius, denominator, numerators): """Select the least admissible grid point in the closed search interval.""" first = ceil((target - radius) * denominator) last = floor((target + radius) * denominator) for n in range(first, last + 1): if not has_progression(numerators | {n}): numerators.add(n) return Fraction(n, denominator), n - first raise ValueError(f'no admissible cut within {radius} of {target}') def construct_grid(rounds): endpoints = [Fraction(1), Fraction(2)] previous_middle = None previous_variation = Fraction(0) print('round rays rejected candidates') for r in range(1, rounds + 1): denominator = 2 ** (16 * r + 32) epsilon = Fraction(1, 1024 * 3 ** (2 * r)) # The right endpoint 2 belongs to the ray of 1. Earlier denominators divide D. numerators = {p.numerator * (denominator // p.denominator) for p in endpoints[:-1]} refined = [endpoints[0]] rejections = 0 for left, right in zip(endpoints, endpoints[1:]): third = (right - left) / 3 radius = epsilon * (right - left) u, skipped_u = choose_cut(left + third, radius, denominator, numerators) v, skipped_v = choose_cut(right - third, radius, denominator, numerators) rejections += skipped_u + skipped_v refined.extend((u, v, right)) if previous_middle is not None: old_left, old_right = previous_middle gap = max(u - old_right, old_left - v) if 100 * len(numerators) * gap <= 1: raise ValueError(f'round {r}: consecutive middle children too close at {u}') previous_middle = (u, v) widths = [b - a for a, b in zip(refined, refined[1:])] count = 3 ** r if len(numerators) != count or len(widths) != count: raise ValueError(f'round {r}: expected {count} leaves and rays') if min(widths) < Fraction(1, 2 * count) or max(widths) > Fraction(2, count): raise ValueError(f'round {r}: leaf width outside the bounds of Appendix A') variation = sum(abs(b - a) for a, b in zip(widths, widths[1:])) if variation > previous_variation / 3 + 8 * epsilon: raise ValueError(f'round {r}: adjacent-width variation exceeds the recurrence bound') print(f'{r:5d} {count:4d} {rejections:19d}') endpoints = refined previous_variation = variation return endpoints[:-1] def main(): parser = argparse.ArgumentParser(description='Construct and verify the phase grid of Appendix A.') parser.add_argument('--rounds', type=int, default=5, help='number of complete rounds (default: 5)') parser.add_argument('--show-phases', action='store_true', help='list the final phases as exact rationals') args = parser.parse_args() if args.rounds < 1: parser.error('--rounds must be positive') phases = construct_grid(args.rounds) if args.show_phases: print('phases in [1, 2):') for phase in phases: print(phase) if __name__ == '__main__': main()