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)......
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 )\).
Problem 316: Numbers in Decimal Expansions
Mathematical Foundation
Theorem (Conway Leading Number / Waiting Time Formula). Let be a target string over alphabet with . The expected number of characters one must examine before appears completely is:
Proof. (Martingale / Casino argument.) Consider an infinite sequence of gamblers. Gambler arrives at position and bets $1 that . If correct (probability ), the stake is multiplied by (fair odds) and wagered on , etc. If any bet fails, the gambler is ruined.
Define the cumulative wealth process . This is a martingale with .
Let be the first time the pattern completes. At time :
- Gambler has a nonzero payout iff the substring equals a suffix of that matches a prefix. Specifically, the gambler starting at position (for ) has payout iff .
- The total amount paid in is .
By the optional stopping theorem (the martingale is bounded in expectation and ):
Lemma (Relationship to ). gives the expected position of the last character of the first occurrence. Since is the expected position of the first character:
where is the decimal representation of and .
Proof. If the pattern first completes at position (the last character), the first character is at position . Linearity of expectation gives .
Verification. For , , :
- : prefix “5” = suffix “5”
- : prefix “53” suffix “35”
- : prefix “535” = suffix “535”
, so .
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: where and (digits of ). The inner loop over is , and each prefix-suffix comparison is , giving per value. With KMP preprocessing: .
- Space: for the string representation.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 316: Numbers in Decimal Expansions
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.
"""
def g(n):
"""Compute g(n) - expected starting position of first occurrence in random decimal expansion."""
s = str(n)
d = len(s)
W = 0
for k in range(1, d + 1):
if s[:k] == s[d-k:]:
W += 10**k
return W - d + 1
def solve():
P = 10**16
total = 0
for n in range(2, 1000000):
m = P // n
total += g(m)
print(total)
if __name__ == "__main__":
solve()