# Function that returns the sum of the number of digits from 1 to n.

I want an algebraically closed-form version of this: $$\displaystyle f(x)=\sum_{n=1}^{x}\left\lfloor \log_{10}\left(n\right) + 1 \right\rfloor$$

Here are some examples to illustrate what I'm asking for.

$$f(1) = 1\\ f(2) = 1 + 1 = 2\\ f(3) = 1 + 1 + 1 = 3\\$$

Basically, $$f(x) = x \iff \lfloor log_{10}{x} \rfloor=0$$

Here are some examples of $$f(x)$$ for slightly larger numbers.

$$f(10)=11\\ f(11)=13\\ f(12)=15\\ f(100)=192\\$$

Is it possible to devise an algebraically closed form of this?

• It is easier to derive a formula for the ranges $10-100$ , $100-1\ 000$ , $1\ 000-10\ 000$ and so on. Commented Oct 8, 2022 at 10:34
• @Peter how would one go about doing that? Commented Oct 26, 2022 at 20:56
• For $1\le x\le9$ you have $f(x)=x$; for $10\le x\le99$ you have $f(x)=f(9)+2(x-9)$; for $100\le x\le999$ you have $f(x)=f(99)+3(x-99)$; for $1000\le x\le9999$ you have $f(x)=f(999)+4(x-999)$, and so on. Commented Oct 27, 2022 at 15:41

Take $$x=9876$$, for example, then $$\sum_{n=1}^{9876}\lfloor\log(x)\rfloor+1=\sum_{n=1}^{9}1+\sum_{n=10}^{99}2+\sum_{n=100}^{999}3+\sum_{n=1000}^{9876}4$$ Extending the pattern, we get that $$f(x)=\left(\sum_{k=0}^{\lfloor\log(x)\rfloor-1}\sum_{n=10^k}^{10^{k+1}-1}(k+1)\right)+\sum_{n=10^{\lfloor\log(x)\rfloor}}^{x}\lfloor\log(x)\rfloor+1$$ $$f(x)=\left(\sum_{k=0}^{\lfloor\log(x)\rfloor-1}9(k+1)\cdot10^k\right)+(x-10^{\lfloor\log(x)\rfloor}+1)(\lfloor\log(x)\rfloor+1)$$ $$f(x)=10^{\lfloor\log(x)\rfloor}\lfloor\log(x)\rfloor-\frac{1}{9}\left(10^{\lfloor\log(x)\rfloor}-1\right)+(x-10^{\lfloor\log(x)\rfloor}+1)(\lfloor\log(x)\rfloor+1)$$