What is so special about the number 19?

The question came up last month in a conversation among YSDA alumni, when the school turned 19. Someone pointed out that 19 is a twin prime. Someone else replied that 19 is the number of cells in the only non-trivial normal magic hexagon.

Wait, what is a “magic hexagon”? Let us start there.

Magic Squares and Magic Hexagons

You probably know about magic squares. A magic square is a square grid of numbers in which every row, every column, and both main diagonals add up to the same total, known as the magic constant. We also usually require the numbers to be consecutive - typically from $1$ to $n^2$ - and call it a normal magic square. Otherwise, we could simply put the same number into every cell, which would be a very boring way to fill a square.

Magic squares have been known for millennia and are now very well understood. We have algorithms for constructing normal magic squares of every order $n>2$.

A magic hexagon applies the same idea to a hexagonal grid. Its cells form straight lines in three directions, and every such line must have the same sum. As with squares, a magic hexagon is called normal if it contains the consecutive numbers from $1$ to $3n^2-3n+1$, the total number of cells in a hexagon of order $n$.

Above you can see the only non-trivial normal magic hexagon in existence - apart from its own rotations and reflections. The proof is straightforward. In each of the three directions, the cells are partitioned into $2n-1$ lines. Therefore, the sum of all the numbers must be divisible by $2n-1$. For every order $n>3$, the sum of the numbers from $1$ to $3n^2-3n+1$ fails this divisibility test.

Well, ending the story here would be no fun. To make things more interesting, let’s look at the so-called abnormal magic hexagons. Here we relax one constraint: the numbers on the grid must still be consecutive, but they no longer have to start at $1$.

This small relaxation suddenly allows new solutions to appear.

Finding them, however, is not easy. Unlike with magic squares, there was no formulaic construction or deterministic algorithm. The only known approach was to wander through a brutally large search space of possible arrangements. According to Wikipedia, as of July 2026, the largest known solution was a hexagon of order $n=9$, found by Klaus Meffert in 2024.

So… what makes these solutions so hard to find? And how about we try?

Chapter 1: Making Observations (With Human Brain)

There is a clear tension between two independent constraints:

  1. The numbers must be consecutive;
  2. All line sums must be equal, even though the lines have different lengths.

The prior solutions I found suggested that people had already tried several search algorithms and likely optimized them well. That made me think that, if I wanted to advance the field, I should focus not on making the search faster, but on making the search space smaller.

Observation: Antisymmetric hexagons are much simpler

First, let’s restrict the numbers on the grid to the symmetric interval $-K,\ldots,K$ for some $K$. If all line sums are equal, this is equivalent to requiring that every line sum be zero.

Second, put $0$ in the center and require that cells opposite each other under a 180-degree rotation contain opposite values. If one cell contains $x$, its antipodal cell contains $-x$.

Notice how many constraints disappear. Every line through the center sums to zero automatically because its values cancel in opposite pairs. Every other line has an antipodal line with the sum of antipodal opposite numbers. If one of them sums to zero, so does the other.

Of course, simplifying the constraints introduces a risk: perhaps no solutions satisfy the extra symmetry at all. At this point, antisymmetry was simply a plausible place to search, following the drunkard’s principle.

But once I started thinking about zero-sum hexagons, another structure appeared.

Observation: Every zero-sum hexagon is build from the same 6-point ring

Consider any hexagonal grid, zero-sum or not. Take the six cells surrounding any interior point and add the alternating pattern \([-1,+1,-1,+1,-1,+1].\) Leave the central cell unchanged.

Every straight line that intersects this ring receives either no contribution or two opposite contributions, $+1$ and $-1$. Its sum therefore remains unchanged. We can add any multiple of this pattern without changing a single line sum.

These local alternating rings form a basis: every zero-sum hexagon can be built as a unique linear combination of them. I will omit the proof for brevity, but the idea is fairly straightforward. Starting at the outer layer, choose ring coefficients that cancel its cells, then peel the layer away and continue inward by induction.

An order-$n$ zero-sum hexagon therefore has two equivalent representations:

  • its visible cell values;
  • an order-$(n-1)$ potential field, recording how much of each local ring it contains.

The potential field representation also satisfies every line-sum constraint by construction. It does not, however, guarantee that the visible values are distinct and consecutive. Those remain difficult global constraints.

Below you can play with the potential field of the order-3 antisymmetric hexagon. Notice that however you change the potential field, the line sums in te hexagon remain zero. However, making the hexagon “magic” is still a very hard challenge.

(Unfortunately, this playground looks tiny for mobile readers)

Interactive potential-field visualization

This representation also plays well with antisymmetry. The potential field of an antisymmetric hexagon is itself symmetric.

The idea is that instead of searching among arbitrary arrangements and repeatedly repairing broken lines, we can now search entirely inside the space where every line already sums to zero. Which might - or might not - be a smaller search space for the solver.

Chapter 2: Finding New Hexagons (AI Writes Code)

Around the same time, I had been helping to pre-solve problems for the Midnight Code Cup 2026, a programming competition in which using LLMs is explicitly encouraged. Many of its tasks are optimization problems. My main takeaway was that LLMs can be unusually effective at developing domain-specific solvers, leaving general-purpose tools such as Z3 and OR-Tools far behind.

So instead of reaching for another generic constraint solver, I went to GPT-5.6 Sol and gave it more freedom with the problem. I pointed out the antisymmetry restriction and the potential-field representation. The model searched for related concepts and connected the problem to Heffter arrays: combinatorial arrangements of signed integers with prescribed zero-sum conditions. The problems are not identical, but the connection suggested better ways to organize values and exchange them while controlling the affected sums.

The resulting program abandoned the generic constraint-solver approach in favor of custom simulated annealing. I then did some due diligence through several rounds of optimization: asked the model to use Numba for the hot loops, identified memory-allocation and random-number-generation bottlenecks with perf, and eventually squeezed another 50% of performance out of the program. Here is the end result.

Then I left it running on my home server for a few days across roughly 24 CPU cores.

As I had hoped, the combination of a smaller search space and a specialized solver worked remarkably well. Soon I had discovered magic hexagons of every order up to $n=21$. Here they are:

Magic hexagon
Abnormal zero-sum magic hexagon of order 21
Potential field
Potential field of the order 21 magic hexagon
Loading…

The cell values look chaotic and noisy. Their potential fields do not. They resemble terrain maps, with broad slopes, ridges, valleys, and surprisingly smooth transitions.

I did not yet know whether this smoothness was a clue or merely an artifact of the search. Either way, it was difficult to look at those landscapes and not suspect that some larger structure was hiding underneath.

Chapter 3: Finding All Hexagons (AI Runs The Show)

Repeatedly finding larger solutions naturally suggested a conjecture:

This was a strong claim. Before this project, only a handful of abnormal magic hexagons were known. And don’t forget I had imposed the additional restriction of antisymmetry.

But inspired by the recent successes of AI in mathematics, I was curious to see how it would approach the problem. Could AI prove it from the outset?

I decided to try two AI systems:

  • GPT-5.6 Sol, the strongest general-purpose model available to me through my personal subscription;
  • Aristotle, a Lean-oriented theorem-proving agent.

I first gave GPT-5.6 Sol (high) the problem statement, the known solutions, and several additional intuitions. It proposed new hypotheses and possible constructions, then began chopping away, reducing the problem to smaller pieces. The work gained traction. At this stage, I still felt very much in the driver’s seat: learning unfamiliar mathematical machinery, checking the arguments, rejecting unproductive directions, and steering a process that I could mostly follow.

Several days of iteration passed. The problem had been reduced to a few key lemmas, so I brought in Aristotle to pursue a formal proof in parallel.

Eventually, the process stalled. Both agents remained optimistic, but they were clearly stuck, rehashing the same ideas without making meaningful progress. I weakened the conjecture to the more modest claim that infinitely many abnormal magic hexagons exist. We explored new hypotheses and even non-constructive approaches

Still, we hit a wall.

I then employed GPT-5.6 Sol (max), which reasoned for many hours and… also failed to find a proof. Before running out of credits, however, it produced several new ideas. Those ideas became part of the project’s shared context and remained available in later conversations.

In one such conversation, while I was once again directing GPT-5.6 Sol (high) toward a proof, it picked up several of those earlier ideas and combined them into what looked like a breakthrough. After checking it computationally, I had a constructive argument for every order $n>800$ divisible by $16$.

Once that foothold was established, the work REALLY gained traction. Iteration by iteration, I pushed GPT-5.6 Sol (high) to generalize the construction: first to orders divisible by $8$, then by $4$, then by $2$, and finally to remove the divisibility condition altogether. The proved threshold also fell from $800$ to $114$.

The threshold of $114$ is important to interpret correctly. It is a convenient bound under which the proof’s inequalities and combinatorial choices are easy to justify theoretically. It does not appear to be a fundamental limitation of the construction, because in practice, the same method succeeds well below that threshold.

Once the general construction existed, I pushed again, this time for simplicity and determinism. It was clear the initial construction contained many special cases, auxiliary choices, and pieces of machinery inherited from previous attempts. Sure enough, GPT-5.6 Sol (high) repeatedly found redundancies in the construction and replaced them with a cleaner deterministic algorithm.

It took dozens of long conversations and days of reasoning, but the conjecture was solved. And the result is constructive: it does not merely assert that these hexagons exist, but gives an algorithm for building them, starting from relatively low orders. Combined with the finite witnesses up to $n=21$ I found with bruteforce before, it covers every order $n>3$.

It is important to note that the proof was not formalized in Lean at the time of writing this post and was not independently verified. This will be the natural next step.

And here are some hexagons of much larger orders, generated by the construction. Notice the pattern?

Magic hexagon
Potential field
Loading…

Chapter 4: Reflections

  • I started this project with AI as a co-pilot and myself in the driver’s seat. By the end, I was more of a passenger: the AI was doing much of the creative work, while I nudged it toward directions that felt promising.

    I am glad that some ideas born in my human brain turned out to be instrumental. Antisymmetry plays a major role in the final construction, and the potential fields - well, not so much. Yet their smoothness remains interesting, and perhaps there’s more to discover.

  • GPT-5.6 Sol is a remarkably capable mathematical reasoner, but it has a double-edged tendency toward tunnel vision. It can go very deep in a chosen direction. When that direction is right, this is incredibly powerful. When it is wrong, you had better keep an arbiter in the loop - another model or, in my case, a human - to remind it of the bigger picture and notice when meaningful progress has stopped.

  • It was essential that the model had access to the internet. It found related papers and mathematical concepts that I could not have anticipated and supplied in advance. That was one reason I used the ChatGPT web interface rather than Codex. Another was personal: I wanted this project to remain primarily a mental exercise during my time off, rather than turning into another software-engineering project that kept me in front of a computer. For a serious proof campaign, however, I would use a more systematic setup: several models working in parallel, explicit roles for proposing and criticizing arguments, persistent shared context, and access to internet search through a service such as exa.ai.

  • In software engineering, code review became a bottleneck once LLMs made code generation cheap. Statically typed languages, static analysis and testing suddenly mattered more than ever because they could take some of the growing burden of correctness verification away from humans.

    Mathematics now faces a similar problem. AI can produce candidate theories and proofs faster than people can responsibly verify them. Lean and other machine-verifiable proof systems must be an enormous boon to the community, offering a way to scale verification along with generation.

  • I wonder if the techniques discovered during this project can be applied to other combinatorial construction problems and developed into something more general than a solution to one hexagonal puzzle. Heffter arrays naturally come to mind.

  • It’s funny how this story began with an interest in the number 19 and ended with a construction of magic hexagons for every order greater than $3$. The remaining challenge is to make the proof machine-verifiable - now Aristotle and leanprover/comparator have some work to do.