# Calculating the number of times a digit is written when given two numbers

My homework asks me the following:

If a student writes the integers from 5 to 305 inclusive by hand, how many times will she write the digit 5?

I started out by writing every number that contains 5 and I got 31, but 31 is not among the answers possible:

5, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 115, 125, 135, 145, 155, 165, 175, 185, 195, 205, 215, 225, 235, 245, 255, 265, 275, 285, 295, 305

I counted 55, 155, and 255 as two each since there are two occurrences of the digit 5 in each. I can't figure out what I'm doing wrong.

In addition, suppose I were given the numbers 1 and 100,000 - writing them out isn't efficient, and I would assume there's a formula for this but I can't figure that out either.

-
You are not counting $50$, $51$, bunch of others. –  André Nicolas Jul 20 '13 at 6:08
What are 51, 52, and so on --- chopped liver? –  Gerry Myerson Jul 20 '13 at 6:08
Doh! How could I be so stupid! However, if I add 9*3 (9 other numbers with the digit 5, and three sets of those 9 digits), and then add 27 to 31, I get 58, which isn't an answer either. –  Dustin L. Jul 20 '13 at 6:12
Hint: $305 = 5 \times 61$, what happens is you $\mod 5$ those two numbers? What is left? –  Amzoti Jul 20 '13 at 6:27

Count 000 up to 299. Of the 300 unit digits, $\frac1{10}$ are 5. Of the 300 tens digits, $\frac1{10}$ are 5. None of the hundreds digits are 5. Adding the one in 305, I count 61=30+30+1 in the integers from 5 to 305.

-
I really like your answer because for the most part it's simple and makes sense. However, I'm having trouble understanding how you got 300 unit digits and 300 tens digits. Perhaps this is out of the scope of the original question; if so, I apologize. –  Dustin L. Jul 20 '13 at 6:26
@DustinL.: There are $300$ numbers from $000$ up to $299$. There are $300$ unit digits going $$0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,\dots$$ There are $300$ tens digits going $$0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,\dots$$ There are $300$ hundreds digits, but the first hundred are $0$, the second hundred are $1$ and the third hundred are $2$. Some of the $0$ won't be written when they are the leading digit, but since we are not counting $0$s, that doesn't matter. –  robjohn Jul 20 '13 at 6:34
I understand what you're saying as far as this particular problem, but I don't understand the concepts. Suppose I was asked to find the number of occurrences of the digit 7 inclusive of the numbers 1 and 1000. How would I go about solving that using your explanation? –  Dustin L. Jul 20 '13 at 6:51
There would be $1000$ unit, tens, and hundreds digits, $\frac1{10}$ of which are $7$s. That would make $300$ occurrences of a $7$ in the list. The same can be said for all non-zero digits except the $1$ which occurs once more in $1000$. –  robjohn Jul 20 '13 at 9:43
Thank you so much! –  Dustin L. Jul 20 '13 at 19:56

You missed 50, 51, ..., 54, 56, 57, ..., 59, 150, ..., 159, and 250, ..., 259, which is a total of 9 * 3 = 27 more than your count of 31, for a total of 58.

Also, I count 31 numbers ending in 5 in your list, so it appears that you are not, in fact, counting 55, 155, and 255 twice. Adding these last three 5s gives 61, which is the answer that I get with some quick python code.

Additionally, given an arbitrary range, you can start by building up from smaller ranges. For example, 5 will appear in the ones place of a range of length 10 such as 0-9 or 624-633 exactly once. Then, in a range of length 100, in addition to the 10 5s in the ones place from the 10 ranges of length 10 contained in that range of length 100, there will be 10 5s in the tens place, for a total of 20. To find this for a range that's more complicated, just separate it into several parts and remember to account for the extra 5s in the highest place.

-

Fives in terminal position (units digit): $10$ in each bunch of $100$, plus the silly extra one for $305$.

Fives in second position from the end (tens digit): $10$ in each bunch of $100$.

Total: $31+30$.

-
Could you please clarify this? I don't understand how you got 10 5s in the terminal position in each set of 100. I count 11, since 55 would count as two, is this correct? –  Dustin L. Jul 20 '13 at 6:20
IN terminal position, that means last digit $5$. Am organizing this way so as to give a smooth not error-prone analysis. The front $5$ in $55$ is counted later, along with the $9$ others from $50$ to $59$. –  André Nicolas Jul 20 '13 at 6:23

First convince yourself that twenty $5$s occur between $0$ and $99$.You get ten $5$s from the unit's place: $05, 15, 25, ... 95$ and you get another ten from the ten's place of $50, 51, 52, ... 59$.

It's thus easy to see that twenty $5$s occur between $5$ and $99$ inclusive (since $0, 1,2,3,4$ don't count). Again, there are twenty $5$s between $100$ and $199$ since the $1$ in the hundred's place don't count. And again there are twenty from $200$ to $299$. Finally, add the one last $5$ from $305$ to obtain a sum of $61$.

-