"""Check finite residue models and reproduce the certificate by a second update.""" from fractions import Fraction from math import ceil, floor, isqrt, lcm import numpy as np def check_finite_orbits(): first_terms = sum(Fraction(1, 2**d - 1) for d in (4, 9, 25)) prefix = floor(2**40 * first_terms) assert prefix == floor(2**40 * (first_terms + Fraction(1, 2**47))) assert prefix == 75_452_494_113 assert f"{prefix:040b}"[-9:] == "100100001" print("Certified binary digits 32 through 40: 100100001") comparisons = 0 for base in (2, 3, 5): for weights in ({4: 1, 9: 1}, {4: 2, 6: -3, 9: 1}, {5: -1, 7: 2}): period = lcm(*weights) value = sum(Fraction(a, base**d - 1) for d, a in weights.items()) points = [] for n in range(period): uncarried = sum( Fraction(a * base ** (n % d), base**d - 1) for d, a in weights.items() ) assert (base**n * value - uncarried).denominator == 1 points.append(uncarried % 1) comparisons += 1 assert sum(floor(base * v) for v in points) == (base - 1) * sum(points) residues = {(n % 4, n % 9) for n in range(36)} assert len(residues) == 36 carries = [(r, s) for r, s in sorted(residues) if Fraction(2**r, 15) + Fraction(2**s, 511) >= 1] assert carries == [(3, 8)] print(f"Checked {comparisons} finite orbit positions and the carry in periods 4 and 9.") def check_enclosure(): width, unit, cutoff, tail_limit, band = 2**14, 2**46, 12_000, 500_000, 682 # This sieve marks composites, starting with an unmarked array. composite = bytearray(tail_limit + 1) for k in range(2, isqrt(tail_limit) + 1): if not composite[k]: composite[k * k::k] = bytes([1]) * len(range(k * k, tail_limit + 1, k)) counts = np.zeros(width, dtype=np.int64) counts[0] = unit omitted = 0 for p in range(2, tail_limit + 1): if composite[p]: continue d = p * p if p > cutoff: omitted += -(-unit // d) continue if d <= 14: offsets = [floor(Fraction(width * 2**r, 2**d - 1)) for r in range(d)] else: offsets = [width >> k for k in range(1, 15)] # Divide each input first, and carry the remainders through the # positive convolution. The remainder numerator stays below d*d. quotient, remainder = np.divmod(counts, d) multiplicity = d - len(offsets) whole = multiplicity * quotient residual = multiplicity * remainder for k in offsets: whole += np.roll(quotient, k) residual += np.roll(remainder, k) counts = whole + residual // d missing = 1 - Fraction(sum(map(int, counts)), unit) moment = Fraction(sum(j * int(c) for j, c in enumerate(counts)), width * unit) band_mass = Fraction(sum(map(int, counts[-band:])), unit) error = Fraction(1, 2 * width) + Fraction(omitted, unit) + Fraction(1, tail_limit) lower = moment - band_mass - missing - error * width / band upper = moment + missing + error bounds = floor(10**6 * lower), ceil(10**6 * upper) assert bounds == (383422, 385089) print(f"Quotient/remainder calculation in millionths: [{bounds[0]}, {bounds[1]}]") if __name__ == "__main__": check_finite_orbits() check_enclosure()