Sub topic 13
How Often a Digit Appears in a Range
Assume you know place value, integer division with remainders, basic algebra, and divisibility. This chapter focuses on counting how many times a chosen digit appears in every number in a range.
Count one place at a time
To count a digit, separate the numbers by place value:
- Ones place
- Tens place
- Hundreds place
- Thousands place
For example, count the digit 3 from 1 to 500.
| Place | Numbers containing 3 there | Count |
|---|---|---|
| Ones | 3, 13, 23, ..., 493 | 50 |
| Tens | 30–39, 130–139, ..., 430–439 | 50 |
| Hundreds | 300–399 | 100 |
| Total | All three places | 200 |
Therefore, the digit 3 appears 200 times.
The reason each complete block of 100 numbers contributes 10 occurrences in a given lower place is that the chosen digit appears once in every group of ten.
The cycle method
For a place with value \(p\), the digit pattern repeats every \(10p\) numbers.
- At the ones place, \(p=1\), so the cycle length is 10.
- At the tens place, \(p=10\), so the cycle length is 100.
- At the hundreds place, \(p=100\), so the cycle length is 1,000.
For a nonzero digit, each complete cycle contributes \(p\) occurrences at that place.
For example, at the tens place, the digit 7 appears in:
- 70–79
- 170–179
- 270–279
- and so on
Each 100-number cycle contributes 10 occurrences.
For a range that does not start at 1, count from 1 to the upper endpoint and subtract the count from 1 to one less than the lower endpoint:
\[ \text{count}(L\text{ to }R)=F(R)-F(L-1) \]
Here, \(F(n)\) means the number of appearances from 1 through \(n\).
Zero is a special case
Leading zeroes are not written. For example, 7 is not written as 007. Therefore, zero cannot be counted in the same way as digits 1 through 9.
Consider the ones place from 1 to 500. The multiples of 10 give 50 zeroes there. At the tens place, zero appears in 100–109, 200–209, 300–309, 400–409, and only 500 in the final partial block. That gives 41 more zeroes.
Thus, zero appears \(50+41=91\) times from 1 to 500.
A common error is to count the missing leading zeroes. That overcounts zero, especially in short ranges.
Verify with a short program
A program can check a hand calculation. It is useful for verification, but the place-value method is usually faster in a CAT question.
def count_digit(left, right, digit):
target = str(digit)
return sum(str(number).count(target)
for number in range(left, right + 1))
print(count_digit(1, 100, 1)) # 21
print(count_digit(1, 500, 3)) # 200The first result is 21: the digit 1 appears 10 times in the ones place, 10 times in the tens place, and once in 100.
Choosing the fastest method
Use complete cycles when the upper limit is a power of 10 or close to one. For example, from 1 to 999, every nonzero digit appears 100 times in each place, so it appears \(3 \times 100=300\) times overall.
For an irregular endpoint, split the range into complete blocks and a remainder. Then check that the answer is close to the number of places multiplied by the size of the range.
Exercise
How many times does the digit 2 appear from 1 through 300?
Show answer
From 1 through 99, it appears 20 times. From 100 through 199, it appears 20 times in the tens and ones places. From 200 through 299, it appears 100 times in the hundreds place, 10 times in the tens place, and 10 times in the ones place. The total is 160.