2 Answers
2

Given a number $n$, the total number of digits to represent $1$ to $n$ can be obtained as follows.

Let $k = \left \lfloor \log_{10}(n) \right \rfloor$. Then the number of digits from $1$ to $10^k-1$ is given by
$$\sum_{l=1}^k l \cdot 9 \cdot 10^{l-1} = k \cdot 10^k - \dfrac{10^k-1}9$$
Hence, the number of digits needed to list $1$ to $n$ is
$$f(n) = \underbrace{k \cdot 10^k - \dfrac{10^k-1}9}_{\text{Number of digits from $1$ to $10^k-1$}} + \overbrace{(k+1)\left(n+1-10^k\right)}^{\text{Number of digits from $10^k$ to $n$}} = (n+1)(k+1) - \dfrac{10^{k+1}-1}9$$
where $k = \left \lfloor \log_{10}(n) \right \rfloor$. The function you are after is the inverse of $f(n)$.

You can calculate how many digits are represented by each decade. The one digit numbers supply the first $9$ terms, the two digit numbers supply $2\cdot 90=180$, the three digit numbers the next $3\cdot 900=2700$ and so on. If you want to find the digit at position $n$, first find the decade you are in. The $d$ digit numbers end at $\sum_{i=1}^d 9\cdot i \cdot 10^{i-1}=9\frac{d10^{d+1}-(d+1)x^d+1}{81}=\frac{d10^{d+1}-(d+1)x^d+1}{9}$. Then figure out where you are in the decade. For example, if you want the $5000$th term, the one, two, and three digit numbers fill $2889$, so we want the $2111$st term of the four digit numbers. This is part of the $\lceil \frac {2111}4 \rceil = 528$th number, which is $1527$. Then it is the $2111-1 \pmod 4 +1=3$rd digit of this, so $2$