3
$\begingroup$

I am looking for the count of digits up to 1 million. Is there a formula for this?

For example, the count of digits where $n = 10$ is $12$:

$0 (+1)$
$1 (+1)$
$2 (+1)$
$3 (+1)$
$4 (+1)$
$5 (+1)$
$6 (+1)$
$7 (+1)$
$8 (+1)$
$9 (+1)$
$10 (+2)$

  • 2
    I suggest counting the digits in the units place, the tens place etc ...2012-09-04

2 Answers 2

2

Let $N_{m}$ be the total count of base-$10$ digits in the numbers from $0$ to $m-1$. There are $m$ numbers with at least $1$ digit in that range. If $m\ge 10$, then there are $m-10$ numbers with at least $2$ digits in that range; if $m \ge 100$, then there are $m-100$ numbers with at least $3$ digits; and so on. The total count is then $ \begin{eqnarray} N_{m}&=&1+\sum_{i=0}^{\lfloor \log_{10} m\rfloor}\left(m-10^{i}\right)\\&=&m\left(\lfloor\log_{10} m \rfloor+ 1\right)-\frac{10}{9}\left(10^{\lfloor\log_{10} m \rfloor} - 1\right),\end{eqnarray}$ after summing the geometric series. For example, the count from $0$ through $10$ is $N_{11}=12$; the count through one million is $N_{1000001}=5888897.$ This expression generalizes readily to digits in base $b$: $ N^{(b)}_{m} = m\left(\lfloor\log_{b} m \rfloor+ 1\right)-\frac{b}{b-1}\left(b^{\lfloor\log_{b} m \rfloor} - 1\right).$

5

$ 1+ \sum_{i=1}^n \lfloor \log_{10} i + 1 \rfloor $ where the $1$ infront is to account for the number $0$.

If you are looking for a compact form - that relieves you of summing the terms individually - you can aggregate terms:

  • if $n<10$ then result $= n+1$
  • if $10\le n < 100$ then result $= 10+2\cdot (n-9)$
  • if $100\le n < 1000$ then result $= 190 + 3\cdot (n-90)$
  • if $1000\le n < 10000$ then result $= 190 + 3*900 + 4\cdot (n-900) = 2890 + 4\cdot (n-900)$ and so on

In general: if $n$ has $m=\lfloor \log_{10} n +1 \rfloor$ digits, then the result is $ 10\cdot 1 + 90\cdot 2 + 900\cdot 3 + \dots + 9\cdot 10^{m-2}\cdot (m-1) + m\cdot (n-9\cdot 10^{m-2})$

  • 0
    @Peter you are right. thanks for pointing it out.2012-09-04