#!/usr/bin/env python3 """0002 probe 1 -- the emergent co-rotation phase of the Ulam spiral. Two Ulam spirals of OPPOSITE handedness: S+ : the standard counter-clockwise square spiral, n -> z_+(n) in Z[i]. S- : its mirror S-(n) = conj(z_+(n)), i.e. the clockwise spiral. For a twin pair (n, n+2) the two members sit ONE STEP apart along the arm, so the relevant observable is the CO-ROTATION phase Phi(n) = arg z_+(n) + arg z_-(n+2) (sum of phases) Psi(n) = arg z_+(n+2) - arg z_+(n) (increment) This script checks, exactly (integers only, plus phase for output): (L1) the ring geometry: ring k is the axis-aligned square |x|,|y| <= k, k>=1; (L2) Psi(n) is NON-CONSTANT and n-dependent (the offset point crosses the corner of its ring; a modelled conical helix would have constant Psi = w*2); (L3) the twin pair (n,n+2) is a pair of CONSECUTIVE ring indices or the same ring, and the pair is collinear with the origin IFF the corner-crossing is exact; (L4) Phi(n) is NOT a function of n mod m for the small moduli m that the classical wheel sieve uses -- so it is not killed by the route-61 finite-modulus obstruction (return #893), which only excludes functions of n mod lcm(g). """ import json, math from math import gcd, atan2, pi def ulam(n): """Standard CCW Ulam spiral: 1 -> (0,0), 2 -> (1,0), 3 -> (1,1), 4 -> (0,1), ...""" if n == 1: return (0, 0) k = (math.isqrt(n - 1) + 1) // 2 # (2k-1)^2 < n <= (2k+1)^2 m = n - (2 * k - 1) ** 2 # 1 <= m <= (2k+1)^2-(2k-1)^2 = 8k if m <= 2 * k: # right side, going up: (k, -(k-1) + (m-1)) return (k, m - k) elif m <= 4 * k: # top side, going left return (3 * k - m, k) elif m <= 6 * k: # left side, going down return (-k, 5 * k - m) else: # bottom side, going right return (m - 7 * k, -k) def ring(n): if n == 1: return 0 return math.isqrt(n - 1 - 1) + 1 if False else max(abs(c) for c in ulam(n)) def check_ring(n): x, y = ulam(n) return max(abs(x), abs(y)) def main(): N = 4000 # (L1) ring = max(|x|,|y|) == the spiral index k from the construction # recompute k independently from the formula-free definition: k = min r s.t. n <= (2r+1)^2 L1 = True for n in range(1, N + 1): x, y = ulam(n) k = 1 while (2 * k + 1) ** 2 < n: k += 1 # ring 0 only for n=1 expect = 0 if n == 1 else k if max(abs(x), abs(y)) != expect: L1 = False print("L1 fail", n, ulam(n), expect) break # (L2) Psi(n) = arg z(n+2) - arg z(n): non-constant, n-dependent def phase(n): x, y = ulam(n) return atan2(y, x) def dphase(n): d = phase(n + 2) - phase(n) while d > pi: d -= 2 * pi while d <= -pi: d += 2 * pi return d psis = [dphase(n) for n in range(1, N - 2) if n % 2 == 1] uniq = len(set(round(p, 9) for p in psis)) # a modelled conical helix z = r e^{i w n} has dphase == 2w constant is_const = uniq <= 2 # (L3) twin pairs: (n, n+2) ring indices def isprime(n): if n < 2: return False if n % 2 == 0: return n == 2 i = 3 while i * i <= n: if n % i == 0: return False i += 2 return True twins = [(n, n + 2) for n in range(3, N - 1) if isprime(n) and isprime(n + 2)] ringdiffs = {} collinear = 0 for n, n2 in twins: x1, y1 = ulam(n); x2, y2 = ulam(n2) d = check_ring(n2) - check_ring(n) ringdiffs[d] = ringdiffs.get(d, 0) + 1 if x1 * y2 - y1 * x2 == 0: collinear += 1 # (L4) is Phi(n) a function of n mod m? Test the small moduli of the wheel. def Phi(n): x1, y1 = ulam(n); x2, y2 = ulam(n + 2) return math.atan2(y1, x1) - math.atan2(y2, x2) # = phase(n) - phase(n+2) nonperiodic = {} for m in [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 30, 210]: table = {} bad = 0 for n in range(1, N - 2): r = n % m v = Phi(n) if r in table: if abs(table[r] - v) > 1e-9: bad += 1 else: table[r] = v nonperiodic[str(m)] = {"distinct_residue_values_mismatched": bad, "is_function_of_mod_m": bad == 0} out = { "L1_ring_is_max_abs_coord": L1, "L2_psi_distinct_values_over_odd_n": uniq, "L2_psi_is_constant_like_a_modelled_helix": is_const, "L3_twin_ring_index_difference_histogram": dict(sorted(ringdiffs.items())), "L3_twin_pairs_collinear_with_origin": collinear, "L3_twin_pairs_total": len(twins), "L4_Phi_is_a_function_of_n_mod_m": nonperiodic, } print(json.dumps(out, indent=2)) with open("ulam_phase.json", "w") as f: json.dump(out, f, indent=2) if __name__ == "__main__": main()