Sudoku Puzzle Generator
Generate valid 9ร9 Sudoku puzzles with guaranteed unique solutions. Four difficulty levels from Easy to Expert. Print-ready grid output.
About
A correctly constructed Sudoku puzzle has exactly one valid solution. Generating such a puzzle requires two distinct phases: producing a complete Latin square that satisfies all row, column, and 3ร3 box constraints, then strategically removing clues while verifying uniqueness after each removal via a constraint-propagation solver. Failure to enforce uniqueness yields an ambiguous grid - unsuitable for logical deduction and frustrating to solve. This generator implements recursive backtracking with randomized candidate selection for board construction and a dual-phase carving algorithm that checks solution count โค 1 at every step.
Difficulty is controlled by the number of revealed clues: Easy provides 36 - 45 clues, Medium 27 - 35, Hard 22 - 26, and Expert as few as 17 - 21. The minimum possible clue count for a unique 9ร9 Sudoku is 17, proven by McGuire et al. in 2012. Note: Expert-level generation may take longer due to the exhaustive uniqueness verification required at low clue counts. The algorithm approximates difficulty by clue count alone - it does not analyze solving technique complexity (naked pairs, X-wings, etc.).
Formulas
The Sudoku constraint can be expressed formally. Let S be the set of cells indexed r, c โ {0, โฆ, 8}. Each cell holds a value v(r, c) โ {1, โฆ, 9}. The puzzle is valid when three uniqueness constraints hold simultaneously:
The box index for cell (r, c) is computed as:
Where r = row index (0 - 8), c = column index (0 - 8), v(r,c) = digit placed in that cell, br and bc = box row and column indices. The generation algorithm fills cells using backtracking with time complexity O(9n) worst-case, where n = empty cells, though constraint propagation prunes this dramatically in practice.
Reference Data
| Difficulty | Clues Given | Empty Cells | Approx. Generation Time | Typical Solving Time (Human) | Techniques Required |
|---|---|---|---|---|---|
| Easy | 36-45 | 36-45 | < 200 ms | 5-15 min | Naked Singles |
| Medium | 27-35 | 46-54 | < 500 ms | 15-30 min | Naked & Hidden Singles |
| Hard | 22-26 | 55-59 | < 1 s | 30-60 min | Pairs, Pointing, Box/Line |
| Expert | 17-21 | 60-64 | 1-5 s | 1-3 hr | X-Wing, Swordfish, Chains |
| Sudoku Grid Constants | |||||
| Grid Size | 9 ร 9 = 81 cells | ||||
| Sub-boxes | 9 boxes of 3 ร 3 | ||||
| Total Valid Completed Grids | 6,670,903,752,021,072,936,960 (≈ 6.67 ร 1021) | ||||
| Min Clues for Uniqueness | 17 (proven 2012, McGuire - Tugemann - Civario) | ||||
| Known 17-clue Puzzles | ≈ 49,000 catalogued | ||||
| Digits Used | 1 through 9 (0 not used) | ||||
| Constraint Groups | 27 (9 rows + 9 columns + 9 boxes) | ||||
| Cells per Group | 9 | ||||
| Peers per Cell | 20 (shared row + column + box, minus self, minus duplicates) | ||||
| Symmetry Types | Rotational (180ยฐ), Diagonal, Full (4-fold), None | ||||