If you write down all the numbers from 1 to n, how many digits would you have written down?

171 Views Asked by At

I've seen the question for numbers like 50, 100 or 1000, but not for $n$. Although I found a formula that might be the answer, but I don't know the name of it or the proof for it. I couldn't find it anywhere on internet as well. Here it is: $$d\times(n+1) - \frac{10^d - 1}{9}$$ $d$ is the number of digits $n$ has.

For example, for $n = 16$:

Answer: $\displaystyle2\times (16 + 1) - \frac{10^2 - 1}{9} = 34 - 11 = 23$

Also: $12345678910111213141516\rightarrow 23 \text{ digits}$

Looking forward for replies!

Thanks!

Edit 1: For those who also want to know how $d$ is calculated: $d = \lfloor\log_{10}n\rfloor + 1$

1

There are 1 best solutions below

2
On BEST ANSWER

Well, it's clear we write down a "ones" digit for each $n$. And we write down a "tens" digit for all but the first $9$ so we do $n-9$ "tens" digits. And $n-99$ hundreds digits and so on.

So if $d$ is the number of digits as you put it, we have the number of digits written is.

$n + (n-9) + (n-99) + ...... + (n-\underbrace{999....9}_{d-1})=$

$n\times d - (9 + 99 + ..... + \underbrace{999....9}_{d-1})=$

$n\times d- ((10-1) + (100-1) + ...... + (10^d -1))=$

$n\times d - [(10 + 100 + ..... + 10^{d-1}) -(d-1)]=$

$n\times d - [(10 + 100 + ..... + 10^{d-1})+1] + d=$

$n\times d + d - [1+10 + 100 + ..... + 10^{d-1}] =$

$(n+1)\times d - [\underbrace{111.....1}_d] =$

$(n+1)\times d - \frac {10^{d}-1}9$

Nice job!