Sum of digits of number from 1 to n 
Is there any general formula for calculating the sum of digits of number from $1$ to $n$?
$n < 10^9$

 A: To make the counting easier I will consider the sum of the digits of all integers from $0$ to $n$ - clearly this does not change the answer.  Denote this sum by $D(n)$.
Edit.  Here is an improved answer.  As a couple of people thought my previous answer was worth upvoting I have not deleted it, see below.
Write $n=10q+r$ where $q\ge0$ and $0\le r\le 9$.  The digits of all numbers from $0$ to $n$ can be split into the following four sets:


*

*the units digits of numbers from $0$ to $10q-1$;  

*the other digits of numbers from $0$ to $10q-1$;  

*the units digits of numbers from $10q$ to $10q+r$;  

*the other digits of numbers from $10q$ to $10q+r$.


The first group consists of the digits from $0$ to $9$ repeated $q$ times each.  The second consists of the digits of the numbers $0,1,\ldots,q-1$ repeated $10$ times each.  The third consists of the digits $0,1,\ldots,r$.  The fourth consists of the digits of $q$ repeated $r+1$ times.  So if we write $d(q)$ for the sum of the digits of $q$, we obtain the recursive formula
$$D(10q+r)=45q+10D(q-1)+\frac{r(r+1)}{2}+(r+1)d(q)\ .$$

Here is my previous answer.  First consider all numbers from $0$ to $10^k-1$.  There are $10^k$ such numbers, each having $k$ digits if we allow leading zeros.  This gives $k10^k$ digits altogether, and they are equally distributed among the $10$ possibilities, $k10^{k-1}$ of each digit.  So
$$D(10^k-1)=k10^{k-1}(0+1+\cdots+9)=(45k)10^{k-1}\ .$$
Next consider $n=a10^k-1$, where $1\le a\le9$.  All of the above occur $a$ times over; there are also the initial digits $0,1,\ldots,a-1$ occurring $10^k$ times each.  So
$$D(a10^k-1)=(45ak)10^{k-1}+(1+\cdots+(a-1))10^k=(45ak)10^{k-1}+\frac{a(a-1)}{2}10^k\ .$$
Finally consider $n=a10^k+b$ with $1\le a\le 9$ and $0\le b<10^k$.  The sum of the digits includes all the above; and all the numbers from $0$ to $b$, with an initial digit $a$ prepended.  Thus we obtain a recursive formula which may perhaps in practice be the easiest way to calculate $D(n)$: if $1\le a\le 9$ and $0\le b<10^k$, then
$$D(a10^k+b)=(45ak)10^{k-1}+\frac{a(a-1)}{2}10^k+a(b+1)+D(b)\ .$$
A: There is no rational function (formula involving only $+$, $-$, $\cdot$ and $/$) that does this. Indeed, for $n = 100...00$, we have $f(n+1)=f(n)+2$. Therefore the function $f(x+1)-f(x)$, which is also rational, takes on the value $2$ an infinite number of times, but a rational function cannot take on the same value an infinite number of times unless it's constant, and $f(x+1)-f(x)$ is clearly not constant.
A: Let $n=\sum_{i=0}^k10^i\cdot n_i$, so the $n_i$ are the decimal digits of $n$. Let $[x]$ denote the integer part of $x$. Then the sum $S$ of all digits of all integers up to $n$ is
$$S=\sum_{i=0}^kn_i\cdot\left(n+1-10^i\cdot\left[\frac{n}{10^i}\right]\right)+\frac{1}{2}\cdot\sum_{i=0}^k10^in_i(n_i-1)+45\cdot\sum_{i=0}^k\left[\frac{n}{10^{i+1}}\right].$$
As I feared, it's very ugly. From the edit in your question (stating $n<10^9$) I would guess it's a programming question. Programming a loop is much nicer for such small numbers.
A: I actually managed to find, through several calculations, a general recursive formula for the sum of the digits fom $0$ to $n$, which I denoted by $Dg(n)$.
However, $n$ has to be written using the positional notation, namely $10^z\alpha+10^{z-1}\beta+10^{z-2}\gamma+...+\omega$, where $\alpha,\beta,\gamma,...,\omega$ are the digits of $n$ in order from left to right, and $z+1$ corresponds to the number of digits of n.
That being said, the formula is
$$
Dg(10^z\alpha+10^{z-1}\beta+10^{z-2}\gamma+\cdots+\omega)=
$$
$$
=\alpha(45\times z\times 10^{z-1}+1+10^{z-1}\beta+10^{z-2}\gamma+\cdots+\omega)++Dg(10^{z-1}\beta+10^{z-2}\gamma+\cdots+\omega)+10^z\times T_{\alpha-1}
$$
where $T_n$ is the nth triangular number.
The calculation ends when the recursion reaches $Dg(\omega)$, which is equal to $\frac{\omega(\omega+1)}{2}$.
A: The sum of consecutive natural numbers can be calculated with this formula:
n/2[2a + (n-1) x d] where:
n = total number of consecutive natural numbers to be summed
d = difference between two consecutive natural numbers (generally 1)
a = first natural number
Thus, sum of digits 1 to 100 is:
100/2(2+99x1) = 50 x 101 = 5050
