TExcelTAD
Home › Basic Math & Pre-Algebra › Divisibility Rules

Divisibility Rules

Is 7 452 divisible by 3? You could do the long division, but there is a shortcut: add the digits (7+4+5+2=187 + 4 + 5 + 2 = 18), 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

"aa is divisible by bb" means bb is a factor of aa: a÷ba \div b 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: 7+4+5+2=187+4+5+2 = 18 ✓
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: 624=8×78624 = 8 \times 78 ✓
9 digit sum is divisible by 9 7 452: 18 ✓
10 last digit is 0 6 310 ✓
11 alternating sum of digits (+−+−…+ - + - \ldots from the right) is divisible by 11 9 482: 2−8+4−9=−112 - 8 + 4 - 9 = -11 ✓

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: 3 586=3 580+63\,586 = 3\,580 + 6. 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 100=4×25100 = 4 \times 25 (so only the last two digits matter for 4 and 25) and 1000=8×1251000 = 8 \times 125 (last three digits for 8).

Why the digit-sum rules work

Look at what each place is worth compared with a multiple of 9:

10=9+1100=99+11000=999+1\begin{aligned} 10 &= 9 + 1 \\ 100 &= 99 + 1 \\ 1000 &= 999 + 1 \end{aligned}

Every power of ten is "a multiple of 9, plus 1". So:

7 452=7×1000+4×100+5×10+2=7×(999+1)+4×(99+1)+5×(9+1)+2=(7×999+4×99+5×9)+(7+4+5+2)=multiple of 9+digit sum\begin{aligned} 7\,452 &= 7 \times 1000 + 4 \times 100 + 5 \times 10 + 2 \\ &= 7 \times (999 + 1) + 4 \times (99 + 1) + 5 \times (9 + 1) + 2 \\ &= (7 \times 999 + 4 \times 99 + 5 \times 9) + (7 + 4 + 5 + 2) \\ &= \text{multiple of 9} + \text{digit sum} \end{aligned}

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 10=11−110 = 11 - 1, which makes the place values alternate between +1+1 and −1-1.

Let's check that claim about remainders in code:

python · try it yourself
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 12÷812 \div 8 is not whole.

Here are the rules written as code and checked against plain % on many numbers:

python · try it yourself
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. 347×26=9 022347 \times 26 = 9\,022? 347→3+4+7=14→5347 \to 3+4+7 = 14 \to 5; 26→826 \to 8; 5×8=40→45 \times 8 = 40 \to 4. And 9 022→9+0+2+2=13→49\,022 \to 9+0+2+2 = 13 \to 4. 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 == 0 for 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
text
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 6
python · try it yourself
print([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 5+□+2+8=15+□5 + \square + 2 + 8 = 15 + \square. The next multiple of 9 after 15 is 18, so □=3\square = 3 (□=12\square = 12 is not a digit). Check: 5 328=9×5925\,328 = 9 \times 592.

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:

72 919:9−1+9−2+7=22→ 22=2×11, divisible31 417:7−1+4−1+3=12→ not a multiple of 11, not divisible\begin{aligned} 72\,919&{:}\quad 9 - 1 + 9 - 2 + 7 = 22 && \to\ 22 = 2 \times 11,\ \text{divisible} \\ 31\,417&{:}\quad 7 - 1 + 4 - 1 + 3 = 12 && \to\ \text{not a multiple of 11, not divisible} \end{aligned}

12 leaves remainder 1 when divided by 11, and so does 31 417:

python · try it yourself
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.

python · try it yourself
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?

  1. Yes, because the digit sum 18 is divisible by 3
  2. No, because it ends in 6
  3. Yes, because it is even
  4. No, because 8 is not divisible by 3

Which number is divisible by 4?

  1. 3 418
  2. 5 726
  3. 7 332
  4. 9 150

A number passes the tests for 2 and for 4. Must it be divisible by 8?

  1. Yes, always
  2. No: for example 12
  3. Only if it is odd
  4. Yes, because 2×4=82 \times 4 = 8