All Euler problems
Project Euler

Numbers for Which No Three Consecutive Digits Have a Sum Greater Than a Given Value

How many 20-digit numbers n (without any leading zeros) exist such that no three consecutive digits of n have a sum greater than 9?

Source sync May 21, 2026
Problem #0164
Level Level 06
Solved By 6,571
Languages C++, Python
Answer 378158756814587
Length 382 words
dynamic_programminglinear_algebrasequence

Problem Statement

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

How many \(20\) digit numbers \(n\) (without any leading zero) exist such that no three consecutive digits of \(n\) have a sum greater than \(9\)?

Problem 164: Numbers for Which No Three Consecutive Digits Have a Sum Greater Than a Given Value

Mathematical Development

Theorem 1 (Recurrence). Let f(ℓ,a,b)f(\ell, a, b) denote the number of ℓ\ell-digit suffixes (allowing leading zeros) such that the first two digits are aa and bb, and every three consecutive digits sum to at most 9. Then:

f(ℓ,a,b)={1if ℓ=2,∑d=09−a−bf(ℓ−1,b,d)if ℓ≥3.f(\ell, a, b) = \begin{cases} 1 & \text{if } \ell = 2, \\ \displaystyle\sum_{d=0}^{9 - a - b} f(\ell - 1, b, d) & \text{if } \ell \geq 3. \end{cases}

Proof. For ℓ=2\ell = 2, the suffix is exactly the two digits a,ba, b and there are no three-consecutive-digit windows, so f(2,a,b)=1f(2, a, b) = 1.

For ℓ≥3\ell \geq 3, the suffix is a,b,d,…a, b, d, \ldots where dd is the third digit. The constraint on the first window requires a+b+d≤9a + b + d \leq 9, i.e., d≤9−a−bd \leq 9 - a - b. Since a+b≤9a + b \leq 9 (otherwise no valid suffix exists at all, and the sum is empty), we have d∈{0,1,…,9−a−b}d \in \{0, 1, \ldots, 9 - a - b\}. Choosing dd and continuing from the pair (b,d)(b, d) with ℓ−1\ell - 1 remaining digits gives the recurrence. □\square

Theorem 2 (Answer formula). The number of 20-digit numbers with no three consecutive digits summing above 9 is:

Answer=∑a=19∑b=09−af(20,a,b)\text{Answer} = \sum_{a=1}^{9} \sum_{b=0}^{9-a} f(20, a, b)

Proof. The first digit aa ranges over {1,…,9}\{1, \ldots, 9\} (no leading zeros). The second digit bb must satisfy a+b≤9a + b \leq 9 (otherwise the pair (a,b)(a, b) cannot begin any valid triple), so b∈{0,…,9−a}b \in \{0, \ldots, 9 - a\}. The remaining 18 digits are counted by f(20,a,b)f(20, a, b). □\square

Lemma 1 (State space size). The number of valid states (a,b)(a, b) with 0≤a,b≤90 \leq a, b \leq 9 and a+b≤9a + b \leq 9 is exactly (112)=55\binom{11}{2} = 55.

Proof. We count pairs (a,b)(a, b) with a+b≤9a + b \leq 9:

∑a=09(10−a)=10+9+⋯+1=10⋅112=55.\sum_{a=0}^{9} (10 - a) = 10 + 9 + \cdots + 1 = \frac{10 \cdot 11}{2} = 55.

□\square

Lemma 2 (Matrix exponentiation alternative). Define the 55×5555 \times 55 transition matrix MM where rows and columns are indexed by valid states (a,b)(a,b), and M(a,b),(b,d)=1M_{(a,b),(b,d)} = 1 if a+b+d≤9a + b + d \leq 9 (i.e., d≤9−a−bd \leq 9 - a - b), and 0 otherwise. Then f(ℓ,a,b)=∑(c,e)(Mℓ−2)(a,b),(c,e)f(\ell, a, b) = \sum_{(c,e)} (M^{\ell-2})_{(a,b),(c,e)}. This allows computation in O(553log⁡ℓ)O(55^3 \log \ell) time.

Proof. Each multiplication by MM extends the suffix by one digit. After ℓ−2\ell - 2 multiplications, we sum over all terminal states. □\square

Editorial

The natural state is the last two digits already placed. Once those are known, the next digit is allowed to range only up to 9−a−b9-a-b, so the rest of the prefix no longer matters. That makes this a small dynamic program on ordered pairs (a,b)(a,b) rather than on full 20-digit strings.

The implementation starts by enumerating all valid first-two-digit pairs, respecting the no-leading-zero rule. It then extends the number one digit at a time with a rolling DP table: each state (a, b) pushes its count to all next states (b, c) satisfying a+b+c≤9a+b+c \le 9. After the twentieth digit has been placed, summing the remaining state counts gives the answer.

Pseudocode

Create a DP table indexed by the last two digits.

Initialize all two-digit prefixes:
    the first digit must be between 1 and 9,
    the second digit must allow at least one valid continuation,
    so only pairs with $a+b \le 9$ receive count 1.

For each remaining digit position from 3 through 20:
    build a fresh table.
    For every current pair $(a,b)$ with nonzero count:
        try every digit $c$ from 0 up to $9-a-b$.
        Add the count from $(a,b)$ into the next state $(b,c)$.
    Replace the old table with the new one.

Return the sum of all counts in the final table.

Complexity Analysis

  • Time: O(L⋅S⋅D)O(L \cdot S \cdot D) where L=20L = 20 (digit positions), S=55S = 55 (states), and D≤10D \leq 10 (digit choices per state). This gives O(20⋅55⋅10)=O(11,000)O(20 \cdot 55 \cdot 10) = O(11{,}000).
  • Space: O(S)=O(55)O(S) = O(55) with a rolling array (two layers of DP).

Answer

378158756814587\boxed{378158756814587}

Code

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

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

// Problem 164: No three consecutive digits sum > 9
// DP with state (last two digits)

int main() {
    const int DIGITS = 20;

    // dp[a][b] = count of valid numbers ending in digits a, b
    long long dp[10][10] = {};

    // First digit: 1-9, second digit: 0 to 9-first
    for (int a = 1; a <= 9; a++)
        for (int b = 0; b <= 9 - a; b++)
            dp[a][b] = 1;

    // Process digits 3 through 20
    for (int pos = 3; pos <= DIGITS; pos++) {
        long long ndp[10][10] = {};
        for (int a = 0; a <= 9; a++) {
            for (int b = 0; b <= 9 - a; b++) {
                if (dp[a][b] == 0) continue;
                int maxc = 9 - a - b;
                for (int c = 0; c <= maxc; c++) {
                    ndp[b][c] += dp[a][b];
                }
            }
        }
        memcpy(dp, ndp, sizeof(dp));
    }

    long long ans = 0;
    for (int a = 0; a <= 9; a++)
        for (int b = 0; b <= 9; b++)
            ans += dp[a][b];

    cout << ans << endl;
    return 0;
}