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 Apr 19, 2026
Problem #0164
Level Level 06
Solved By 6,571
Languages C++, Python
Answer 378158756814587
Length 321 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 Foundation

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=09abf(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+d9a + b + d \leq 9, i.e., d9abd \leq 9 - a - b. Since a+b9a + b \leq 9 (otherwise no valid suffix exists at all, and the sum is empty), we have d{0,1,,9ab}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=19b=09af(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+b9a + b \leq 9 (otherwise the pair (a,b)(a, b) cannot begin any valid triple), so b{0,,9a}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 0a,b90 \leq a, b \leq 9 and a+b9a + b \leq 9 is exactly (112)=55\binom{11}{2} = 55.

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

a=09(10a)=10+9++1=10112=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+d9a + b + d \leq 9 (i.e., d9abd \leq 9 - a - b), and 0 otherwise. Then f(,a,b)=(c,e)(M2)(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

Count 20-digit numbers where no three consecutive digits sum > 9. Dynamic programming with state = (last two digits). We use dynamic programming with a rolling array. We then state: (last_two_digits) = (a, b) with a + b <= 9. Finally, initialize for length 2.

Pseudocode

DP with rolling array
State: (last_two_digits) = (a, b) with a + b <= 9
Initialize for length 2
Extend from length 2 to length 20
Sum over starting digits a >= 1

Complexity Analysis

  • Time: O(LSD)O(L \cdot S \cdot D) where L=20L = 20 (digit positions), S=55S = 55 (states), and D10D \leq 10 (digit choices per state). This gives O(205510)=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;
}