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.
 A: 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.
A: 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.
A: 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$.
A: 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$.
