Project Euler Lab - Problem 156

#156 - Counting Digits

● AdvancedOfficial difficulty: 57%PolynomialsTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Starting from zero the natural numbers are written down in base \(10\) like this:
\[0\,1\,2\,3\,4\,5\,6\,7\,8\,9\,10\,11\,12\cdots\]

Consider the digit \(d=1\). After we write down each number \(n\), we will update the number of ones that have occurred and call this number \(f(n,1)\). The first values for \(f(n,1)\), then, are as follows:

\[\begin{array}{cc} n & f(n, 1)\\ \hline 0 & 0\\ 1 & 1\\ 2 & 1\\ 3 & 1\\ 4 & 1\\ 5 & 1\\ 6 & 1\\ 7 & 1\\ 8 & 1\\ 9 & 1\\ 10 & 2\\ 11 & 4\\ 12 & 5 \end{array}\]

Note that \(f(n,1)\) never equals \(3\).
So the first two solutions of the equation \(f(n,1)=n\) are \(n=0\) and \(n=1\). The next solution is \(n=199981\).

In the same manner the function \(f(n,d)\) gives the total number of digits \(d\) that have been written down after the number \(n\) has been written.
In fact, for every digit \(d \ne 0\), \(0\) is the first solution of the equation \(f(n,d)=n\).

Let \(s(d)\) be the sum of all the solutions for which \(f(n,d)=n\).
You are given that \(s(1)=22786974071\).

Find \(\sum s(d)\) for \(1 \le d \le 9\).

Note: if, for some \(n\), \(f(n,d)=n\) for more than one value of \(d\) this value of \(n\) is counted again for every value of \(d\) for which \(f(n,d)=n\).

This problem is taken from Project Euler, Problem 156.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=156. Published Friday, 25th May 2007, 10:00 pm. Solved by 2,940 members at time of mirroring.

Why this is useful

Optimization. The transferable skill is replacing infeasible enumeration with a mathematical reduction - the core move in calibration and large-scale computation (Phases 10, 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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided 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.