Sub topic 10
Digit Sums and Remainders Modulo 9 and 3
This chapter assumes you can do long division with remainders, write a number as digits times powers of 10, and solve a simple linear equation. A remainder "modulo 9" (written mod 9) is the remainder left after dividing by 9.
The core fact: 10 leaves remainder 1
A decimal number is a sum of digits times powers of 10. Take a number with digits d₃ d₂ d₁ d₀:
n = 1000·d₃ + 100·d₂ + 10·d₁ + d₀
Now look at the powers of 10 modulo 9:
- 10 = 9 + 1, so 10 leaves remainder 1.
- 100 = 99 + 1, so 100 leaves remainder 1.
- 1000 = 999 + 1, so 1000 leaves remainder 1.
Every power of 10 is a string of 9s plus 1. Each place value therefore contributes its digit times 1, plus a multiple of 9. Writing this out:
n = (999·d₃ + 99·d₂ + 9·d₁) + (d₃ + d₂ + d₁ + d₀)
The first bracket is a multiple of 9. So n and its digit sum leave the same remainder when divided by 9. The same argument works for 3, because 3 divides 9 and every multiple of 9 is also a multiple of 3.
n = 7384
digit_sum = sum(int(d) for d in str(n))
print(digit_sum, n % 9, digit_sum % 9) # 22 4 4The digit sum is 22, and 22 leaves remainder 4 when divided by 9, which matches 7384 mod 9.
Repeating the digit sum: the digital root
If the digit sum is still large, add its digits again. Repeating until one digit is left gives the digital root. For 7384: 7+3+8+4 = 22, then 2+2 = 4. The digital root is 4, and it equals the remainder mod 9, except that a remainder of 0 is written as 9 (for positive numbers).
def digital_root(n):
return 0 if n == 0 else 1 + (n - 1) % 9
print(digital_root(7384)) # 4
print(digital_root(729)) # 9, because 729 is a multiple of 9In practice you rarely need the digital root as a number. You usually want "remainder mod 9", so reduce the digit sum and treat 9 as 0.
Working with sums, differences and products
Remainders mod 9 behave well under arithmetic. If a leaves remainder r and b leaves remainder s:
- a + b leaves the remainder of r + s.
- a − b leaves the remainder of r − s (add 9 if this is negative).
- a × b leaves the remainder of r × s.
Combined with the digit-sum rule, you can find the remainder of a big product without computing it.
Example. Find the remainder when 123456 × 789 is divided by 9.
- 123456 has digit sum 21, which leaves remainder 3.
- 789 has digit sum 24, which leaves remainder 6.
- The product leaves the remainder of 3 × 6 = 18, which is 0.
The actual product is 97,406,784, with digit sum 45, a multiple of 9. The method agrees.
Casting out nines as a check
You can use the same rule to check a hand calculation. Reduce each input mod 9, do the operation on the remainders, and compare with the remainder of your answer.
Example. Someone claims 3764 × 825 = 3,105,200.
- 3764 → digit sum 20 → remainder 2.
- 825 → digit sum 15 → remainder 6.
- Expected remainder of the product: 2 × 6 = 12 → 3.
- Claimed answer 3,105,200 → digit sum 11 → remainder 2.
The remainders differ, so the claim is wrong. The correct product is 3,105,300, with digit sum 12 and remainder 3.
The check has limits. It can only prove a result wrong, never right. These errors slip through:
- Swapped digits, such as 3,150,300 for 3,105,300, because the digit sum does not change.
- Errors that are multiples of 9, such as a digit that is 9 too large or too small in a different place.
- A missing or extra zero, which does not change the digit sum.
For these, add a size estimate (see below).
Digit sums of numbers written side by side
CAT questions often glue numbers together, such as 1234567891011…. Gluing changes the number, but the digit sum is just the sum of all the digits written, which is the sum of the digit sums of the pieces.
Example. Find the remainder mod 9 of the number formed by writing 1, 2, 3, …, 20 side by side.
- Digits of 1 to 9 add up to 45.
- For 10 to 19, the tens digits add up to 10 and the units digits add up to 45, giving 55.
- The number 20 adds 2.
- The total digit sum is 45 + 55 + 2 = 102. Since 102 = 99 + 3, the remainder is 3.
There is a shortcut. The glued number has the same remainder as 1 + 2 + … + 20 = 210, because each piece has the same remainder as its own digit sum. And 210 = 207 + 3, so the remainder is 3 again.
big = int("".join(str(i) for i in range(1, 21)))
print(big % 9) # 3Reversals and the multiple of 9
Take a two-digit number with digits a and b, which is 10a + b. Its reversal is 10b + a. Their difference is:
(10a + b) − (10b + a) = 9a − 9b = 9(a − b)
So the difference is always a multiple of 9, and the quotient is the difference of the digits. The same holds for any number and any rearrangement of its digits, because both have the same digit sum and therefore the same remainder mod 9. Their difference leaves remainder 0.
print(all(((10 * a + b) - (10 * b + a)) % 9 == 0
for a in range(1, 10) for b in range(10))) # TrueThis turns questions of the form "a two-digit number exceeds its reversal by 27" into the simple equation 9(a − b) = 27, so a − b = 3.
Finding a missing digit
When one digit is unknown, set the digit sum equal to a multiple of 9 (or 3) and read off the digit.
Example. Which digit d makes 48d73 divisible by 9?
The digit sum is 4 + 8 + d + 7 + 3 = 22 + d. The next multiple of 9 above 22 is 27, so d = 5. The only other candidate, 18, is below 22, and 36 would need d = 14. Check: 48573 ÷ 9 = 5397.
For divisibility by 3, more than one digit can work. With 48d73, the digit sum 22 + d must be a multiple of 3, so d ∈ {2, 5, 8}.
Choosing the method
| Question type | Tool |
|---|---|
| Remainder of n mod 9 or mod 3 | Digit sum of n, reduced |
| Remainder of a product or sum | Reduce each factor, then combine |
| Is a large result plausible? | Cast out nines, then estimate the size |
| Number minus a rearrangement | Always a multiple of 9 |
| Unknown digit and divisibility by 9 or 3 | Digit sum equals a multiple of 9 or 3 |
Other bases
The proof used only "10 is 1 more than 9". In base b, the number b is 1 more than b − 1, so the digit sum of a base-b numeral leaves the same remainder as the number modulo b − 1.
Example. 213 in base 5 equals 2·25 + 1·5 + 3 = 58. The digit sum is 6, which leaves remainder 2 mod 4. And 58 = 56 + 2, which also leaves remainder 2 mod 4.
In base 8, the digit sum gives the remainder mod 7. In base 2, it gives the remainder mod 1, which is useless. Binary digit sums are used for other things, such as parity of the number of 1s.
Common errors
- Applying the rule to other divisors. Digit sums say nothing direct about divisibility by 6, 7 or 11. For 6, test both 2 (last digit even) and 3 (digit sum). For 11, use the alternating digit sum, which is the next chapter's topic.
- Writing remainder 9. A remainder is always less than the divisor. A digit sum of 18 or 27 means remainder 0.
- Negative remainders. If r − s is negative, add 9 before reading the answer. For example, 2 − 6 = −4 becomes 5.
- Treating the digit sum of a product as the product of digit sums. It is true only after reducing mod 9. The digit sum of 12 × 12 = 144 is 9, while 3 × 3 = 9 happens to agree, but 13 × 13 = 169 has digit sum 16, while 4 × 4 = 16 only agrees because both reduce to 7.
- Trusting a pass. A matching remainder means the answer might be right. Confirm with a size estimate such as 3764 × 825 ≈ 3800 × 800 = 3,040,000.
Exercise
Find the remainder when 4567 × 8912 is divided by 9, without multiplying the numbers.
Show answer
4567 has digit sum 22, which leaves remainder 4. 8912 has digit sum 20, which leaves remainder 2. The product leaves the remainder of 4 × 2 = 8. The actual product, 40,701,104, has digit sum 17, which leaves remainder 8.
Exercise
A two-digit number exceeds the number formed by reversing its digits by 27. How many such two-digit numbers are there?
Show answer
Write the number as 10a + b. The difference is 9(a − b) = 27, so a − b = 3. Digit a runs from 3 to 9 and b = a − 3 runs from 0 to 6, giving 30, 41, 52, 63, 74, 85 and 96. That is 7 numbers. The number 30 counts because 30 − 03 = 27.
Exercise
The number formed by writing the integers 1 to 12 side by side (123456789101112) is divided by 3 and by 9. Find both remainders.
Show answer
The digit sum is 45 (digits 1 to 9) + 1 + 0 + 1 + 1 + 1 + 2 + ... counted piece by piece: 10 gives 1, 11 gives 2, 12 gives 3, so the total is 45 + 1 + 2 + 3 = 51. Then 51 mod 9 = 6 and 51 mod 3 = 0. Check with 1 + 2 + ... + 12 = 78, and 78 mod 9 = 6. The remainders are 6 (mod 9) and 0 (mod 3).