Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATH BASICS
You are counting every occurrence of the digit 1 in the positive integers from 1 through 13. This is different from counting how many integers contain at least one 1. The integers that contribute are 1, 10, 11, 12, and 13. The number 11 contributes twice because its tens digit and its ones digit are both 1.
| INTEGER | CONTRIBUTION | REASON |
|---|---|---|
| 1 | 1 | Its only digit is 1 |
| 2 through 9 | 0 | None of their digits is 1 |
| 10 | 1 | The tens digit is 1 |
| 11 | 2 | Both digit positions are 1 |
| 12 | 1 | The tens digit is 1 |
| 13 | 1 | The tens digit is 1 |
| Total | 6 | 1 + 1 + 2 + 1 + 1 = 6 |
So the final count is 6, not 5. The value 5 would count the integers that contain 1, treating 11 as one matching integer instead of counting its two matching digit positions.
How many occurrences of the digit 1 appear in the integers from 1 through 13?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
There are two sets of values to inspect: every positive integer from 1 through 13, and every digit inside the current integer. An outer loop visits the integers. For each one, an inner loop extracts its digits and adds one to a shared total whenever the extracted digit equals 1.
int total = 0;
for (int current = 1; current <= 13; current++) {
int temp = current;
while (temp > 0) {
int digit = temp % 10;
if (digit == 1) {
total++;
}
temp /= 10;
}
}For current = 11, the inner loop extracts 1, increments total, extracts the other 1, and increments total again. The per-integer contribution is therefore 2. Integers such as 2 through 9 still pass through the inner loop, but their extracted digits never match 1, so they add 0.
The inner loop must remove digits as it scans them, but that removal should happen to a temporary copy. When current is 11, set temp to 11. The first extraction gives 1, then digit removal changes temp to 1. The next extraction gives 1 again, then digit removal changes temp to 0, ending the scan.
current = 11, temp = 11
extract 1, total increases
current = 11, temp = 1
extract 1, total increases
current = 11, temp = 0
stop inner loopIf you divide current instead of temp, the outer loop variable no longer represents the integer being visited. After scanning 11, current becomes 0, so the outer traversal can skip integers or stop early. Resetting temp = current at the start of every outer-loop pass keeps each digit scan independent.
Repair this fragment so digit removal changes a temporary copy instead of the outer-loop variable.
for (int current = 1; current <= 13; current++) {
while (current > 0) {
int digit = current % 10;
if (digit == 1) total++;
current /= 10;
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The outer loop must use current <= 13 because 13 is part of the requested range. When current is 13, the inner loop extracts 3 and then 1, so this integer contributes 1. If you write current < 13, the loop stops after 12 and never scans 13.
| INTEGER GROUP | PER-INTEGER CONTRIBUTION | RUNNING TOTAL |
|---|---|---|
| 1 | 1 | 1 |
| 2 through 9 | 0 | 1 |
| 10 | 1 | 2 |
| 11 | 2 | 4 |
| 12 | 1 | 5 |
| 13 | 1 | 6 |
Changing the bound from current <= 13 to current < 13 changes the result from 6 to 5 because it removes the contribution from 13. With the inclusive bound, the outer loop inspects every integer from 1 through 13, and the inner loop inspects every digit of each integer. Each matching digit position adds exactly one to the running total.