Divisibility Rules
Is 7 452 divisible by 3? You could do the long division, but there is a shortcut: add the digits (), and since 18 is divisible by 3, so is 7 452. Divisibility rules like this let you test for small factors by looking at the digits, and the reason they work is a neat piece of place-value thinking that leads straight to the check digits on barcodes and credit cards.
The rules
" is divisible by " means is a factor of : leaves remainder 0.
| Divisible by | Test | Example |
|---|---|---|
| 2 | last digit is even (0, 2, 4, 6, 8) | 3 586 ✓ |
| 3 | digit sum is divisible by 3 | 7 452: ✓ |
| 4 | last two digits form a number divisible by 4 | 9 316: 16 ✓ |
| 5 | last digit is 0 or 5 | 2 745 ✓ |
| 6 | divisible by 2 and by 3 | 7 452: even, digit sum 18 ✓ |
| 8 | last three digits form a number divisible by 8 | 5 624: ✓ |
| 9 | digit sum is divisible by 9 | 7 452: 18 ✓ |
| 10 | last digit is 0 | 6 310 ✓ |
| 11 | alternating sum of digits ( from the right) is divisible by 11 | 9 482: ✓ |
For 7, there is no simple digit test worth memorising; just divide.
Why the "last digits" rules work
Any whole number can be split into "the last digit" plus "a multiple of 10". For 3 586: . Since 10 is divisible by 2 and by 5, the part 3 580 is too, so whether 3 586 is divisible by 2 or 5 depends only on the last digit.
The same reasoning works with (so only the last two digits matter for 4 and 25) and (last three digits for 8).
Why the digit-sum rules work
Look at what each place is worth compared with a multiple of 9:
Every power of ten is "a multiple of 9, plus 1". So:
The first bracket is always divisible by 9 (and therefore by 3). So the number and its digit sum leave the same remainder when divided by 9 or by 3. The rule for 11 uses the same trick with , which makes the place values alternate between and .
Let's check that claim about remainders in code:
def digit_sum(n):
return sum(int(d) for d in str(n))
for n in [7452, 1234, 98765, 500]:
print(f"{n}: digit sum {digit_sum(n)}, "
f"n % 9 = {n % 9}, digit sum % 9 = {digit_sum(n) % 9}, n % 3 = {n % 3}")7452: digit sum 18, n % 9 = 0, digit sum % 9 = 0, n % 3 = 0
1234: digit sum 10, n % 9 = 1, digit sum % 9 = 1, n % 3 = 1
98765: digit sum 35, n % 9 = 8, digit sum % 9 = 8, n % 3 = 2
500: digit sum 5, n % 9 = 5, digit sum % 9 = 5, n % 3 = 2
The two middle columns always agree.
Combining rules
A number is divisible by 6 when it passes the tests for 2 and 3, by 12 when it passes 3 and 4, by 15 when it passes 3 and 5. This works because the two tests check coprime factors (factors that share no common factor except 1). It would be wrong to say "divisible by 2 and 4 means divisible by 8": 12 passes both tests but is not whole.
Here are the rules written as code and checked against plain % on many numbers:
def digits(n):
return [int(c) for c in str(n)]
rules = {
2: lambda n: digits(n)[-1] in (0, 2, 4, 6, 8),
3: lambda n: sum(digits(n)) % 3 == 0,
4: lambda n: n % 100 % 4 == 0,
5: lambda n: digits(n)[-1] in (0, 5),
6: lambda n: n % 2 == 0 and sum(digits(n)) % 3 == 0,
9: lambda n: sum(digits(n)) % 9 == 0,
11: lambda n: sum(d if i % 2 == 0 else -d for i, d in enumerate(reversed(digits(n)))) % 11 == 0,
}
for b, rule in rules.items():
agree = all(rule(n) == (n % b == 0) for n in range(1, 20001))
print(f"rule for {b:2d} agrees with % on 1..20000: {agree}")rule for 2 agrees with % on 1..20000: True
rule for 3 agrees with % on 1..20000: True
rule for 4 agrees with % on 1..20000: True
rule for 5 agrees with % on 1..20000: True
rule for 6 agrees with % on 1..20000: True
rule for 9 agrees with % on 1..20000: True
rule for 11 agrees with % on 1..20000: True
Casting out nines
Because a number and its digit sum have the same remainder mod 9, you can check a multiplication by hand: reduce each factor to its digit-sum remainder, multiply those, and compare with the answer's remainder. ? ; ; . And . They match, so the answer is probably right. (A mismatch proves an error; a match can't catch every mistake, for example swapped digits.)
In software
- Check digits are divisibility rules in disguise. An ISBN-10 is valid when a weighted digit sum is divisible by 11; credit card numbers use the Luhn algorithm, which checks divisibility by 10. A single mistyped digit breaks the rule, so typos are caught before a database lookup.
- Leap years are a divisibility rule: divisible by 4, except centuries, unless divisible by 400. In code:
y % 4 == 0 and (y % 100 != 0 or y % 400 == 0). - Bit tricks: in binary, "divisible by 2" is "last bit is 0" and "divisible by 8" is "last three bits are 0", the same last-digits rule in base 2. Code writes
n & 7 == 0for speed (see Binary and Hexadecimal).
Exercises
Exercise 1: Apply the rules
Which of 2, 3, 4, 5, 6, 9, 10 divide 23 760?
Answer
All of them: 2, 3, 4, 5, 6, 9 and 10.
Solution
last digit 0 → divisible by 2, 5 and 10
last two digits 60 → 60 = 4 × 15, divisible by 4
digit sum 2+3+7+6+0 = 18 → divisible by 3 and 9
even and divisible by 3 → divisible by 6print([b for b in [2, 3, 4, 5, 6, 9, 10] if 23760 % b == 0])[2, 3, 4, 5, 6, 9, 10]
Exercise 2: Missing digit
The four-digit number 5 □ 2 8 is divisible by 9. What is the missing digit?
Answer
3
Solution
The digit sum is . The next multiple of 9 after 15 is 18, so ( is not a digit). Check: .
Exercise 3: Divisible by 11?
Use the alternating-sum rule to decide whether 72 919 and 31 417 are divisible by 11.
Answer
72 919 is (alternating sum 22); 31 417 is not (alternating sum 12).
Solution
Start from the last digit with +, then alternate signs:
12 leaves remainder 1 when divided by 11, and so does 31 417:
print(72919 % 11, 72919 // 11)
print(31417 % 11)0 6629
1
Exercise 4: Leap years
Write the leap-year rule as a function and list the leap years among 1896, 1900, 1904, 2000, 2023, 2024, 2100.
Answer
1896, 1904, 2000, 2024
Solution
1900 and 2100 are centuries not divisible by 400, so they are not leap years; 2000 is divisible by 400, so it is.
def is_leap(y):
return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
print([y for y in [1896, 1900, 1904, 2000, 2023, 2024, 2100] if is_leap(y)])[1896, 1904, 2000, 2024]
Test yourself
Is 8 136 divisible by 3?
- Yes, because the digit sum 18 is divisible by 3
- No, because it ends in 6
- Yes, because it is even
- No, because 8 is not divisible by 3
Which number is divisible by 4?
- 3 418
- 5 726
- 7 332
- 9 150
A number passes the tests for 2 and for 4. Must it be divisible by 8?
- Yes, always
- No: for example 12
- Only if it is odd
- Yes, because