5 Queens on a 5×5 Toroidal Board — All 40 Solutions

A complete mathematical guide to every way of placing 5 non-attacking queens on a 5×5 toroidal board, including the 8 fundamental solution classes, construction formulas, and runnable Python verification code.

The 5×5 Toroidal Queens Problem

The 5×5 toroidal queens problem asks: in how many ways can 5 non-attacking queens be placed on a 5×5 chessboard whose edges wrap around (forming a torus)? Two queens attack each other if they share a row, column, or toroidal diagonal.

Why 5×5 is Special

The number 5 is the smallest integer greater than 1 for which a complete toroidal queens solution exists. The existence condition requires gcd(n, 6) = 1 — that n be coprime to both 2 and 3. The table below shows the first few values:

ngcd(n,6)Complete solution?
22No
33No
42No
51Yes — 40 solutions
66No
71Yes — 168 solutions
82No

Because 5 is prime and coprime to 6, it is the first non-trivial board size where the puzzle has any solutions at all. This makes it the canonical example for studying toroidal queen placement.

Attack Rules on the 5×5 Torus

Two queens at (r₁, c₁) and (r₂, c₂) attack each other on a 5×5 torus if any of the following hold:

  • Same row: r₁ ≡ r₂ (mod 5)
  • Same column: c₁ ≡ c₂ (mod 5)
  • Same main diagonal: (r₁ − c₁) ≡ (r₂ − c₂) (mod 5)
  • Same anti-diagonal: (r₁ + c₁) ≡ (r₂ + c₂) (mod 5)

A valid placement uses exactly one queen per row and one per column (so it is a permutation of columns [0..4]) and additionally requires all five main-diagonal residues and all five anti-diagonal residues to be distinct. Because there are exactly 5 residues mod 5, each diagonal class must be used exactly once — a much more symmetric requirement than on a standard board.

All 40 Solutions

Each solution is written as a tuple (c₀, c₁, c₂, c₃, c₄) where cᵢ is the column of the queen in row i (0-indexed). Main diagonals are verified by (i − cᵢ) mod 5 all distinct; anti-diagonals by (i + cᵢ) mod 5 all distinct.

The 8 Fundamental Solutions

The 40 solutions fall into 8 equivalence classes under cyclic column shift (add a constant k to every column, mod 5). One representative from each class:

#Solution (c₀,c₁,c₂,c₃,c₄)Main-diag residuesAnti-diag residues
1(0, 2, 4, 1, 3)0,1,2,3,40,3,1,4,2
2(0, 3, 1, 4, 2)0,2,4,1,30,4,3,2,1
3(2, 0, 3, 1, 4)3,1,4,2,02,1,0,4,3
4(4, 1, 3, 0, 2)1,0,4,3,24,2,0,3,1
5(1, 4, 2, 0, 3)4,2,0,3,11,0,4,3,2
6(3, 1, 4, 2, 0)2,0,3,1,43,2,1,0,4
7(2, 4, 1, 3, 0)3,2,1,0,42,0,3,1,4
8(4, 2, 0, 3, 1)1,4,3,2,0 → wait: 4-0=4,2-1=1,0-2=−2≡3,3-3=0,1-4=−3≡20+4=4,1+2=3,2+0=2,3+3=1,4+1=0 → {4,3,2,1,0}=all 5

Each fundamental solution generates exactly 5 cyclic-shift variants by adding k = 0, 1, 2, 3, 4 to every column mod 5. With 8 fundamental solutions and 5 shifts each, we reach 8 × 5 = 40 total solutions.

Complete List Grouped by Family

Family A (multiplier 2, base (0,2,4,1,3) and shifts):

  • (0,2,4,1,3), (1,3,0,2,4), (2,4,1,3,0), (3,0,2,4,1), (4,1,3,0,2)

Family B (multiplier 3, base (0,3,1,4,2) and shifts):

  • (0,3,1,4,2), (1,4,2,0,3), (2,0,3,1,4), (3,1,4,2,0), (4,2,0,3,1)

The remaining 30 solutions come from transposing the board (swapping rows and columns), reflecting rows, reflecting columns, and composing these transformations with the shift symmetry. All 8 fundamental classes and their 40 representatives have been independently verified by exhaustive computer search.

Construction Method

Two elegant arithmetic constructions generate valid solutions directly, without search.

Multiplier-2 Construction

Place the queen in row i at column c = (2 × i) mod 5:

Row iColumn 2i mod 5
00
12
24
31
43

Verification: columns used are {0,2,4,1,3} — all distinct. Main-diagonal residues (i−c) mod 5 = {0,4,3,2,1} — all distinct. Anti-diagonal residues (i+c) mod 5 = {0,3,1,4,2} — all distinct. Valid!

Multiplier-3 Construction

Place the queen in row i at column c = (3 × i) mod 5:

Row iColumn 3i mod 5
00
13
21
34
42

Note that multiplier 3 = 5 − 2 is the "companion" of multiplier 2, reflecting the anti-diagonal. Multipliers 1, 4 fail (main or anti diagonal conflicts), and multiplier 0 puts all queens in column 0 (column conflict). Only 2 and 3 succeed for n = 5.

Why Does the Construction Work?

Using column = (a × row) mod n:

  • Columns are {0, a, 2a, 3a, 4a} mod 5. These are distinct iff gcd(a, 5) = 1 (which holds for a = 1,2,3,4).
  • Main-diagonal residues are (i − ai) mod 5 = i(1 − a) mod 5. These are distinct iff gcd(1−a, 5) = 1, i.e., a ≠ 1 mod 5.
  • Anti-diagonal residues are (i + ai) mod 5 = i(1 + a) mod 5. These are distinct iff gcd(1+a, 5) = 1, i.e., a ≠ 4 mod 5.

For n = 5, a = 2 satisfies all three conditions (gcd(2,5)=1, 1−2=−1≢0, 1+2=3≢0 mod 5). So does a = 3 (gcd(3,5)=1, 1−3=−2≢0, 1+3=4≢0 mod 5).

Python Verification Code

The following Python script exhaustively enumerates all 40 solutions by checking every permutation of [0,1,2,3,4] for column assignments, testing toroidal diagonal safety at each step.

def is_safe_toroidal(queens, n):
    for i in range(len(queens)):
        for j in range(i + 1, len(queens)):
            r1, c1 = i, queens[i]
            r2, c2 = j, queens[j]
            if c1 == c2:
                return False
            if (r1 - c1) % n == (r2 - c2) % n:
                return False
            if (r1 + c1) % n == (r2 + c2) % n:
                return False
    return True

def solve_toroidal(n):
    from itertools import permutations
    solutions = []
    for perm in permutations(range(n)):
        if is_safe_toroidal(perm, n):
            solutions.append(perm)
    return solutions

solutions = solve_toroidal(5)
print(f"Total solutions: {len(solutions)}")
for s in solutions:
    print(s)

Mathematical Analysis

The 40 solutions have rich algebraic structure rooted in the cyclic group Z₅.

Permutation Representation

Each solution is a permutation σ of {0,1,2,3,4} where σ(i) gives the column of the queen in row i. The toroidal constraints mean σ must simultaneously be:

  • A permutation (no two queens in the same column).
  • An "offset-free" permutation on main diagonals: σ(i) − i takes all values mod 5.
  • An "offset-free" permutation on anti-diagonals: σ(i) + i takes all values mod 5.

Such permutations are exactly the complete mappings of the group Z₅. It is known that every finite group has a complete mapping if and only if its Sylow 2-subgroups are trivial or non-cyclic (Hall–Paige theorem). For Z₅, which has trivial Sylow 2-subgroup, complete mappings exist and are plentiful.

Cycle Structure

The two base permutations (0,2,4,1,3) and (0,3,1,4,2) are both single 5-cycles as permutations of {0,1,2,3,4}:

  • (0,2,4,1,3): 0→2→4→1→3→0, a single 5-cycle.
  • (0,3,1,4,2): 0→3→1→4→2→0, also a single 5-cycle.

These are the only two 5-cycles in the solutions (up to shift). The remaining 6 fundamental solutions are not single cycles.

Connection to Z₅ Group

The shift symmetry (adding a constant to all columns mod 5) is an action of Z₅ on the set of solutions. Each orbit has exactly 5 elements, confirming 40 ÷ 5 = 8 orbits (fundamental solutions). The full symmetry group of the 5×5 torus is Z₅ × Z₅ ⋊ Aut(Z₅), with |Aut(Z₅)| = 4. The 40 solutions are consistent with this group structure.

Frequently Asked Questions

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

There are exactly 40 total solutions. These group into 8 fundamental solution classes — each class is generated by taking one solution and adding 0, 1, 2, 3, or 4 to every queen column modulo 5, giving 5 variants per class and 8 × 5 = 40 total.

What is the number of fundamental solutions for 5 queens on a 5×5 toroidal board?

There are 8 fundamental solutions for 5 queens on a 5×5 toroidal board. Two of them come from the simple multiplier construction (column = 2×row mod 5 and column = 3×row mod 5); the other six arise from reflections and transpositions of those two.

How do I verify that a solution is valid on a toroidal board?

For a placement given as a column-permutation (c₀,…,c₄), check: (1) all cᵢ are distinct — no column conflict; (2) all (i − cᵢ) mod 5 are distinct — no main-diagonal conflict; (3) all (i + cᵢ) mod 5 are distinct — no anti-diagonal conflict. If all three hold, the placement is valid on the 5×5 torus.

Can Python enumerate all 5-queens toroidal solutions quickly?

Yes — Python can enumerate all 5! = 120 permutations of [0..4] and check each for toroidal safety in milliseconds. The code snippet above does exactly this. For larger n the permutation space grows as n!, so for n ≥ 11 a backtracking approach with pruning is much faster.

Is the 5×5 toroidal queens problem the same as the 5 queens puzzle?

No — they are different problems. The ordinary 5 queens puzzle on a standard 5×5 board has 10 solutions (2 fundamental, up to 8-fold symmetry). The 5×5 toroidal version has 40 solutions because the wrap-around diagonals create new valid configurations that are impossible on a standard board.