Toroidal Queens Puzzle

Place non-attacking queens on a toroidal chessboard where the top edge wraps to the bottom and the left edge wraps to the right — turning the board into a mathematical donut.

What is a Toroidal Chessboard?

A toroidal chessboard is an ordinary n×n grid in which opposite edges are glued together. The top row and the bottom row become neighbours, and the leftmost column and the rightmost column become neighbours. Topologically the result is a torus — the surface of a donut.

On a standard chessboard a queen placed in a corner attacks only three directions along the edge. On a toroidal board every queen attacks in all eight directions without limit, because a ray that exits one side immediately re-enters from the opposite side. This simple change has profound consequences for how many queens can be placed without conflict.

Visualising the Wrap-Around

Imagine the classic arcade game Pac-Man: when the character moves off the right side of the screen it reappears on the left. The toroidal chessboard works exactly the same way, but in both horizontal and vertical directions simultaneously.

Concretely, a queen at position (r, c) on an n×n torus attacks:

  • Same row: all cells (r, x) for x ≠ c, where column indices are taken mod n.
  • Same column: all cells (x, c) for x ≠ r, where row indices are taken mod n.
  • Main diagonal (↘ direction): all cells (r+k, c+k) mod n for k = 1, …, n−1.
  • Anti-diagonal (↗ direction): all cells (r−k, c+k) mod n for k = 1, …, n−1.

Because every diagonal on a torus wraps around and has exactly n cells, there are exactly 2n distinct diagonals (n in each direction). Two queens conflict on the main diagonal if (r₁−c₁) ≡ (r₂−c₂) (mod n), and on the anti-diagonal if (r₁+c₁) ≡ (r₂+c₂) (mod n).

Why the Donut Shape?

Mathematically, the torus is formed by taking the unit square [0,1]×[0,1] and identifying the pair of horizontal edges and the pair of vertical edges. The result is a surface with no boundary at all — every point on a torus looks locally identical to every other point. This is why the toroidal queens puzzle has a much higher degree of symmetry than the ordinary puzzle: rotations of the board by 1/n of a turn (i.e., cyclic shifts of rows or columns) map solutions to solutions.

How Toroidal Wrapping Changes the Puzzle

On a standard n×n board the n queens problem has solutions for every n ≥ 4 (and for n = 1). The toroidal version is far more restrictive.

The Existence Condition

A complete solution — placing n non-attacking queens on an n×n toroidal board — exists if and only if gcd(n, 6) = 1. This means n must be coprime to both 2 and 3. The first values of n for which a solution exists are:

  • n = 1 (trivial)
  • n = 5 (smallest non-trivial case)
  • n = 7
  • n = 11
  • n = 13
  • n = 17, 19, 23, 25, …

For n divisible by 2 or 3 — including n = 8, the classic chessboard size — no complete solution exists. The reason is that on a torus the n main diagonals form n equivalence classes mod n. If n is even, diagonals in opposite pairs share attack lines; if n is a multiple of 3, a similar arithmetic obstruction arises.

No Safe Corners or Edges

On a standard board, corners and edges are slightly "safer" because they are attacked by fewer diagonal rays. On a torus there are no corners or edges — every square is attacked by exactly four diagonals. This means all positions are symmetric, and there are no positional shortcuts for the solver.

Modular Arithmetic Construction

When gcd(n, 6) = 1, solutions can be constructed directly using arithmetic. One family of solutions places queens at positions (i, (a·i) mod n) for i = 0, 1, …, n−1, where a is chosen so that neither a, 1, nor a+1 (mod n) and a−1 (mod n) lead to diagonal conflicts. For n = 5, the multipliers a = 2 and a = 3 both work.

Symmetry Group

On a torus, the symmetry group acting on solutions includes:

  • n² cyclic shifts — shifting all queen positions by (Δr, Δc) maps any solution to another solution.
  • Rotations and reflections of the torus (a smaller group than the full 8-fold dihedral group of the square).

For n = 5, each of the 8 fundamental solutions generates 5×5 = 25 shifted copies, but many of these coincide under the reflection symmetry, giving 40 total distinct solutions from 8 fundamentals.

Solutions on the 5×5 Toroidal Board

The 5×5 toroidal board is the smallest interesting case. It supports a complete placement of 5 queens (one per row and column with no diagonal conflicts) and also allows counting partial placements of 4 queens.

5 Queens: 40 Total, 8 Fundamental

There are exactly 40 solutions for placing 5 non-attacking queens on a 5×5 torus, forming 8 fundamental solution classes under cyclic translation symmetry.

Two particularly elegant solutions come from arithmetic progressions:

  • Multiplier-2 family: column = (2 × row) mod 5 → queens at (0,0),(1,2),(2,4),(3,1),(4,3).
  • Multiplier-3 family: column = (3 × row) mod 5 → queens at (0,0),(1,3),(2,1),(3,4),(4,2).

From each such base solution, shifting all columns by 0, 1, 2, 3, or 4 gives 5 distinct solutions, yielding 2 × 5 = 10 solutions. The remaining 30 arise from the reflections and rotations of the torus, accounting for all 40 solutions across 8 equivalence classes.

4 Queens: 20 Arrangements

When the constraint is relaxed to 4 non-attacking queens on the same 5×5 torus, the answer jumps to 20 valid arrangements. With fewer queens, the diagonal constraints are easier to satisfy — every valid 5-queen solution contains 5 subsets of 4 queens that are each a valid 4-queen placement, but additional 4-queen placements also exist that cannot be extended to 5 queens.

5 Queens on 5×5 Torus

Complete enumeration of all 40 solutions, construction method, Python verification code, and mathematical analysis.

4 Queens on 5×5 Torus

All 20 ways to place 4 non-attacking queens on a 5×5 toroidal board, with combinatorial analysis.

Toroidal Chessboard Explained

Visual guide to edge-wrapping mechanics, diagonal paths on a torus, and the underlying mathematics.

Mathematical Properties

The toroidal queens puzzle sits at the intersection of combinatorics, group theory, and number theory. Here we summarise the key mathematical results.

Existence: gcd(n, 6) = 1

As stated above, a complete n-queens solution on an n×n torus exists if and only if n is coprime to 6. This is a theorem, not merely an observation: the proof shows that if 2 | n then the n main diagonals partition into conflicting pairs, and if 3 | n a similar three-way obstruction arises.

Solution Count Table

nFundamental solutionsTotal solutions
111
5840
74168 (= 4 × 7²/... see note)
11403 520
134010 920 (≈ 40 × 13²/... see note)

Note: total solutions = (number of fundamental solutions) × n², divided by any additional symmetries of the specific solution. For most solutions the divisor is 1, giving total = fundamental × n².

Connection to Latin Squares

A complete toroidal n-queens solution is equivalent to a Latin square of order n in which neither the main diagonal nor the back diagonal contains a repeated symbol. Such Latin squares are called doubly diagonal Latin squares or Keedwell Latin squares. The connection provides another way to count and construct solutions.

Connection to Modular Arithmetic (Z_n)

When the multiplier construction works — i.e., when gcd(a(a+1)(a−1), n) = 1 — the solution set forms a coset of a cyclic subgroup in Z_n × Z_n. This algebraic structure is why the solution count is always a multiple of n² for the arithmetic-family solutions.

Computational Results

For larger n coprime to 6 (n = 17, 19, 23, …), solutions can be generated in O(n) time using the modular arithmetic construction. Complete enumeration via backtracking with toroidal conflict checking runs in polynomial time for small n but grows rapidly — the exact counts for very large n remain an active area of combinatorial research.

Frequently Asked Questions

What is a toroidal chessboard?

A toroidal chessboard is an n×n grid where the top and bottom edges are connected and the left and right edges are connected, creating the topology of a torus (donut shape). A queen moving off one edge immediately reappears on the opposite side, so there are no boundaries limiting its attack.

How many solutions exist for 5 queens on a 5×5 toroidal board?

There are exactly 40 total solutions for placing 5 non-attacking queens on a 5×5 toroidal board. These fall into 8 fundamental solution classes; each class generates 5 solutions by cyclically shifting all queen columns, giving 8 × 5 = 40 total.

How many ways can 4 non-attacking queens be placed on a 5×5 toroidal board?

There are exactly 20 ways to place 4 non-attacking queens on a 5×5 toroidal board. This is the answer to the partial-placement version of the toroidal puzzle for this board size.

Why is there no solution for 8 queens on an 8×8 toroidal board?

Because 8 is divisible by 2, and gcd(8, 6) = 2 ≠ 1. On a torus with even side length n, the n main-diagonal equivalence classes split into two interlocked families, making it impossible to assign one queen to each family without collision. No complete 8-queens solution exists on the 8×8 torus.

What is the connection between the toroidal queens puzzle and modular arithmetic?

On an n×n torus (with n coprime to 6), placing queens at positions (i, a·i mod n) for i = 0…n−1 always yields a valid solution for an appropriate multiplier a. This is because the conflict conditions reduce to checking that certain differences are non-zero in Z_n, which is guaranteed when the multiplier is chosen so that a, a+1, and a−1 are all coprime to n.