Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
  1. What is the sum of the digits of all numbers from 1 to 1000000?
  2. In general, what is the sum of all digits between 1 and N?
  3. f(n) is a function counting all the ones that show up in 1, 2, 3, ..., n. so f(1)=1, f(10)=2, f(11)=4 etc. When is the first time f(n)=n.

So for the first question, I tried thinking about it such that between 000000 and 999999, each digit will appear the same number of times, so if I find out how many times one digit appears I can just apply that to the other 9 digits (then add 1 for the last number 1000000):

(the number of times 1 digit appears)*(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) = ...

1 Appears once between 1 and 9 1 appears 11 times between 1 and 99 1 appears 11 * 10 + 10 = 120 times between 1 and 999 ...I'm not sure how to find the pattern

But firstly I'm not so sure of my approach, secondly I'm not sure about how to find how many times one particular number appears, and third if this method worked it doesn't seem very good for solving the second part of the question.

Lastly, I had a similar question to the first 2 (question 3) so I just grouped it with those. I hope they are related, and if not I can make a seperate question for that one.


share|cite|improve this question
I suppose for question three you aren't looking for n=1? – Matthew Conroy Jun 30 '12 at 3:33
Write the numbers one under the other, lined up, say in order thogh that doesn't matter. Let us pad short numbers with lead $0$'s so everybody has $6$ digit. Include $000000$. This won't affect the sum. Then each digit appears in each column $10^5$ times. Thus sum is $(6)(10^5)(0+1+\cdots+9)$ – André Nicolas Jun 30 '12 at 3:44
@MatthewConroy yes sorry, n = 1 doesn't count. – Matt Jun 30 '12 at 3:46
Anyone else catch the pun with the title? "Some" sum problems. – Joe Jun 30 '12 at 4:04
Remember the padding. How many numbers, for example, have a $7$ in the second position from the right? So put a $7$ there. The first position can be filled in $10$ ways. for each of these, the third position can be filled in $10$ ways. Same for fourth, fifth, sixth, for a total of $10\times 10\times 10\times 10\times 10$. Or else put a $7$ there, the rest, when you remove the $7$, is any $5$-digit number (again, remember padding). There are $100000$ of these. – André Nicolas Jun 30 '12 at 4:26
up vote 4 down vote accepted

For question three, we can simply compute. Besides n=1, the next solution is 199981, as generated by this PARI/GP code:

Define $r(n)$ to be the number of ones in the digits of $n$:

r(n) = nn=n;cc=0;while(nn>0,if((nn%10)==1,cc=cc+1);nn=floor(nn/10));return(cc)

Then run a loop and output $i$ if the sum of $r(i)$ from 1 up to $i$ is equal to $i$:


The output is this:

1 199981

This shows $f(1)=1$ and $f(199981)=199981$ and there are no other solutions less than 199981.

Similarly for question one, we define $g(n)$ to be the sum of the digits of $n$:

g(n) = nn=n; cc=0; while(nn>0,cc=cc+(nn % 10);nn=floor(nn/10));return(cc)

then calculate:


which yields the sum 27000001.

share|cite|improve this answer
Thanks for the answer. However, I'm looking for some kind of method I can use to solve these problems by hand. I understand how I would approach this if I were to write code to find the answer. – Matt Jun 30 '12 at 4:11
You can use the method I illustrated above by hand. It'll just take a while. – Matthew Conroy Jun 30 '12 at 4:18

I'll take the first question, which is the simplest of the 3. It will make things easier if we start looking at the sum of the digits of all numbers between 0 and 999,999. Start by adding leading 0s to pad all numbers to 6 digits.

Consider the first digit. If the first digit is a 1, there are 10 choices for each of the remaining 5 digits. So there are $10^5$ numbers in that range that have 1 as a first digit. The same can also be said of any other number as the first digit. So if you sum the first digit of all those numbers together, you get $(1+2+3+4+5+6+7+8+9)\times10^5=4,500,000$. The same is true for the remaining digits. So the sum of all the digits of all numbers between 0 and 999,999 is $4,500,000\times 6=27,000,000$.

We counted 0, which we didn't really want, but the sum of the digits of 0 is 0, so that doesn't affect our sum. That leaves just 1,000,000 which has a digit sum of 1, making our final total 27,000,001.

share|cite|improve this answer

Let our sum be S: add them forwards and backwards: 1 + 2 + 3 + ... +n = S and n+(n-1)+(n-2)+ ... +1 = S add the rows and you get the sum of n terms each equal to (n+1). This is then equal to n(n+1) = 2 S Therefore S = n(n+1)/2

You can easily compute the result for any n.
For n = 5 we have 1+2+3+4+5 = (5*6)/2 = 15
For n = 1000000 the result is: 10^6(10^6+1)/2 = (5 x 10^5)(10^6+1) = 5x10^11 + 5x10^5 = 500,000,500,000

Thanks to first grader Carl Friedrich Gauss!!

share|cite|improve this answer
This gives the sum of the integers from $1$ to $1,000,000$, but the question asks for the sum of the digits of the integers from $1$ to $1,000,000$. – Michael Albanese Jul 25 '14 at 3:48

Formula for this question is, X=n(n+1)/2 X=1000000(1000000+1)/2 X=1000000(1000001)/2 X=1000000(500000.5) X=500,000,500,000

share|cite|improve this answer
What does this add to the conversation that the other answers don't? – Mike Pierce Oct 25 '15 at 6:04

protected by Daniel Fischer Mar 29 at 23:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.