Project Euler Lab - Problem 316

#316 - Numbers in Decimal Expansions

● ResearchOfficial difficulty: 67%RandomnessTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

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.
This problem is taken from Project Euler, Problem 316.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=316. Published Saturday, 25th December 2010, 01:00 pm. Solved by 798 members at time of mirroring.

Why this is useful

Probability Statistics. Expectation and state-based probability reasoning underpin pricing, risk, and statistical inference (Phases 7, 11, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.