Inside the Solver

How the Sudoku Solver Works: Logic + Backtracking Explained

By LK Forge  ·  6 min read  · 

Type a puzzle into the Sudoku solver and it fills the whole grid in one click — but it does not guess wildly, and it is not a language model. It runs two engines the way a good human solver does: it first exhausts pure logic, and only where logic runs out does it fall back to a backtracking search. To show where that hand-off happens we ran the shipped engine headless across 240 generated puzzles. The result is clean: easy puzzles are solved entirely by two human techniques, while on the hardest puzzles logic fills about 62% of the blanks before search takes over — and every one is done in under a millisecond.

Two Solvers in One

Sudoku has a special property: a proper puzzle is deducible — there is always a next square you can prove, without guessing, if you know enough techniques. So the solver tries deduction first and keeps search in reserve:

The Logic Pass: Naked and Hidden Singles

The solver's deduction uses the two techniques every Sudoku beginner learns, applied relentlessly:

Each placement removes candidates elsewhere, which can expose the next single — the familiar cascade where one move unlocks the whole corner. The solver loops these two rules until neither finds anything more, then reports what is left.

The Backtracking Search

When logic stalls, the solver switches to depth-first backtracking: pick an empty cell, try a legal candidate, and recurse. If a later cell runs out of candidates the grid is unsolvable down that path, so it undoes the last guess and tries the next digit. Because every placement must already respect Sudoku's rules, dead ends are caught almost immediately and the search never wanders far. The same recursion, capped at two solutions, is how the Check button proves a puzzle is proper — a valid Sudoku has exactly one solution, and finding a second means the clues are ambiguous.

Where Logic Ends and Search Begins

We measured the hand-off directly. For 60 generated puzzles at each of four difficulties, we ran the solver's own logic pass until it stalled and recorded how much of the grid it had filled, then let the backtracking search finish. The share of each puzzle that pure logic can crack drops steadily as clues are removed:

100 100 Easy 94 88 Medium 74 57 Hard 62 45 Expert % of blanks logic fills % of puzzles logic solves outright lkforge.com
Easy puzzles fall entirely to naked and hidden singles; by Expert, logic fills roughly 62% of the blanks and solves fewer than half of puzzles outright — the rest is finished by the backtracking search.

Every number

The full measurement — 60 seeded puzzles per tier, run through the shipped engine:

DifficultyAvg cluesLogic solves outrightBlanks logic fillsSolve timeUnique
Easy40.0100%100%0.04 ms100%
Medium32.088%94%0.10 ms100%
Hard27.057%74%0.42 ms100%
Expert24.645%62%0.61 ms100%

"Logic solves outright" is the share of puzzles finished by naked and hidden singles alone; "blanks logic fills" is the average share of empty cells placed before logic stalls. Every generated puzzle had exactly one solution. Single core, no time limit.

A separate headless sweep comparing naive backtracking, MRV backtracking and constraint propagation — where the MRV heuristic cuts search ~163× vs naive and propagation solves a typical puzzle in ~2 nodes — is published as the LK Forge Sudoku Solver Benchmark dataset on Hugging Face.

The 17-clue limit. Mathematicians proved in 2012 that no uniquely solvable Sudoku can have fewer than 17 clues. We fed the solver one of those minimal 17-clue puzzles: it confirms the solution is unique and completes the grid in about 15 milliseconds — the backtracking search working hardest, on the least information a proper Sudoku can carry.

Reproduce It Yourself

Every number above comes from the solver's exact shipped engine, run headless. The engine is a single JavaScript file you can load with Node:

import fs from 'fs'; import vm from 'vm';
// grab the shipped engine: lkforge.com/games/sudoku/sudoku-core.js
const code = fs.readFileSync('sudoku-core.js', 'utf8');
const ctx = {}; vm.createContext(ctx);
vm.runInContext(code, ctx);
const { makeSudoku, mulberry32 } = ctx.LKSudoku;
const S = makeSudoku(3, 3);                       // standard 9x9

const { puzzle } = S.generate(mulberry32(42), 24);  // an expert puzzle
let g = puzzle.slice(), h, filled = 0;
while ((h = S.hint(g))) { g[h.cell] = h.val; filled++; }   // logic pass
console.log('placed by logic:', filled);
console.log('unique?', S.countSolutions(puzzle, 2) === 1);
console.log('full solution:', S.solve(puzzle) !== null);

The same solve(), hint() and countSolutions() are exactly what the buttons in the Sudoku solver call.

Frequently Asked Questions

Is there a Sudoku solver that shows the answer?

Yes — this page's Sudoku solver is a free in-browser tool. Type any puzzle into the grid and it solves it in one click, checks whether it has a single valid answer, or steps you through the logic one hint at a time. No sign-up, and it runs entirely in your browser.

What algorithm does the Sudoku solver use?

Two engines working together: a logic pass that finds naked singles (a cell with only one candidate) and hidden singles (a digit that fits only one cell in a row, column or box), and a depth-first backtracking search that completes any valid puzzle. It is not a generative-AI or LLM wrapper.

How does the solver handle very hard puzzles?

Measured across 60 puzzles per tier, easy puzzles are solved completely by naked and hidden singles, while on expert puzzles that logic fills about 62% of the blanks before it stalls and the backtracking search finishes the rest — every tier in well under a millisecond.

Can the solver check if my Sudoku has a unique solution?

Yes — it counts solutions and tells you whether the puzzle has exactly one. A proper Sudoku has a single valid answer; more than one means the clues are ambiguous.

Can it solve a 17-clue Sudoku?

Yes — 17 is the proven minimum number of clues a uniquely solvable Sudoku can have. The solver confirms such a puzzle has exactly one solution and completes it in about 15 milliseconds.

Is the Sudoku solver free?

Completely free, with no account required. The engine runs client-side in your browser, so nothing you type is uploaded.

OPEN THE SUDOKU SOLVER