Sub topic 12
How Many Digits Are Used to Write 1 to n
This chapter assumes you can count how many integers lie in a range, form and solve a linear equation, and use powers of 10. It also assumes you have read the earlier chapter on place value. The question type here is common in CAT and similar exams: "A book has pages numbered 1 to n. How many digits are used?" Or the reverse: "Numbering the pages used 1392 digits. How many pages are there?"
The core idea: group by number of digits
Numbers with the same length contribute the same number of digits each, so count each length group separately.
| Group | Range | How many numbers | Digits each | Digits in group | Running total |
|---|---|---|---|---|---|
| 1-digit | 1 to 9 | 9 | 1 | 9 | 9 |
| 2-digit | 10 to 99 | 90 | 2 | 180 | 189 |
| 3-digit | 100 to 999 | 900 | 3 | 2700 | 2889 |
| 4-digit | 1000 to 9999 | 9000 | 4 | 36000 | 38889 |
The group sizes follow the pattern 9 × 10^(k−1) numbers of k digits each. The running totals 9, 189, 2889 and 38889 are worth memorising. They are the breakpoints you compare against in almost every problem of this type.
The last row of the table is the pattern for all larger groups. Take the largest number of k digits, 99...9. Every number from 1 to it has been written, so the running total is the sum of the group digits so far.
Forward problem: digits used for 1 to n
Let n have d digits. All groups shorter than d are complete. The d-digit group is partial, running from 10^(d−1) to n.
Total = (digits in all complete shorter groups) + d × (n − 10^(d−1) + 1)
The last bracket is the count of numbers from 10^(d−1) to n, inclusive.
Example. Pages 1 to 250.
- Complete groups: 9 + 180 = 189.
- Three-digit numbers from 100 to 250: 250 − 100 + 1 = 151 numbers.
- Digits there: 3 × 151 = 453.
- Total: 189 + 453 = 642.
A one-line shortcut
Think of the digits level by level. Every number from 1 to n has at least one digit. Every number from 10 to n has a second digit. Every number from 100 to n has a third, and so on. The count at level k is n − 10^(k−1) + 1. Summing over k = 1 to d gives:
Total = d × (n + 1) − (11...1, with d ones)
For three-digit n this is 3(n + 1) − 111. For n = 250: 3 × 251 − 111 = 753 − 111 = 642, which matches. For two-digit n it is 2(n + 1) − 11, and for four-digit n it is 4(n + 1) − 1111.
The shortcut is faster for a single answer. The group method is easier to adapt when the question changes shape, such as the reverse problem below.
Reverse problem: digits used to pages
Now you are given the total T and must find n. Work out which group the total ends in by subtracting complete groups.
Example. Numbering a book's pages used 1392 digits. How many pages?
- 1392 is more than 189 and less than 2889, so the last page has 3 digits.
- Remove the first two groups: 1392 − 189 = 1203 digits are spent on three-digit pages.
- Each such page takes 3 digits: 1203 ÷ 3 = 401 pages.
- The three-digit pages start at 100, so the last page is 99 + 401 = 500.
Check with the forward shortcut: 3 × 501 − 111 = 1503 − 111 = 1392. ✓
When the division is not exact
If the leftover digits are not divisible by d, no complete numbering produces that total. For example, 1000 digits: 1000 − 189 = 811, and 811 ÷ 3 = 270 remainder 1. A remainder means the problem cannot be a full page run. In a well-posed exam question this will not happen. If it does, recheck your breakpoint subtraction first.
Checking with code
Brute force confirms both methods. The functions below follow the formulas in this chapter.
def digits_used(n):
"""Digits needed to write 1..n using d*(n+1) - 11...1."""
d = len(str(n))
return d * (n + 1) - (10**d - 1) // 9
def last_number(total):
"""Largest n such that writing 1..n uses exactly `total` digits.
Returns None if no such n exists."""
k, count, passed = 1, 9, 0
while total > k * count:
total -= k * count
passed += count
k += 1
count *= 10
q, r = divmod(total, k)
return passed + q if r == 0 else None
# Compare with brute force for every n up to 5000.
for n in range(1, 5001):
assert digits_used(n) == sum(len(str(i)) for i in range(1, n + 1))
print(digits_used(250)) # 642
print(last_number(1392)) # 500
print(last_number(189)) # 99
print(last_number(1000)) # NoneIn last_number, the loop peels off one complete group at a time. count is the size of the current group, and passed counts the numbers already written. The loop ends when the remaining digits fit inside the current group.
Variations you will meet
Ranges that do not start at 1
For pages a to b, subtract: digits(1 to b) − digits(1 to a−1).
Example. Pages 50 to 300.
- digits(1 to 300) = 3 × 301 − 111 = 792
- digits(1 to 49) = 2 × 50 − 11 = 89
- Answer: 792 − 89 = 703
Check by groups: pages 50 to 99 are 50 two-digit numbers, giving 100 digits. Pages 100 to 300 are 201 three-digit numbers, giving 603 digits. The total is 703. ✓
Finding the m-th digit in a long string
Suppose you write 1, 2, 3, ... in a row as 123456789101112... and ask for the 200th digit.
- 1 to 99 uses 189 digits, so the 200th digit lies in the three-digit group.
- 200 − 189 = 11, so we need the 11th digit among 100101102103...
- 11 = 3 × 3 + 2, so three full numbers (100, 101, 102) are used, and we want the 2nd digit of the next number, 103.
- The 2nd digit of 103 is 0.
The general rule is to divide the leftover by d. The quotient is the number of full numbers skipped. The remainder, if nonzero, is the position inside the next number. If the remainder is 0, the answer is the last digit of the last full number.
Other edge cases
- Starting at 0. If the sequence starts at 0, add one digit, because 0 is written with one digit.
- Leading zeros. Do not count them unless the question says numbers are padded, for example "001, 002".
- Subsets of numbers. For only even numbers or only multiples of 5, the groups still work, but each group's count of numbers changes. For even numbers from 10 to 98 there are 45 numbers, not 90.
- Cost problems. A shop charges per digit to print house numbers. The digit total is the first step, and you multiply by the price afterwards.
Speed and accuracy
- Decide the digit length of the answer before computing anything. A total below 189 means two-digit pages, and a total between 189 and 2889 means three-digit pages.
- Use the shortcut d(n+1) − 11...1 when n is given. Use group subtraction when the total is given.
- Do a sanity check with size. For three-digit n the total must be between 189 and 2889, and roughly 3n minus about 111.
- Check the reverse answer by plugging it into the forward formula, as in the 1392 example.
Exercise
How many digits are used to write the page numbers 1 to 450?
Show answer
Complete groups give 9 + 180 = 189 digits. The three-digit pages run from 100 to 450, which is 351 numbers, using 3 × 351 = 1053 digits. Total: 189 + 1053 = 1242. Check with the shortcut: 3 × 451 − 111 = 1353 − 111 = 1242.
Exercise
Numbering a book's pages from 1 used 2893 digits in total. How many pages does the book have?
Show answer
2893 is just above 2889, so the last page has 4 digits. After 999 pages, 2893 − 2889 = 4 digits remain, which is 4 ÷ 4 = 1 four-digit page. The last page is 999 + 1 = 1000. Check: 4 × 1001 − 1111 = 4004 − 1111 = 2893.
Exercise
The numbers 1, 2, 3, ... are written in a row as 123456789101112... What is the 1000th digit?
Show answer
1 to 99 uses 189 digits. The remaining 1000 − 189 = 811 digits fall among three-digit numbers. Since 811 = 3 × 270 + 1, the numbers 100 to 369 (270 numbers) use 810 digits, so 999 digits are used up to 369. The 1000th digit is the first digit of 370, which is 3.