Sudoku · algorithms · complexity

The math behind Sudoku — algorithms, NP-completeness, generation

11 min readTechnicalUpdated May 2026

A technical guide for developers, math nerds, and people who want to know what’s actually happening when a Sudoku puzzle is generated. Topics: how solvers work (constraint propagation + backtracking), how generators produce unique-solution puzzles, why Sudoku is NP-complete, the proof that 17 is the minimum clue count, and the exact number of valid 9×9 grids (6.67 sextillion).

The constraints, formally

A Sudoku puzzle is a partial function f: {1..9} × {1..9} → {1..9} (cells to digits) such that the unique completion satisfies:

The first three are constraint satisfaction. The fourth (uniqueness) is what distinguishes a well-formed Sudoku puzzle from a partially-filled grid that happens to be solvable.

Solvers: constraint propagation + backtracking

Modern fast Sudoku solvers use a two-stage strategy:

Stage 1: constraint propagation

For each empty cell, maintain a candidate set — the digits that could legally go there. Apply rules iteratively until no further deductions are possible:

Propagation reduces candidates without guessing. For Easy and most Medium puzzles, this stage solves the puzzle entirely.

Stage 2: backtracking

When propagation stalls (no further deductions possible) but the puzzle isn’t solved, the solver picks the cell with the fewest candidates and tries each in turn:

function solve(grid):
  if grid is complete: return grid
  cell = pickCellWithFewestCandidates(grid)
  for each candidate value in cell.candidates:
    place value in cell
    propagate constraints (Stage 1 again)
    if no contradiction:
      result = solve(grid)
      if result: return result
    undo placement and propagation
  return null  # no valid completion

The “fewest candidates” cell choice is a most-constrained-variable heuristic from constraint satisfaction theory. Picking randomly works but is slower; picking the most-constrained cell minimizes the search tree.

Performance

Modern hand-rolled backtracking solvers solve 99% of human-difficulty Sudoku in under 1 millisecond on commodity hardware. The hardest known 9×9 puzzles (constructed adversarially) take seconds to tens of seconds even for optimized solvers. Our daily Sudoku generator at sudoku.html runs the solver in browser JavaScript and generates puzzles in 100–1000ms.

Generators: how to create a new puzzle

Generation is harder than solving because you’re searching for a puzzle with both a unique solution and a target difficulty. Standard approach:

Step 1: generate a complete valid grid

Run a randomized backtracking fill on an empty grid. Each cell is assigned a random digit from its candidate set; backtrack on contradictions. This produces a uniformly-random valid 9×9 Sudoku grid in microseconds.

Step 2: remove cells, checking uniqueness

Pick cells in random order. For each cell, temporarily remove its value and run the solver to count solutions. If the puzzle still has exactly one solution, accept the removal. If multiple solutions exist after removal, restore the cell.

solution = generateRandomGrid()
puzzle = copyOf(solution)
positions = shuffle(allCellPositions)
for (r, c) in positions:
  removed = puzzle[r][c]
  puzzle[r][c] = 0
  if countSolutions(puzzle, limit=2) > 1:
    puzzle[r][c] = removed  # undo

The countSolutions call has a limit parameter — we stop counting at 2 because we only need to know “is it unique or not.” This shortcut dramatically reduces generation time vs. counting all solutions.

Step 3: difficulty tuning

Difficulty correlates with two things:

Most generators target a clue count and call it done. Better generators run the propagation-only solver after each removal: if the propagation-only solver can’t solve the resulting puzzle, the puzzle is harder than the propagation rules permit, and you classify it accordingly.

Generation is solving in reverse, with extra constraints. The hard part isn’t making puzzles — it’s making puzzles of a specific difficulty without making them ambiguous.

Sudoku is NP-complete (with caveats)

Generalized Sudoku — the n²×n² version where each row, column, and n×n box must contain digits 1 through n² — is NP-complete. Proved in 2003 by Yato and Seta via reduction from Latin Square Completion (which Colbourn proved NP-complete in 1984).

The reduction:

  1. Take any instance of Latin Square Completion (a partially-filled n×n Latin Square; decide if it can be completed).
  2. Embed it as the upper-left n×n region of an n²×n² Sudoku, with all other cells initialized to enforce that the only valid Sudoku completion corresponds to a valid Latin Square completion.
  3. The construction takes polynomial time. Therefore Sudoku ≥ Latin Square Completion in complexity.

Caveat about 9×9. The classic 9×9 Sudoku has a constant-size input (always 81 cells), so it’s technically solvable in O(1) by lookup — you’d need a database of all 6.67×10²¹ possible grids, but it’s constant. The NP-completeness result applies to the parameterized n²×n² family.

In practice this distinction doesn’t matter. The algorithms that solve 9×9 are the same backtracking algorithms that solve n²×n². The complexity result tells us that fundamentally fast (polynomial-time) algorithms don’t exist for the general case, even though brute-force is fine for 9×9.

The 17-clue minimum proof

For decades, Sudoku enthusiasts asked: what’s the smallest number of clues a unique-solution Sudoku can have? Empirical answer was 17 — people had constructed many 17-clue puzzles — but no known 16-clue example existed.

In 2014, Gary McGuire, Bastian Tugemann, and Gilles Civario proved formally that 17 is the minimum. Method: exhaustive computer search.

The proof took approximately 7.1 million CPU-hours — about 800 CPU-years — on a Stokes supercomputer cluster at the Irish Centre for High-End Computing.

The proof has been independently verified. It is now the minimum-clue Sudoku result.

How many Sudoku grids exist? (6.67 × 10²¹)

In 2005, Bertram Felgenhauer and Frazer Jarvis enumerated all valid 9×9 Sudoku grids:

N = 6,670,903,752,021,072,936,960

Their method:

  1. Fix the top band (rows 1, 2, 3). The number of valid top bands is small enough to enumerate directly: 9! × 56 × 6&sup6; = a few million arrangements after symmetry reduction.
  2. For each top band, count the number of ways to complete it to a full grid.
  3. Use Burnside’s lemma to count by symmetry classes.

Result verified by independent groups in 2006. The count of essentially-different grids (after factoring out the 9! × 6&sup8; = 3,359,232 trivial symmetries: digit relabelings, row/column permutations within bands/stacks, full-grid transposition) is approximately 5,472,730,538 — about 5.5 billion.

Sudoku and Latin Squares: the relationship

A 9×9 Latin Square is a 9×9 grid where each row and column contains the digits 1–9 exactly once. There are 5.524 × 10²⁷ valid 9×9 Latin Squares.

A Sudoku grid is a Latin Square that also satisfies the box constraint. So Sudoku grids are a subset of Latin Squares: about 6.67 × 10²¹ of 5.5 × 10²⁷, or 1 in 800,000.

This ratio matches intuition: the box constraint is a strong filter. About 99.9999% of Latin Squares fail the box constraint somewhere.

Symmetry: what makes two Sudoku grids “the same”

Several operations preserve Sudoku validity:

Two grids related by any composition of these are “essentially the same.” The factor of 9! × 6&sup8; / 2 = ~1.2 billion symmetries reduces the 6.67×10²¹ count to ~5.5 billion essentially-different grids.

Variants: Killer, Hyper, Diagonal, Samurai

Practical generator: Puzzle Cottage’s implementation

Our daily Sudoku generator (in JavaScript, in sudoku.html):

  1. Seed: use the date as a deterministic seed via LCG (s = (1664525s + 1013904223) mod 2³²) so every player worldwide gets the same daily puzzle.
  2. Generate complete grid: randomized backtracking fill of an empty 9×9.
  3. Remove cells: shuffle 81 positions; for each, try removing while validating uniqueness. Continue until target blank-count is reached or no further cell can be removed.
  4. Difficulty target: Easy = 38 blanks, Medium = 48 blanks, Hard = 56 blanks. Three independent puzzles per day, one per level.

Generation runs in 100–1000ms in the browser, cached in localStorage so the user only pays the cost once per puzzle. Source: sudoku.html.

Frequently asked questions

Is Sudoku NP-complete?
Generalized n²×n² Sudoku: yes, proved by Yato and Seta in 2003. Classic 9×9 is technically constant-size, but algorithms that solve general Sudoku are the same backtracking family.
How are Sudoku puzzles generated?
Generate a random valid grid, then remove cells one at a time while verifying the puzzle still has a unique solution.
What’s the minimum clue count?
17 — proved in 2014 by McGuire, Tugemann, and Civario via 7.1 million CPU-hour exhaustive search.
How many valid grids exist?
6,670,903,752,021,072,936,960 (~6.67 × 10²¹). About 5.5 billion essentially different after symmetry.
What algorithm do solvers use?
Constraint propagation followed by backtracking. Most-constrained-variable heuristic for branch selection.
Is Sudoku really a Latin Square?
A subset. Sudoku grids satisfy the Latin Square constraints plus the box constraint. About 1 in 800,000 Latin Squares is a valid Sudoku.
Why backtracking instead of CSP libraries?
For 9×9, hand-rolled backtracking is faster. CSP libraries are useful for variants (Killer, Hyper).
Maximum unsolvable clue count?
You can construct unsolvable grids with up to ~80 clues, but the more interesting result is the 17-clue minimum for solvable.