Squares Sudoku
Squares Sudoku is Killer Sudoku, restricted to perfect squares. The base puzzle is an ordinary Sudoku grid: each row, column, and box holds the digits through exactly once. Onto this base sits a partition of the grid into cages, each a connected polyomino of one or more cells, with one further demand: the digits in each cage sum to a perfect square. The squares allowed by convention are the small ones a -to- digit sum can reach, namely , , , and .
The puzzle sits in the family of Killer-style Sudoku variants that emerged in the wake of the early-s Sudoku boom, designed to add an arithmetic dimension without losing the purity of the underlying Latin-square logic. Killer Sudoku itself attaches an explicit sum value to each cage; Squares Sudoku replaces those values with the simple membership test “the cage’s sum is one of ”. The model gains an or where Killer Sudoku has an , and the puzzle becomes strictly harder for human solvers because the cage’s target is hidden behind an enumeration.
For a CP solver the change is small. The Killer-cage equation becomes , encoded as a one-hot Boolean over the four allowed targets with a reified equality on each. The solver searches over the joint space of digit assignment and target-square selection.
Rules and a small instance
A Squares Sudoku puzzle is a grid of cells. The grid carries a partition into cages, each a connected polyomino of one or more cells. The puzzle’s constraints are the three Sudoku constraints from Chapter together with a fourth.
Each row contains each digit from to exactly once.
Each column contains each digit from to exactly once.
Each box contains each digit from to exactly once.
For every cage, the sum of its cells’ digits lies in .
Unlike Killer Sudoku, the cage-sum target is not pre-declared; the solver must choose, for each cage, which perfect square the cage will hit. The fourth rule is therefore a kind of “meta-clue”: it constrains the structure of the digit assignment without committing to a specific target per cage.
Figure 8.1 shows the instance from the recreational mathematics archive: cages of varying shape, no givens, and the perfect-square sum rule applied to each cage.
The programming model
Let for . The Sudoku layer is the three families of all-different constraints from Chapter . The cage layer adds, for each cage with cell set , the constraint
In CP-SAT this is modelled with a one-hot Boolean for the four allowed targets: the second equation reified against the indicator through OnlyEnforceIf. With cages and four allowed targets, the additional Boolean count is , modest by CP-SAT standards.
A CP-SAT solver
from ortools.sat.python import cp_model
SQUARES = [4, 9, 16, 25]
def solve_squares_sudoku(cages):
n = 9
m = cp_model.CpModel()
x = [[m.NewIntVar(1, 9, f"x{r}{c}")
for c in range(n)] for r in range(n)]
for r in range(n): m.AddAllDifferent(x[r])
for c in range(n):
m.AddAllDifferent([x[r][c] for r in range(n)])
for br in range(3):
for bc in range(3):
block = [x[br*3+i][bc*3+j]
for i in range(3) for j in range(3)]
m.AddAllDifferent(block)
for cage in cages:
s = sum(x[r][c] for (r, c) in cage)
bs = [m.NewBoolVar("") for _ in SQUARES]
m.Add(sum(bs) == 1)
for b, t in zip(bs, SQUARES):
m.Add(s == t).OnlyEnforceIf(b)
sv = cp_model.CpSolver()
status = sv.Solve(m)
if status not in (cp_model.OPTIMAL, cp_model.FEASIBLE):
raise RuntimeError(
"no solution: " + sv.StatusName(status))
return [[sv.Value(x[r][c])
for c in range(n)] for r in range(n)]
The puzzle in Figure 8.1 solves uniquely in about milliseconds, returning Figure 8.2.
A spot-check confirms the rule. The two-cell cage holds , summing to . The four-cell cage holds , summing to . The two-cell cages , , each hold pairs summing to , forcing the rare partition (since is forbidden by row distinctness).
The arithmetic of the constraint
Before the Sudoku layer is imposed, two-cell sums and admit the multisets and respectively. A connected two-cell cage shares a row or column, so Sudoku excludes the repeated digits. Sum admits ; sum is impossible even without Sudoku (the maximum is ).
For three cells, sum requires , sum admits seven multisets, and sum admits two: and . Whether repeated digits survive Sudoku depends on cage geometry. In an L-shaped cage across a box boundary, two cells can share neither a row, column nor box. Arithmetic and Sudoku restrictions must therefore be applied together.
The choice of allowed squares matters. With allowed, single-cell cages of value would be permitted but fixed-digit clues constraining those cells to . With , larger cages of five or more cells (whose sums can exceed ) become viable. The conventional set keeps the puzzle restricted to interesting cage shapes.
Sources. Killer Sudoku, the parent of the Squares Sudoku variant, was popularised in Japan by Tetsuya Nishio and in the West through The Times of London in ; the Japanese name is sankaku or Sumdoku. The specific perfect-squares restriction used here is documented in the recreational mathematics archive accompanying this book; the unique-solution check was performed by enumeration with CP-SAT, and the puzzle does have a single completion under the constraint set . The instance is original to that archive.