Combinatorics · Computational search

Four new terms of an integer sequence

How small can a patch of grid squares be if every pair of n colors has to touch somewhere? I helped push the answer from n = 18 to n = 22 in the On-Line Encyclopedia of Integer Sequences.

Problem
Peter Kagey’s Square Game
Collaborator
Ryan Lee
When
March – October 2026
Stack
C++17, OpenMP, CUDA, SLURM, Python, SAT

Overview

OEIS A278299 asks for the fewest grid squares you need to color with n colors so that every color sits edge-to-edge with every other color at least once. The answers are hard to pin down: until 2026, only a(2) through a(11) were known exactly. This page is about the part I worked on.

New terms
4a(19)–a(22), March and October 2026
Proved by my SAT search
a(21) = 116Previously only 116 ≤ a(21) ≤ 117
Also settled
a(22) = 132Example coloring meets the lower bound
Bounds posted
a(27) ≤ 195Plus bounds for a(23)–a(26)

01 The problem

Make every color touch every other color

Take n colors, numbered 1 to n. Color some connected squares of graph paper (a polyomino) so that, for every pair of different colors, at least one square of the first shares an edge with a square of the second. Corners don’t count. a(n) is the smallest number of squares that can do it.

For four colors there are six pairs to cover, and six squares are enough: in the shape on the right, the top 1 touches 4 and 3, that 4 touches 2, the left 4 touches 3, 3 touches 2, and 2 touches the bottom 1. Five squares can’t do it, so a(4) = 6.

Kagey’s Square Game turns this into a puzzle: you click numbers onto a grid, it marks pairs that repeat in red, and a valid grid you submit becomes an upper bound for the sequence. Kagey traces the idea to a ≠ b ≠ c ≠ a written on the board in a linear algebra class; the one-dimensional version is a solved problem, and Alec Jones suggested the two-dimensional one.

Polyomino for a(4) = 6 Six squares in three rows. Top row, columns 2 and 3: 1, 4. Middle row: 4, 3, 2. Bottom row, column 3: 1. Every pair of the colors 1 to 4 shares an edge. 1 4 4 3 2 1
Fig. 1The a(4) = 6 example from the OEIS entry. All six pairs of colors share an edge.

02 Why it’s hard

A huge search with a bound to beat

Two things have to be chosen at once: the shape, and the color of every square. For n = 22 and a fixed 132-square shape there are 22132, about 10177, ways to color it, and the number of possible shapes grows exponentially too. Nobody checks those one by one.

What makes progress possible is a lower bound. Peter Exley gave the one in the entry:

a(n) ≥ max( ⌊(n2 + n + 5)/4⌋, n·⌈(n − 1)/4⌉ )

Edge bound

Each of the n(n−1)/2 pairs needs its own shared edge. A polyomino with k squares and perimeter P has (4k − P)/2 internal edges, and compact shapes have the smallest perimeter, so k can’t be too small. The difference between internal edges and pairs is the slack: how many edges can be wasted on repeats.

Contact bound

Each color has to touch n − 1 others, and a square has at most four neighbors, so every color needs at least ⌈(n − 1)/4⌉ squares. For n = 22 that is 6 squares per color, 132 in all.

A lower bound settles the “nothing smaller works” half of a proof. If you can then show one coloring that hits the bound exactly, the value is proved. The hard part is finding that coloring when the slack is close to zero, or proving none exists.

03 Approach 1

Parallel simulated annealing

My first tool was a heuristic search, written in C++17 with OpenMP. It works on a score: the number of color pairs that don’t touch yet. Zero means a valid coloring.

  1. Start random. Grow a random connected shape of k squares and fill it with every color at least once plus random extras.
  2. Make a small change. Move one square to another spot on the edge of the shape, swap the colors of two squares, or recolor one square.
  3. Accept or undo. Keep any change that doesn’t make the score worse. Keep a worse one with probability e−Δ/T, where the temperature T cools geometrically from 5 to 0.01, so the search can climb out of dead ends early and settles down later.
  4. Run many at once. Each thread runs its own independent attempts under dynamic scheduling. The first thread to reach zero sets an atomic flag and the rest stop; if none succeeds, k goes up by one.

A second version replaced the hash map with a flat grid array, updated the score incrementally instead of recounting every edge, kept shapes connected when squares move, and added parallel tempering, where threads at different temperatures swap configurations. I then wrote a CUDA annealer that gives each candidate shape its own GPU thread block, with the threads counting edges cooperatively.

The runs went to the BYU supercomputer as SLURM batch jobs. The job script rebuilds the binary whenever it lands on a node with a different CPU instruction set, since it is compiled with -march=native.

04 Approach 2

Exact search with a SAT solver

A SAT solver takes a long list of true/false constraints and either finds values that satisfy all of them or proves that none exist. To use one here, I fix a candidate shape and translate the question into Boolean variables that say which color each square gets, with clauses saying that each square gets exactly one color and that every pair of colors meets across at least one shared edge. Candidate shapes are enumerated by perimeter, which the edge bound limits.

The search for n = 21 at 116 squares came back satisfiable: the solver returned a complete coloring. Since 116 is the lower bound for n = 21, that one example proves a(21) = 116. The same work settled a(22) = 132, which is exactly the contact bound, with the coloring in Fig. 2. Each returned coloring is checked by a separate verifier that shares no code with the encoder. Ryan Lee’s SAT search for a(20) = 106 worked the same way: the example it returned meets the bound.

I built the SAT tooling in Python with the CaDiCaL solver, with help from Claude Code.

05 Results

The new terms and bounds

Values and bounds as listed in OEIS A278299. The lower bound is Exley’s formula. Upper bounds for n ≥ 23 come from explicit colorings.
na(n)Lower boundStatusCredit in the entry
199696Proved; example meets the edge boundNels Buhrley and Ryan Lee
20106106Proved; Ryan Lee’s SAT searchNels Buhrley and Ryan Lee
21116116Proved; my SAT searchNels Buhrley
22132132Proved; example meets the contact boundNels Buhrley
23139–141139OpenBounds: Nels Buhrley
24151–154151OpenBounds: Nels Buhrley
25163–167163OpenBounds: Nels Buhrley; 167 by Dmitry Kamenetsky
26182182In comments; meets the contact boundNels Buhrley
27190–195190OpenBounds: Nels Buhrley

The data section of the entry stops at a(22) because a(23) is still open; a(26) is recorded in the comments.

Coloring for a(22) = 132 A 12 by 11 rectangle of 132 squares colored with 22 colors, each used exactly 6 times, so that every pair of colors shares an edge. The full grid is listed in the table below the figure. 15 20 6 1 13 19 11 2 4 6 5 17 18 11 21 4 17 3 5 20 10 18 8 10 2 15 9 11 6 22 19 1 12 5 16 4 3 13 7 8 13 9 16 21 3 7 15 5 6 14 11 3 4 19 6 2 16 18 3 14 9 8 22 20 8 2 10 19 12 14 10 9 20 17 11 16 10 5 1 8 6 15 21 17 19 15 1 17 22 21 18 20 7 10 13 12 18 4 9 2 7 19 17 14 12 11 22 18 13 20 12 22 4 14 7 1 2 13 5 9 7 21 8 15 12 21 16 22 14 16 1 3
Fig. 2 as a table
The a(22) = 132 coloring, row by row (12 columns).
Row 1152061131911246517
Row 218112141735201018810
Row 3215911622191125164
Row 431378139162137155
Row 5614113419621618314
Row 69822208210191214109
Row 720171116105186152117
Row 81915117222118207101312
Row 918492719171412112218
Row 10132012224147121359
Row 1172181512211622141613
Fig. 2The a(22) = 132 coloring from the OEIS entry, drawn from its text diagram: a 12 × 11 rectangle in which each of the 22 colors appears exactly 6 times. It has 241 internal edges for 231 pairs, so only 10 edges are spent on repeated pairs.
Coloring for a(19) = 96 A 12 by 8 rectangle of 96 squares colored with 19 colors so that every pair of colors shares an edge. The full grid is listed in the table below the figure. 12 1 6 2 18 11 17 18 9 13 16 18 19 11 3 17 6 10 12 13 6 19 9 10 7 9 2 7 4 14 18 7 12 14 3 19 10 17 16 8 3 5 19 1 9 15 10 8 2 14 7 6 15 1 17 13 14 8 4 9 4 11 15 16 19 4 5 10 1 2 13 5 17 8 18 5 2 15 12 16 3 12 11 6 15 13 3 7 11 5 8 1 18 4 16 14
Fig. 3 as a table
The a(19) = 96 coloring, row by row (12 columns).
Row 112162181117189131618
Row 219113176101213619910
Row 379274141871214319
Row 4101716835191915108
Row 521476151171314849
Row 6411151619451012135
Row 71781852151216312116
Row 8151337115811841614
Fig. 3The a(19) = 96 coloring by Ryan Lee and me: a 12 × 8 rectangle with 172 internal edges for 171 pairs. Exactly one pair, 3 and 18, touches twice; every other edge covers a new pair.

06 Takeaways

What I learned, and the limits

  • Pick the tool by the slack. When a size leaves plenty of room for repeated pairs, annealing finds colorings quickly. When the slack is near zero (the a(19) coloring has exactly one spare edge), an exact search is the better bet.
  • A bound turns an example into a proof. All four new values sit exactly on Exley’s lower bound, so each one is proved by a single verified coloring. That is also the limit of what I have shown: none of these proofs needed the solver to rule a size out.
  • The open cases are harder. For a(23), a(24), a(25) and a(27) the gap between bounds is 2 to 5 squares. Closing it means either a coloring on the bound or proving that every shape of that size fails, which is far more expensive.
  • Check results independently. A verifier that shares no code with the search is cheap insurance before submitting anything to the OEIS.
  • C++17
  • OpenMP
  • CUDA
  • SLURM
  • Python
  • SAT (CaDiCaL)
  • Simulated annealing
  • Claude Code