Sub topic 13
How often a digit appears in a range
This chapter assumes you can write a number as digits times place values, and that you know how to count digits used in 1 to n (the previous chapter). Here the question changes. Instead of "how many digits are printed?", you are asked "how many times is the digit 7 printed?" or "how many numbers contain a 7?". CAT asks both, and they have different answers.
Appearances versus numbers
Two kinds of question look alike.
- Appearances: count every printed copy of the digit. The number 11 contains two 1s, and both count.
- Numbers containing the digit: count each number once, however many copies it has.
For 1 to 100, the digit 1 appears 21 times. The numbers from 1 to 100 that contain a 1 are only 20, because 11 is counted once. Read the question for the words "how many times" or "how many numbers" before you start.
Counting one place at a time
Each digit of a number sits in a place: units, tens, hundreds, and so on. The digit d can appear in each place independently, so you count place by place and add the results.
Take the numbers 00 to 99, with a leading zero written for single-digit numbers.
- The units digit takes each value 0 to 9 once in every block of ten, so every digit appears 10 times in the units place.
- The tens digit is the same: every digit appears 10 times.
So each of the digits 1 to 9 appears 20 times in 1 to 99. For 1 to 999, add the hundreds place and each digit 1 to 9 appears 3 × 100 = 300 times. The pattern for 1 to 10^k − 1 is:
| Range | Digits used per number | Times each digit 1 to 9 appears |
|---|---|---|
| 1 to 9 | 1 | 1 |
| 1 to 99 | up to 2 | 20 |
| 1 to 999 | up to 3 | 300 |
| 1 to 9999 | up to 4 | 4000 |
The general count is k × 10^(k−1) for a range 1 to 10^k − 1.
The place-by-place rule for any limit
For a limit that is not 99 or 999, split n around the place you are counting. For place value p (1, 10, 100, ...):
- high is the number formed by the digits to the left of that place.
- cur is the digit at that place.
- low is the number formed by the digits to the right of that place.
For a digit d from 1 to 9, the count at that place is:
- high × p, from the complete cycles (for example, 0 to 99 in the tens place gives ten full 0 to 9 cycles of the units, so p = 10 copies per cycle);
- plus p if cur > d (one more full block of p numbers has d in this place);
- plus low + 1 if cur = d (only the numbers up to n in the last block count);
- plus 0 if cur < d.
Worked example: the digit 3 in 1 to 350
Take each place in turn.
- Units (p = 1): high = 35, cur = 0, low = 0. The count is 35 × 1 = 35, since cur < 3. These are 3, 13, 23, ..., 343.
- Tens (p = 10): high = 3, cur = 5, low = 0. The count is 3 × 10 = 30, plus 10 because cur > 3, so 40. These are 30 to 39, 130 to 139, 230 to 239 and 330 to 339.
- Hundreds (p = 100): high = 0, cur = 3, low = 50. cur = 3, so the count is 50 + 1 = 51. These are 300 to 350.
The total is 35 + 40 + 51 = 126.
Worked example: pages 1 to 250 and the digit 2
- Units: high = 25, cur = 0, so 25.
- Tens: high = 2, cur = 5, so 20 + 10 = 30.
- Hundreds: high = 0, cur = 2, low = 50, so 51.
The total is 25 + 30 + 51 = 106.
The digit 0 needs separate handling
Zero is different because a leading zero is never printed. The number 7 has no tens digit, so you must not count a tens zero for it.
The simplest way for 1 to 999 is to subtract. The digits used in 1 to 999 total 9 + 180 + 2700 = 2889. The digits 1 to 9 each appear 300 times, which is 2700. So zeros appear 2889 − 2700 = 189 times.
You can also count them directly: units zeros are 10, 20, ..., 990 (99 numbers), and tens zeros come from 100 to 109, 200 to 209, ..., 900 to 909 (9 × 10 = 90). That gives 99 + 90 = 189.
For the place-by-place rule with d = 0, a place counts only if high ≥ 1, and the full-cycle part is (high − 1) × p instead of high × p. When cur is greater than 0, add p. When cur is 0, add low + 1.
Checking with code
This function applies the rule and compares it with brute-force counting. The brute-force check is the one you use to test the method, not to answer exam questions.
def count_digit(n, d):
"""Count how many times digit d is printed when writing 1..n."""
total = 0
p = 1
while p <= n:
high = n // (p * 10)
cur = (n // p) % 10
low = n % p
if d > 0:
total += high * p
if cur > d:
total += p
elif cur == d:
total += low + 1
elif high > 0:
total += (high - 1) * p
if cur > 0:
total += p
else:
total += low + 1
p *= 10
return total
def brute(n, d):
return sum(str(i).count(str(d)) for i in range(1, n + 1))
print(count_digit(100, 1)) # 21
print(count_digit(350, 3)) # 126
print(count_digit(250, 2)) # 106
print(count_digit(1000, 0)) # 192
for n in (99, 100, 250, 350, 999, 1000, 2345):
for d in range(10):
assert count_digit(n, d) == brute(n, d), (n, d)
print("all checks passed")Counting numbers that contain a digit
For "how many numbers contain a 5", count the complement: numbers that do not contain it. A three-digit string 000 to 999 with no 5 has 9 choices per place (0 to 9 without 5), so there are 9 × 9 × 9 = 729 strings. One of them is 000, which is not in the range, so 728 numbers from 1 to 999 contain no 5. The numbers that do contain a 5 are 999 − 728 = 271.
Compare with appearances: the digit 5 is printed 300 times in 1 to 999, but only 271 numbers contain it. The gap of 29 comes from numbers with more than one 5, like 55 and 155.
The complement method works only when the digit is allowed in every place. If the digit is 0, a number's leading places cannot be 0, so count by length: 9 one-digit numbers (none contain 0), 81 two-digit numbers without 0 (9 × 9) and 729 three-digit numbers without 0 (9 × 9 × 9).
Common errors
- Counting leading zeros. Writing 7 as 007 inflates the zero count. The place-by-place rule for d = 0 avoids this.
- Forgetting the endpoint. In 1 to 100, the number 100 contributes one 1 and two 0s. If the range ends at 10^k, treat it as a separate number.
- Using 20 for every case. The "20 per digit" fact holds only up to 99. For 1 to 200, the digit 1 appears 20 + 100 (hundreds place in 100 to 199) = 120 times, not 40.
- Mixing up appearances and numbers. Check the question wording first.
- Starting from the wrong value. If a range starts at 50 rather than 1, compute the count up to the end and subtract the count up to 49.
Choosing the fastest method
| Question type | Fastest method |
|---|---|
| Limit is 99, 999, 9999 | Use k × 10^(k−1) for digits 1 to 9 |
| Arbitrary limit n, digit 1 to 9 | Place-by-place rule with high, cur, low |
| Digit 0 | Subtract nonzero counts from total digits, or use the d = 0 rule |
| "How many numbers contain d" | Complement: count strings without d |
| Interval a to b | count(b) − count(a − 1) |
For a quick plausibility check, the digit d appears about once in every ten numbers per place. For 1 to 350 and three places, about 350 × 3 / 10 ≈ 105 appearances, close to the 126 found, which is higher mainly because the hundreds place is 3 for 51 of the numbers.
Exercise
How many times is the digit 5 printed when writing the numbers from 1 to 200?
Show answer
Units place: high = 20, cur = 0, so 20. Tens place: high = 2, cur = 0, so 2 × 10 = 20. Hundreds place: high = 0, cur = 2, which is less than 5, so 0. The total is 40. As a check, 1 to 99 gives 20 and 100 to 199 gives another 20, and 200 has no 5.
Exercise
How many zeros are printed when writing the numbers from 1 to 1000?
Show answer
The digits 1 to 9 each appear 300 times in 1 to 999, so the 2889 digits used in 1 to 999 include 2889 − 2700 = 189 zeros. Then 1000 adds three zeros, so the total is 192.
Exercise
How many of the numbers from 1 to 999 contain at least one digit 7, and how many times is the digit 7 printed in total?
Show answer
Numbers with no 7: 9 × 9 × 9 = 729 strings from 000 to 999, minus the string 000, gives 728. So 999 − 728 = 271 numbers contain a 7. The digit 7 is printed 3 × 100 = 300 times in total, which is larger because numbers like 77 and 177 contain more than one 7.