# seam.py -- job #4: Seam Lemma edge cases and extension, plus a small direct-gcd census. # Deterministic stdout (no timings). Python 3, standard library only. from math import gcd def primes_below(n): s = bytearray([1]) * n s[0:2] = b"\x00\x00" for i in range(2, int(n ** 0.5) + 1): if s[i]: s[i * i::i] = bytearray(len(s[i * i::i])) return [i for i in range(n) if s[i]] PR = primes_below(10000) print("A. Direct gcd census, every residue r mod p# tested by gcd(r(r+2), p#) = 1, T2..T17") print(" tile | width | D | prod(q-2), 2= 1000: break if p > 2: Pn = Pp * p edge = 0 for k in range(1, p + 1): a = (k * Pp - 1) % Pn if gcd(a, Pn) == 1 and gcd((a + 2) % Pn, Pn) == 1: edge += 1 nfold += 1 if edge != p - 2: departures.append((p, edge)) if p <= 97: print(f" fold by {p:3d}: P_prev bits {Pp.bit_length():4d}, seam survivors = {edge} (p-2 = {p - 2}) {'MATCH' if edge == p - 2 else 'DIFFERS'}") Pp *= p print(f" folds tested 3 <= p < 1000: {nfold}; departures from p-2: {departures if departures else 'none'}; P_prev at the last fold has {(Pp // 997).bit_length()} bits") print() print("E. Reduced test (only the mod-p condition; for q < p, k*P_prev -+ 1 = -+1 mod q is never struck), every prime 3 <= p < 10000") m = 1 # P_prev mod p is recomputed per p from the primes below it departures = [] nfold = 0 for i, p in enumerate(PR): if p == 2: continue m = 1 for q in PR[:i]: m = m * q % p edge = sum(1 for k in range(1, p + 1) if (k * m - 1) % p != 0 and (k * m + 1) % p != 0) nfold += 1 if edge != p - 2: departures.append((p, edge)) print(f" folds tested: {nfold} (p = 3 .. {PR[-1]}); departures from p-2: {departures if departures else 'none'}")