All Euler problems
Project Euler

Numbers in Decimal Expansions

Let p = p_1 p_2 p_3... be an infinite random decimal sequence where each digit is uniform on {0,..., 9}. For a positive integer n with d digits, let k be the smallest index such that p_k p_(k+1)......

Source sync Apr 19, 2026
Problem #0316
Level Level 17
Solved By 787
Languages C++, Python
Answer 542934735751917735
Length 327 words
probabilitysequencebrute_force

Problem Statement

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

Let \(p = p_1 p_2 p_3 \cdots \) be an infinite sequence of random digits, selected from \(\{0,1,2,3,4,5,6,7,8,9\}\) with equal probability.

It can be seen that \(p\) corresponds to the real number \(0.p_1 p_2 p_3 \cdots \)

It can also be seen that choosing a random real number from the interval \([0,1)\) is equivalent to choosing an infinite sequence of random digits selected from \(\{0,1,2,3,4,5,6,7,8,9\}\) with equal probability.

For any positive integer \(n\) with \(d\) decimal digits, let \(k\) be the smallest index such that \(p_k, p_{k + 1}, \dots , p_{k + d - 1}\) are the decimal digits of \(n\), in the same order.

Also, let \(g(n)\) be the expected value of \(k\); it can be proven that \(g(n)\) is always finite and, interestingly, always an integer number.

For example, if \(n = 535\), then

for \(p = 31415926\mathbf {535}897\cdots \), we get \(k = 9\)

for \(p = 35528714365004956000049084876408468\mathbf {535}4\cdots \), we get \(k = 36\)

etc and we find that \(g(535) = 1008\).

Given that \(\displaystyle \sum _{n = 2}^{999} g \left (\left \lfloor \frac {10^6} n \right \rfloor \right ) = 27280188\), find \(\displaystyle \sum _{n = 2}^{999999} g \left (\left \lfloor \frac {10^{16}} n \right \rfloor \right )\).

Note: \(\lfloor x \rfloor \) represents the floor function.

Problem 316: Numbers in Decimal Expansions

Mathematical Foundation

Theorem (Conway Leading Number / Waiting Time Formula). Let s=d1d2dms = d_1 d_2 \cdots d_m be a target string over alphabet {0,,A1}\{0, \ldots, A-1\} with A=10A = 10. The expected number of characters one must examine before ss appears completely is:

W(s)=k=1mAk1[d1dk=dmk+1dm].W(s) = \sum_{k=1}^{m} A^k \cdot \mathbf{1}\bigl[d_1 \cdots d_k = d_{m-k+1} \cdots d_m\bigr].

Proof. (Martingale / Casino argument.) Consider an infinite sequence of gamblers. Gambler jj arrives at position jj and bets $1 that pj=d1p_j = d_1. If correct (probability 1/A1/A), the stake is multiplied by AA (fair odds) and wagered on pj+1=d2p_{j+1} = d_2, etc. If any bet fails, the gambler is ruined.

Define the cumulative wealth process Xt=j=1t(payout of gambler j by time t)tX_t = \sum_{j=1}^{t} (\text{payout of gambler } j \text{ by time } t) - t. This is a martingale with E[Xt]=0E[X_t] = 0.

Let TT be the first time the pattern ss completes. At time TT:

  • Gambler jj has a nonzero payout iff the substring pjpTp_j \cdots p_T equals a suffix of ss that matches a prefix. Specifically, the gambler starting at position Tk+1T - k + 1 (for 1km1 \le k \le m) has payout AkA^k iff d1dk=dmk+1dmd_1 \cdots d_k = d_{m-k+1} \cdots d_m.
  • The total amount paid in is TT.

By the optional stopping theorem (the martingale is bounded in expectation and E[T]<E[T] < \infty):

E[T]=E[total payout at time T]=k=1mAk1[prefixk=suffixk].E[T] = E[\text{total payout at time } T] = \sum_{k=1}^{m} A^k \cdot \mathbf{1}[\text{prefix}_k = \text{suffix}_k]. \quad \square

Lemma (Relationship to g(n)g(n)). W(s)W(s) gives the expected position of the last character of the first occurrence. Since g(n)g(n) is the expected position of the first character:

g(n)=W(s)d+1g(n) = W(s) - d + 1

where ss is the decimal representation of nn and d=sd = |s|.

Proof. If the pattern first completes at position TT (the last character), the first character is at position Td+1T - d + 1. Linearity of expectation gives g(n)=E[T]d+1=W(s)d+1g(n) = E[T] - d + 1 = W(s) - d + 1. \square

Verification. For n=535n = 535, s="535"s = \texttt{"535"}, d=3d = 3:

  • k=1k = 1: prefix “5” = suffix “5” +10\checkmark \Rightarrow +10
  • k=2k = 2: prefix “53” \neq suffix “35” ×\times
  • k=3k = 3: prefix “535” = suffix “535” +1000\checkmark \Rightarrow +1000

W=1010W = 1010, so g(535)=10103+1=1008g(535) = 1010 - 3 + 1 = 1008. \checkmark

Editorial

g(n) for integer n with decimal string s of length d: g(n) = W(s) - d + 1 where W(s) = sum of 10^k for each k in 1..d where the length-k prefix of s equals the length-k suffix of s (Conway Leading Number). Compute: sum of g(floor(10^16 / n)) for n = 2 to 999999. We return total.

Pseudocode

Input: N_max = 999999, T = 10^16
Output: sum of g(floor(T/n)) for n = 2 to N_max
total = 0
For n = 2 to N_max:
Return total

Complexity Analysis

  • Time: O(Nd2)O(N \cdot d^2) where N=999,998N = 999{,}998 and d16d \le 16 (digits of 1016/n\lfloor 10^{16}/n \rfloor). The inner loop over kk is O(d)O(d), and each prefix-suffix comparison is O(k)O(k), giving O(d2)O(d^2) per value. With KMP preprocessing: O(Nd)O(N \cdot d).
  • Space: O(d)O(d) for the string representation.

Answer

542934735751917735\boxed{542934735751917735}

Code

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

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

/*
 * Problem 316: Numbers in Decimal Expansions
 *
 * g(n) = W(s) - d + 1 where W(s) = sum of 10^k for each k where the
 * length-k prefix of s equals the length-k suffix, and d = number of digits.
 *
 * Compute sum of g(floor(10^16 / n)) for n = 2 to 999999.
 */

int main(){
    // Use unsigned long long; 10^16 fits in ull (max ~1.8 * 10^19)
    const unsigned long long P = 10000000000000000ULL; // 10^16

    long long total = 0;

    for(int n = 2; n <= 999999; n++){
        unsigned long long m = P / n;

        // Convert m to string
        char buf[20];
        int d = sprintf(buf, "%llu", m);

        // Compute W(s) = sum of 10^k for matching prefix/suffix
        long long W = 0;
        for(int k = 1; k <= d; k++){
            // Check if prefix of length k equals suffix of length k
            // prefix: buf[0..k-1], suffix: buf[d-k..d-1]
            bool match = true;
            for(int i = 0; i < k; i++){
                if(buf[i] != buf[d - k + i]){
                    match = false;
                    break;
                }
            }
            if(match){
                long long pw = 1;
                for(int j = 0; j < k; j++) pw *= 10;
                W += pw;
            }
        }

        total += W - d + 1;
    }

    cout << total << endl;
    return 0;
}