# Sum of digits of numbers between 1 to n

• Nov 28th 2010, 06:42 PM
pranay
Sum of digits of numbers between 1 to n
Hi, is there any general formula for finding the sum of digits from 1 to n (where n can be upto 10^9) .
I know it's got to do with some multiple of 45 (sum of digits from 1 to 9) but can't relate that to the required forumula.
Thanks .
• Nov 28th 2010, 06:44 PM
Prove It
The sum of the numbers from $\displaystyle \displaystyle 1$ to $\displaystyle \displaystyle n$ is $\displaystyle \displaystyle \frac{n}{2}(1+n)$.
• Nov 28th 2010, 06:47 PM
pranay
Thanks but i was interested in sum of digits rather than the sum of numbers
• Nov 29th 2010, 03:31 AM
Opalg
Quote:

Originally Posted by pranay
Hi, is there any general formula for finding the sum of digits from 1 to n (where n can be upto 10^9) .
I know it's got to do with some multiple of 45 (sum of digits from 1 to 9) but can't relate that to the required formula.

I don't think you are going to find a straightforward formula for this, except for some special cases of n.

For example, you can find the sum of the digits of all the numbers containing k digits, as follows. There are $\displaystyle 9*10^{k-1}$ such numbers (9 possibilities for the first digit, 10 possibilities for each of the remaining k–1 digits). Each of these numbers has k digits, so there are $\displaystyle 9*k*10^{k-1}$ digits altogether. Of these, $\displaystyle (k-1)*9*10^{k-2}$ will be zeros (each number stands a 1-in-10 chance of having a 0 in each position except the first). That leaves $\displaystyle 9k*10^{k-1} - (k-1)*9*10^{k-2}$ other digits. Each of the nine other digits (1 to 9) is equally likely to occur, so each digit occurs $\displaystyle k*10^{k-1} - (k-1)*10^{k-2} = (9k+1)*10^{k-2}$ times. The sum of the numbers from 1 to 9 is 45, so the sum of all the digits in all the k-digit numbers is $\displaystyle 45*(9k+1)*10^{k-2}$.

If you sum that result for k going from 1 to m, you find that the sum of all the digits in all the numbers from 1 to $\displaystyle 10^m-1$ is $\displaystyle \boxed{45*m*10^{m-1}}$.

But if you want the result for a general value of n (not of the form $\displaystyle 10^m-1$ ) then you will have a lot more work to do.
• Nov 29th 2010, 04:36 AM
pranay
Thanks a lot for the great explanation , exaclty what i wanted . Thank you :)