"""2n points of the n x n grid with no three on a line, for n = 14: two in every row, the most there can be. Found here by a depth-first search over sets with quarter-turn symmetry (each choice adds a point and its three rotations about the grid's centre), filling the rows in order and blocking every grid point on a line through two chosen ones. Checked below by testing all triples. """ import math from itertools import combinations from pathlib import Path import kit N = 14 def search(n): block = [[0] * n for _ in range(n)] # how many lines through two chosen points pass through each grid point rows, cols, pts = [0] * n, [0] * n, [] def mark(a, p, d): dx, dy = p[0] - a[0], p[1] - a[1] g = math.gcd(dx, dy); dx //= g; dy //= g x, y = a while 0 <= x < n and 0 <= y < n: x, y = x - dx, y - dy x, y = x + dx, y + dy while 0 <= x < n and 0 <= y < n: block[y][x] += d; x, y = x + dx, y + dy def add(p): for a in pts: mark(a, p, 1) pts.append(p); rows[p[1]] += 1; cols[p[0]] += 1 def remove(): p = pts.pop(); rows[p[1]] -= 1; cols[p[0]] -= 1 for a in pts: mark(a, p, -1) def fill(start): row = next((y for y in range(n) if rows[y] < 2), None) if row is None: return True for x in range(start if rows[row] else 0, n): m = n - 1 k, ok = 0, True for q in sorted({(x, row), (m - row, x), (m - x, m - row), (row, m - x)}): if block[q[1]][q[0]] or q in pts or rows[q[1]] == 2 or cols[q[0]] == 2: ok = False break add(q); k += 1 if ok and fill(x + 1 if rows[row] == 1 else 0): return True for _ in range(k): remove() return False assert fill(0) return sorted(pts) P = search(N) assert len(P) == 2 * N and len(set(P)) == 2 * N assert all((b[0] - a[0]) * (c[1] - a[1]) != (c[0] - a[0]) * (b[1] - a[1]) for a, b, c in combinations(P, 3)) STEP = 18.6 X0, Y0 = (kit.W - STEP * (N - 1)) / 2, 12 at = lambda x, y: (X0 + x * STEP, Y0 + y * STEP) body = ''.join(kit.circle(*at(x, y), 2, 'faint') for x in range(N) for y in range(N)) body += ''.join(kit.dot(*at(x, y), r=6.5, cls='f') for x, y in P) kit.write(__file__, 'mathematics', body, label=f'n = {N} ยท {len(P)} points, no three on a line')