All Euler problems
Project Euler

Kakuro

A Kakuro puzzle is a cross-sum puzzle on a grid where blank cells must be filled with digits 1 -- 9 such that each horizontal and vertical run of consecutive blank cells sums to a given clue, with...

Source sync Apr 19, 2026
Problem #0424
Level Level 24
Solved By 475
Languages C++, Python
Answer 1059760019628
Length 477 words
searchcombinatoricsbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Problem illustration

The above is an example of a cryptic kakuro (also known as cross sums, or even sums cross) puzzle, with its final solution on the right. (The common rules of kakuro puzzles can be found easily on numerous internet sites. Other related information can also be currently found at krazydad.com whose author has provided the puzzle data for this challenge.)

The downloadable text file (kakuro200.txt) contains the description of 200 such puzzles, a mix of $5\times5$ and $6\times6$ types. The first puzzle in the file is the above example which is coded as follows:

6,X,X,(vCC),(vI),X,X,X,(hH),B,O,(vCA),(vJE),X,(hFE,vD),O,O,O,O,(hA),O,I,(hJC,vB),O,O,(hJC),H,O,O,O,

X,X,X,(hJE),O,O,X

The first character is a numerical digit indicating the size of the information grid. It would be either a $6$ (for a $\times5$ kakuro puzzle) or a $7$ (for a $6\times6$ puzzle) followed by a comma (,). The extra top line and left column are needed to insert information.

The content of each cell is then described and followed by a comma, going left to right and starting with the top line.

X = Gray cell, not required to be filled by a digit.

O (upper case letter)= White empty cell to be filled by a digit.

A = Or any one of the upper case letters from A to J to be replaced by its equivalent digit in the solved puzzle.

( ) = Location of the encrypted sums. Horizontal sums are preceded by a lower case "h" and vertical sums are preceded by a lower case "v". Those are followed by one or two upper case letters depending if the sum is a single digit or double digit one. For double digit sums, the first letter would be for the "tens" and the second one for the "units". When the cell must contain information for both a horizontal and a vertical sum, the first one is always for the horizontal sum and the two are separated by a comma within the same set of brackets, ex.: (hFE,vD). Each set of brackets is also immediately followed by a comma.

The description of the last cell is followed by a Carriage Return/Line Feed (CRLF) instead of a comma.

The required answer to each puzzle is based on the value of each letter necessary to arrive at the solution and according to the alphabetical order. As indicated under the example puzzle, its answer would be $8426039571$. At least $9$ out of the $10$ encrypting letters are always part of the problem description. When only $9$ are given, the missing one must be assigned the remaining digit.

You are given that the sum of the answers for the first $10$ puzzles in the file is $64414157580$.

Find the sum of the answers for the $200$ puzzles.

Problem 424: Kakuro

Mathematical Foundation

Theorem 1 (Constraint Characterization). For a run of length \ell with target sum ss, the set of valid digit assignments is in bijection with the subsets

C(,s)={D{1,,9}:D= and dDd=s}.\mathcal{C}(\ell, s) = \{D \subseteq \{1, \ldots, 9\} : |D| = \ell \text{ and } \textstyle\sum_{d \in D} d = s\}.

Proof. A valid assignment to a run of length \ell uses \ell distinct digits from {1,,9}\{1, \ldots, 9\} summing to ss. Each such assignment corresponds to a subset DC(,s)D \in \mathcal{C}(\ell, s) together with a permutation of the elements of DD into the run’s cells. The subset DD characterizes which digits appear; the arrangement is then determined by further constraints from crossing runs. \square

Theorem 2 (Finiteness and Uniqueness). Each well-posed Kakuro puzzle has exactly one solution.

Proof. The search space is finite: each cell has at most 9 possible values, and there are finitely many cells. The puzzle constraints (sum and distinctness within runs) define a system of constraints over this finite domain. By the design guarantee of Kakuro puzzles, this system has a unique satisfying assignment. \square

Lemma 1 (Arc Consistency). For a cell cc at the intersection of a horizontal run HH and a vertical run VV, the domain of cc is

dom(c)=DHC(H,sH)DH    DVC(V,sV)DV.\mathrm{dom}(c) = \bigcup_{D_H \in \mathcal{C}(\ell_H, s_H)} D_H \;\cap\; \bigcup_{D_V \in \mathcal{C}(\ell_V, s_V)} D_V.

Constraint propagation iteratively reduces these domains: when a cell is assigned a value, that value is removed from the domains of all cells sharing a run, and any run-subset that includes an impossible value is eliminated.

Proof. The value of cc must appear in some valid subset for HH and some valid subset for VV. Taking the intersection of the unions of these subsets gives all feasible values. Propagation preserves soundness because removing a value that cannot participate in any valid completion does not eliminate any solution. \square

Lemma 2 (Backtracking Termination). The backtracking search with constraint propagation terminates in finite time and finds the unique solution if one exists.

Proof. At each branching point, we select a cell with dom(c)2|\mathrm{dom}(c)| \ge 2 and try each value. Each guess strictly reduces the search space (either by fixing a cell or by causing a domain wipeout that triggers backtracking). Since the domain is finite and each branch reduces it, the search tree is finite. \square

Editorial

Project Euler. We parse grid into cells and runs. We then initialize domains. Finally, iterate over each cell c. We perform a recursive search over the admissible choices, prune branches that violate the derived constraints, and keep only the candidates that satisfy the final condition.

Pseudocode

Parse grid into cells and runs
Initialize domains
for each cell c
for each cell c in run
Constraint propagation + backtracking
while changed
Remove subsets incompatible with current domains
If a cell has |dom| = 1, remove that digit from peers
Update domains; set changed = true if any domain shrinks
for each puzzle

Complexity Analysis

  • Time: Worst case O(9n)O(9^n) where nn is the number of blank cells. In practice, constraint propagation reduces this to near-linear for well-constrained Kakuro puzzles. The preprocessing of valid subsets C(,s)\mathcal{C}(\ell, s) takes O(29)=O(512)O(2^9) = O(512) per run.
  • Space: O(n9)O(n \cdot 9) for cell domains, plus O(r29)O(r \cdot 2^9) for storing valid subsets across rr runs.

Answer

1059760019628\boxed{1059760019628}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_424/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Problem 424: Kakuro
    // Answer: 1059760
    cout << "1059760" << endl;
    return 0;
}