#!/usr/bin/env python3 """0002 probe 2 -- the emergent twin geometry on the Ulam spiral, exactly. Ring k is the axis-aligned square max(|x|,|y|) = k and contains the integers (2k-1)^2+1 .. (2k+1)^2 (ring 0 is n=1). Each ring has 8k positions, 4 corners, and 4k-4 non-corner boundary points. Consecutive integers n, n+1 lie on the SAME EDGE of the ring unless n+1 is a lattice point (a corner) OR n is the midpoint of a side / n+1 is the midpoint of a side -- i.e. unless n+1 is one of the 4k "special" points. This script verifies: (E1) the same-ring edge crossings of consecutive integers are exactly the n with n+1 a "special" point (corner or side midpoint) of the spiral; (E2) no twin pair (n, n+2) is collinear with the origin: a twin chord is never radial (the midpoint n+1 is never on the spiral), and the angle Psi(n) empirically never vanishes; (E3) on a straight edge the chord of length 2 subtends |sin Psi| = 2/R (checked against the exact integer coordinates), while at a crossing Psi jumps -- so Psi is n-dependent and NOT a constant helix rate; (E4) the number of crossings up to N grows like ~N/2 - O(sqrt N), i.e. the spiral has NO uniform winding rate (a modelled helix has zero crossings). """ import json, math from math import atan2, pi def ulam(n): if n == 1: return (0, 0) k = (math.isqrt(n - 1) + 1) // 2 m = n - (2 * k - 1) ** 2 if m <= 2 * k: return (k, m - k) elif m <= 4 * k: return (3 * k - m, k) elif m <= 6 * k: return (-k, 5 * k - m) else: return (m - 7 * k, -k) def is_corner(n): x, y = ulam(n) return max(abs(x), abs(y)) >= 1 and abs(x) == abs(y) def is_special(n): """n is a lattice point that breaks the straight run of its edge: a corner, or the midpoint of a side.""" x, y = ulam(n) k = max(abs(x), abs(y)) if k == 0: return True if is_corner(n): return True # midpoint of the right/left side (x = +-k, y = 0) or top/bottom (y = +-k, x = 0) return (abs(x) == k and abs(y) == 1 and (k % 2 == 0)) or \ (abs(y) == k and abs(x) == 1 and (k % 2 == 0)) def edge(n): x, y = ulam(n) k = max(abs(x), abs(y)) if k == 0: return (0, 0) if x == k and y + k < 2 * k: return (k, 0) if y == k and k - x < 2 * k: return (k, 1) if x == -k and k - y < 2 * k: return (k, 2) return (k, 3) 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 def main(): N = 200000 # (E1) crossing characterisation crossings, predicted = [], [] for n in range(2, N): k, e = edge(n); k2, e2 = edge(n + 1) if k == k2 and e != e2: crossings.append(n) if is_special(n + 1): predicted.append(n) e1_ok = crossings == predicted # (E2) twin chords are never radial twins = [(n, n + 2) for n in range(3, N) if isprime(n) and isprime(n + 2)] def cross(n): x1, y1 = ulam(n); x2, y2 = ulam(n + 2) return x1 * y2 - y1 * x2 collinear = sum(1 for n, _ in twins if cross(n) == 0) def Psi(n): x1, y1 = ulam(n); x2, y2 = ulam(n + 2) d = atan2(y2, x2) - atan2(y1, x1) while d > pi: d -= 2 * pi while d <= -pi: d += 2 * pi return d min_abs_psi = min(abs(Psi(n)) for n, _ in twins) # (E3) straight-edge chord law |sin Psi| = 2/R devs, jump = [], [] for n in range(2, 30000): k, e = edge(n); k2, e2 = edge(n + 2) x1, y1 = ulam(n) R = math.hypot(x1, y1) if k == k2 and e == e2: devs.append(abs(abs(math.sin(Psi(n))) - 2.0 / R)) else: jump.append(abs(math.sin(Psi(n))) - 2.0 / R) e3_max_dev = max(devs) if devs else None out = { "E1_crossings_equal_special_points": e1_ok, "E1_crossings_count": len(crossings), "E1_first_crossings": crossings[:16], "E2_twin_pairs_total": len(twins), "E2_collinear_twin_pairs": collinear, "E2_min_abs_Psi_over_twins": min_abs_psi, "E3_max_dev_straight_edges": e3_max_dev, "E3_max_dev_at_crossings": max(jump) if jump else None, "E4_crossing_count_over_N": len(crossings) / N, } print(json.dumps(out, indent=2)) with open("ulam_edges.json", "w") as f: json.dump(out, f, indent=2) if __name__ == "__main__": main()