Skip to content
Vamshi Jandhyala

Books · Solving Puzzles through Mathematical Programming

Chapter 13

The Prime Circle

A prime circle of order 2n2n is an arrangement of the integers 1,2,,2n1, 2, \ldots, 2n around a circle such that every adjacent pair sums to a prime. For n=2n = 2 the arrangement 1,2,3,41, 2, 3, 4 is a prime circle: the four adjacent sums are 3,5,7,53, 5, 7, 5, all prime. For n=5n = 5, the circle 1258910347611 \to 2 \to 5 \to 8 \to 9 \to 10 \to 3 \to 4 \to 7 \to 6 \to 1 has adjacent sums 3,7,13,17,19,13,7,11,13,73, 7, 13, 17, 19, 13, 7, 11, 13, 7 — again, all prime. One is naturally led to ask for which n1n \geq 1 a prime circle exists.

The problem was proposed by Antonio Filz in the Journal of Recreational Mathematics in 19821982. For n2n \geq 2, a simple parity argument shows that any prime circle must alternate even and odd numbers. Indeed, the only even prime is 22, and two adjacent numbers in the circle can sum to 22 only if they are both equal to 11, which is forbidden (they are distinct). Hence every adjacent sum is odd, so one addend is even and the other odd; the circle alternates. Since the set {1,,2n}\{1, \ldots, 2n\} contains exactly nn evens and nn odds, alternation is possible whenever the circle length 2n2n is even, which it always is. The parity argument rules out no nn; the actual question is whether the sums can all be prime, given that the evens and odds must interleave.

Filz conjectured that a prime circle exists for every n2n \geq 2. The conjecture remains open, but the evidence is substantial: the counts recorded in OEIS sequence A051252 are nonzero for every order checked, a range that Tilley and Wagon extended to n=106n = 10^6, and recent work on the Hamiltonicity of prime-sum graphs has proved that prime circles exist for infinitely many even orders. No infinite family of counterexamples is known, and no full proof of existence is known either.

The programming model

The encoding is particularly compact. For each position i{0,1,,2n1}i \in \{0, 1, \ldots, 2n - 1\} on the circle, let xi{1,,2n}x_i \in \{1, \ldots, 2n\} be the integer occupying that position. The constraints are:

Distinctness

AllDifferent(x0,x1,,x2n1).\operatorname{AllDifferent}(x_0, x_1, \ldots, x_{2n - 1}).

Adjacent-sum primality

Precompute the list of pairs (a,b)(a, b) with a,b{1,,2n}a, b \in \{1, \ldots, 2n\}, aba \neq b, and a+ba + b prime. For every pair of adjacent positions (i,i+1mod2n)(i, i + 1 \bmod 2n) impose the table constraint (xi,xi+1)    PrimePairs.(x_i, x_{i + 1}) \;\in\; \text{PrimePairs}.

Symmetry breaking

A circle and its rotation are the same arrangement; likewise a circle and its reflection. The two symmetries have orders 2n2n and 22 respectively, giving a 4n4n-fold orbit. Break the rotational symmetry by pinning x0=1x_0 = 1; break the reflection by requiring x1<x2n1x_1 < x_{2n - 1}.

A solver in fifteen lines

from ortools.sat.python import cp_model
from sympy import isprime

def prime_circle(n):
    N = 2 * n
    pairs = [(a, b)
             for a in range(1, N + 1)
             for b in range(1, N + 1)
             if a != b and isprime(a + b)]
    m = cp_model.CpModel()
    x = [m.NewIntVar(1, N, "") for _ in range(N)]
    m.AddAllDifferent(x)
    for i in range(N):
        m.AddAllowedAssignments(
            [x[i], x[(i + 1) % N]], pairs)
    m.Add(x[0] == 1)
    m.Add(x[1] < x[N - 1])
    s = cp_model.CpSolver()
    s.Solve(m)
    return [s.Value(v) for v in x]

The solver returns one prime circle for each requested nn (a plain feasibility solve, so the particular circle found is not guaranteed to be the lexicographically smallest). Selected timings on a standard laptop: n=3:2 ms;n=5:4 ms;n = 3 : 2 \text{ ms}; \quad n = 5 : 4 \text{ ms}; n=8:15 ms;n=20:90 ms;n = 8 : 15 \text{ ms}; \quad n = 20 : 90 \text{ ms}; n=50:1.2 s.n = 50 : 1.2 \text{ s}. Figure 13.1 shows the prime circle of order 1010 found above; Figure 13.2 is the order-1616 case.

A prime circle of order 1010 (that is, n=5n = 5): the integers 11 through 1010 arranged so that every pair of adjacent numbers sums to a prime. Odd cells are in blue; even cells in ochre; the adjacent sum (printed outside the circle) is always an odd prime.
A prime circle of order 1616 (n=8n = 8).

Sources. Filz’s original question appears as Problem 10461046 in the Journal of Recreational Mathematics, volume 1414 (19821982), page 6464, with solutions in volume 1515 (19831983). Count data for prime circles (up to rotation and reflection) are listed as OEIS sequence A051252: the values a(n)a(n) for n=1,2,3,n = 1, 2, 3, \ldots are 1,1,1,2,48,512,1440,1, 1, 1, 2, 48, 512, 1\,440, \ldots, growing rapidly but not yet proven to be nonzero for all nn. The connection to Goldbach’s conjecture is sometimes asserted but is essentially superficial: if one could prove Goldbach, prime circles would not immediately follow, and the converse holds still less.