MATH2400 2,415 words·13 min read

Divisibility Tests

How Divisibility Tests Work#

Recall from Base Number Systems that every positive integer NN has a unique base 10 digit string,

N=(dkdk−1⋯d1d0)10=dk×10k+dk−1×10k−1+⋯+d1×10+d0,N = (d_k d_{k-1} \cdots d_1 d_0)_{10} = d_k \times 10^k + d_{k-1} \times 10^{k-1} + \cdots + d_1 \times 10 + d_0,

where each digit di∈{0,1,…,9}d_i \in \{0,1,\dots,9\}. Also recall that congruences respect addition and multiplication; if we reduce everything mod nn, the digits stay exactly where they are and only the powers of 1010 change. This gives us the master congruence that every test in this lecture falls out of:

N≡dk(10k mod n)+⋯+d1(10 mod n)+d0(modn).\boxed{N \equiv d_k \left(10^k \bmod n\right) + \cdots + d_1\left(10 \bmod n \right) + d_0 \pmod n.}

Basically, to test divisibility by nn we never need to actually divide by nn; we only need to know how the powers of 1010 behave mod nn. There are three behaviours worth knowing:

  • 10i≡0(modn)10^i \equiv 0 \pmod n from some point onwards (this happens exactly when nn divides a power of 1010, i.e. n=2a5bn = 2^a 5^b); then only the last few digits matter.
  • 10≡1(modn)10 \equiv 1 \pmod n (this is n=3n = 3 or 99); then every power of 1010 is 11 and we just sum the digits.
  • 10≡−1(modn)10 \equiv -1 \pmod n (this is n=11n=11); then the powers alternate between 11 and −1-1 and we alternately add and subtract digits.

For everything else (like 77 and 1313), the powers of 1010 cycle through junk values, so we cheat: we multiply the number by a carefully chosen constant first so that a nice pattern appears. Divisibility tests like these matter because they replace division (which is expensive to do in your head) with addition, subtraction and truncation, and they let you sniff out prime factorisations quickly.

Divisibility by Powers of 2 and 5#

Note

Lemma (powers of 2)
An integer is divisible by 2k2^k if and only if the number formed by its last kk digits is divisible by 2k2^k.

Note

Lemma (powers of 5)
An integer is divisible by 5k5^k if and only if the number formed by its last kk digits is divisible by 5k5^k.

Basically, 22 and 55 are the prime factors of 1010, so high enough powers of 1010 absorb any power of 22 or 55; everything past the last kk digits is invisible mod 2k2^k and mod 5k5^k, so you can just throw it away.

Proof. Split NN into the part before and after its last kk digits;

N=(dj⋯dk)10×10k+(dk−1⋯d1d0)10=M×10k+L, where L is the number formed by the last k digits.\begin{align*} N &= (d_j \cdots d_k)_{10} \times 10^k + (d_{k-1} \cdots d_1 d_0)_{10} \\ &= M \times 10^k + L, \text{ where } L \text{ is the number formed by the last } k \text{ digits}. \end{align*}

Since 10k=2k5k10^k = 2^k 5^k, we have 2k∣10k2^k \mid 10^k, i.e. 10k≡0(mod2k)10^k \equiv 0 \pmod{2^k}. Reducing mod 2k2^k,

N≡M×0+L(mod2k), (as 10k≡0)≡L(mod2k).\begin{align*} N &\equiv M \times 0 + L \pmod{2^k}, \text{ (as } 10^k \equiv 0 \text{)} \\ &\equiv L \pmod{2^k}. \end{align*}

Therefore 2k∣N2^k \mid N if and only if 2k∣L2^k \mid L. The exact same argument with 5k∣10k5^k \mid 10^k proves the second lemma. ■\blacksquare

The small cases are the ones you use constantly:

  • 22: last digit is even,
  • 44: last two digits divisible by 44,
  • 88: last three digits divisible by 88,
  • 55: last digit is 00 or 55,
  • 1010: last digit is 00 (since 10=2×510 = 2 \times 5, this is really the 22 test and the 55 test at once).

The tests for 2, 5 and 10 are basically primary school stuff so I'm not gonna dwell on them.

Example. Which powers of 22 and 55 divide 123450123450?
Work through the last digits one layer at a time;

last digit: 0,so 2∣123450 and 5∣123450,last two digits: 50=4(12)+2,so 4∤123450,50=25(2),so 25∣123450,last three digits: 450=125(3)+75,so 125∤123450.\begin{align*} \text{last digit: } &0, &&\text{so } 2 \mid 123450 \text{ and } 5 \mid 123450, \\ \text{last two digits: } &50 = 4(12) + 2, &&\text{so } 4 \nmid 123450, \\ &50 = 25(2), &&\text{so } 25 \mid 123450, \\ \text{last three digits: } &450 = 125(3) + 75, &&\text{so } 125 \nmid 123450. \end{align*}

Notice how once 44 fails there is no point testing 88; if 8∣N8 \mid N then 4∣N4 \mid N, so the powers of 22 stop dead at the first failure. Therefore, the only powers dividing 123450123450 are 212^1 and 51,525^1, 5^2; in fact 123450=2×3×52×823123450 = 2 \times 3 \times 5^2 \times 823.

Example. Find the largest power of 22 dividing 2,345,678,0002{,}345{,}678{,}000.
The lemma works for any kk, not just the k≤3k \leq 3 in the standard table; we keep grabbing one more digit until the test fails.

last three digits: 000=0,so 8∣N (every number divides 0),last four digits: 8000=16(500),so 16∣N,last five digits: 78000=32(2437)+16,so 32∤N.\begin{align*} \text{last three digits: } 000 &= 0, &&\text{so } 8 \mid N \text{ (every number divides } 0\text{)}, \\ \text{last four digits: } 8000 &= 16(500), &&\text{so } 16 \mid N, \\ \text{last five digits: } 78000 &= 32(2437) + 16, &&\text{so } 32 \nmid N. \end{align*}

Therefore, the largest power of 22 dividing 2,345,678,0002{,}345{,}678{,}000 is 24=162^4 = 16. It is important to note that trailing zeros hand you factors for free: three trailing zeros means 103=235310^3 = 2^3 5^3 divides the number immediately, and you only have to do real work from the fourth digit onwards.

Divisibility by 3 and 9#

Note

Lemma (divisibility by 3)
An integer is divisible by 33 if and only if the sum of its digits is divisible by 33.

Note

Lemma (divisibility by 9)
An integer is divisible by 99 if and only if the sum of its digits is divisible by 99.

This is the famous digit-sum test, and the reason it works is one line of modular arithmetic: 10≡1(mod9)10 \equiv 1 \pmod 9, so every power of 1010 is congruent to 11, and each digit just contributes itself.

Proof. Let N=(dk⋯d1d0)10N = (d_k \cdots d_1 d_0)_{10}. Since 10≡1(mod9)10 \equiv 1 \pmod 9, we have 10i≡1i=1(mod9)10^i \equiv 1^i = 1 \pmod 9 for every ii (congruences respect multiplication, so we may raise both sides to the iith power). Then

N=dk×10k+⋯+d1×10+d0≡dk×1+⋯+d1×1+d0(mod9), (replacing each 10i by 1)≡dk+dk−1+⋯+d1+d0(mod9).\begin{align*} N &= d_k \times 10^k + \cdots + d_1 \times 10 + d_0 \\ &\equiv d_k \times 1 + \cdots + d_1 \times 1 + d_0 \pmod 9, \text{ (replacing each } 10^i \text{ by } 1\text{)} \\ &\equiv d_k + d_{k-1} + \cdots + d_1 + d_0 \pmod 9. \end{align*}

So NN is congruent to its own digit sum mod 99; in particular 9∣N9 \mid N if and only if 99 divides the digit sum. Since 10≡1(mod3)10 \equiv 1 \pmod 3 as well, the identical argument works mod 33. ■\blacksquare

Notice that the proof gives us something strictly stronger than the lemma;

N≡dk+dk−1+⋯+d1+d0(mod9)  and (mod3).\boxed{N \equiv d_k + d_{k-1} + \cdots + d_1 + d_0 \pmod 9 \ \text{ and } \pmod 3.}

The digit sum doesn't just tell you whether 9∣N9 \mid N; it tells you the actual remainder. You can also iterate; if the digit sum is still big, take the digit sum of the digit sum, since the congruence keeps holding.

Example. Which of 123450123450 and 1234567812345678 are divisible by 33 or by 99?
For 123450123450,

1+2+3+4+5+0=15=3(5),\begin{align*} 1+2+3+4+5+0 &= 15 \\ &= 3(5), \end{align*}

so 3∣1234503 \mid 123450, but 9∤159 \nmid 15 so 9∤1234509 \nmid 123450; in fact 123450≡15≡6(mod9)123450 \equiv 15 \equiv 6 \pmod 9.
For 1234567812345678,

1+2+3+4+5+6+7+8=36=9(4),\begin{align*} 1+2+3+4+5+6+7+8 &= 36 \\ &= 9(4), \end{align*}

so 9∣123456789 \mid 12345678, and hence automatically 3∣123456783 \mid 12345678 too. Therefore, 123450123450 is divisible by 33 only, while 1234567812345678 is divisible by both 33 and 99.

Casting Out Nines#

Because NN is congruent to its digit sum mod 99 (not just "divisible iff divisible"), we can use the digit-sum lemma to sanity-check arithmetic: any correct equation in Z\mathbb{Z} must still be a correct congruence in Z9\mathbb{Z}_9, and digit sums make working mod 99 nearly free. This trick is called casting out nines.

Example. A friend claims that 6437+2895=93426437 + 2895 = 9342. Check the claim by casting out nines.
Reduce everything mod 99 using digit sums;

6437≡6+4+3+7=20≡2(mod9),2895≡2+8+9+5=24≡6(mod9),6437+2895≡2+6=8(mod9),9342≡9+3+4+2=18≡0(mod9).\begin{align*} 6437 &\equiv 6+4+3+7 = 20 \equiv 2 \pmod 9, \\ 2895 &\equiv 2+8+9+5 = 24 \equiv 6 \pmod 9, \\ 6437 + 2895 &\equiv 2 + 6 = 8 \pmod 9, \\ 9342 &\equiv 9+3+4+2 = 18 \equiv 0 \pmod 9. \end{align*}

Since 8≢0(mod9)8 \not\equiv 0 \pmod 9, the claimed answer is wrong without us ever doing the addition. Therefore, the sum cannot be 93429342; the true answer is 93329332, which indeed satisfies 9+3+3+2=17≡8(mod9)9+3+3+2 = 17 \equiv 8 \pmod 9.

It is important to remember that casting out nines can only ever prove an answer wrong; a passed check does not prove the answer right, and it will never catch a mistake that swaps two digits, because swapping digits does not change the digit sum. For digit swaps you need the mod 1111 test below, where position actually matters.

Divisibility by 11#

Note

Lemma (divisibility by 11)
An integer is divisible by 1111 if and only if the result of alternately adding and subtracting its digits is divisible by 1111.

The mechanism is the mirror image of the digit-sum test: this time 10≡−1(mod11)10 \equiv -1 \pmod{11}, so the powers of 1010 flip sign at every position, i.e. 10i≡(−1)i(mod11)10^i \equiv (-1)^i \pmod{11}.

Proof. Let N=(dk⋯d1d0)10N = (d_k \cdots d_1 d_0)_{10}. Since 10≡−1(mod11)10 \equiv -1 \pmod{11}, we have 10i≡(−1)i(mod11)10^i \equiv (-1)^i \pmod{11}, so

N=dk×10k+⋯+d2×102+d1×10+d0≡dk(−1)k+⋯+d2−d1+d0(mod11)≡d0−d1+d2−d3+⋯(mod11).\begin{align*} N &= d_k \times 10^k + \cdots + d_2 \times 10^2 + d_1 \times 10 + d_0 \\ &\equiv d_k (-1)^k + \cdots + d_2 - d_1 + d_0 \pmod{11} \\ &\equiv d_0 - d_1 + d_2 - d_3 + \cdots \pmod{11}. \end{align*}

Hence 11∣N11 \mid N if and only if 1111 divides the alternating sum of the digits. ■\blacksquare

Basically, the even-position digits (units, hundreds, ...) count positively and the odd-position digits (tens, thousands, ...) count negatively. If you start the alternation from the leftmost digit instead you might get the negative of this sum, which makes no difference to divisibility, but if you want the actual remainder mod 11 you must start from the units digit with a plus sign.

Example. Which of 123450123450 and 12343211234321 are divisible by 1111?
For 123450123450, starting from the units digit,

0−5+4−3+2−1=−3,\begin{align*} 0 - 5 + 4 - 3 + 2 - 1 &= -3, \end{align*}

and 11∤−311 \nmid -3, so 11∤12345011 \nmid 123450 (in fact 123450≡−3≡8(mod11)123450 \equiv -3 \equiv 8 \pmod{11}).
For 12343211234321,

1−2+3−4+3−2+1=0,\begin{align*} 1 - 2 + 3 - 4 + 3 - 2 + 1 &= 0, \end{align*}

and every number divides 00, so 11∣123432111 \mid 1234321. Therefore, only 12343211234321 is divisible by 1111; unsurprising once you notice 1234321=111121234321 = 1111^2 and 11∣111111 \mid 1111.

Catching Digit Swaps#

Here is the follow-up to casting out nines: transposition errors are invisible mod 99 but visible mod 1111, since the alternating sum cares which position each digit sits in. Checking mod 99 and mod 1111 together is a genuinely strong error check.

Example. A friend claims that 8734×56=4891408734 \times 56 = 489140. Check the claim mod 99 and mod 1111.
First cast out nines;

8734≡8+7+3+4=22≡4(mod9),56≡5+6=11≡2(mod9),8734×56≡4×2=8(mod9),489140≡4+8+9+1+4+0=26≡8(mod9).\begin{align*} 8734 &\equiv 8+7+3+4 = 22 \equiv 4 \pmod 9, \\ 56 &\equiv 5+6 = 11 \equiv 2 \pmod 9, \\ 8734 \times 56 &\equiv 4 \times 2 = 8 \pmod 9, \\ 489140 &\equiv 4+8+9+1+4+0 = 26 \equiv 8 \pmod 9. \end{align*}

The mod 99 check passes, so casting out nines sees nothing wrong. Now check mod 1111 with alternating sums (units digit first);

8734≡4−3+7−8=0(mod11),8734×56≡0×56=0(mod11),489140≡0−4+1−9+8−4=−8≡3(mod11).\begin{align*} 8734 &\equiv 4 - 3 + 7 - 8 = 0 \pmod{11}, \\ 8734 \times 56 &\equiv 0 \times 56 = 0 \pmod{11}, \\ 489140 &\equiv 0 - 4 + 1 - 9 + 8 - 4 = -8 \equiv 3 \pmod{11}. \end{align*}

Since 3≢0(mod11)3 \not\equiv 0 \pmod{11}, the claim is false. Therefore, the product cannot be 489140489140; the true answer is 489104489104 (the last two digits were swapped), and you can check its alternating sum is 4−0+1−9+8−4=04-0+1-9+8-4 = 0 as required.

Divisibility by 7#

Note

Lemma (divisibility by 7)
An integer is divisible by 77 if and only if the result of removing its last digit and subtracting 22 times that digit is divisible by 77.

In symbols: write N=10a+bN = 10a + b where bb is the last digit and aa is the rest of the number; then 7∣N7 \mid N if and only if 7∣a−2b7 \mid a - 2b. You apply this repeatedly, shrinking the number by one digit each time, until it is small enough to eyeball.

Why does this work? The powers of 1010 mod 77 cycle through 3,2,6,4,5,13, 2, 6, 4, 5, 1 — no nice pattern — so no digit-sum-style test exists. The trick from the lectures: look for a small multiple of 77 that sits next to a multiple of 1010. We find 21=2×10+121 = 2 \times 10 + 1, so 20≡−1(mod7)20 \equiv -1 \pmod 7, and multiplying NN by 22 makes everything collapse.

Proof. Let N=10a+bN = 10a + b. Multiplying by 22,

2N=20a+2b≡(−1)a+2b(mod7), (as 20=21−1≡−1)≡−(a−2b)(mod7).\begin{align*} 2N &= 20a + 2b \\ &\equiv (-1)a + 2b \pmod 7, \text{ (as } 20 = 21 - 1 \equiv -1\text{)} \\ &\equiv -(a - 2b) \pmod 7. \end{align*}

Since gcd⁡(2,7)=1\gcd(2,7) = 1, multiplying by 22 does not create or destroy factors of 77; hence 7∣N  ⟺  7∣2N  ⟺  7∣a−2b7 \mid N \iff 7 \mid 2N \iff 7 \mid a - 2b. ■\blacksquare

Example. Which of 1234512345 and 9753197531 are divisible by 77?
For 1234512345, repeatedly remove the last digit and subtract twice it;

1234−2(5)=1224,122−2(4)=114,11−2(4)=3.\begin{align*} 1234 - 2(5) &= 1224, \\ 122 - 2(4) &= 114, \\ 11 - 2(4) &= 3. \end{align*}

Since 7∤37 \nmid 3, we conclude 7∤123457 \nmid 12345.
For 9753197531,

9753−2(1)=9751,975−2(1)=973,97−2(3)=91,9−2(1)=7.\begin{align*} 9753 - 2(1) &= 9751, \\ 975 - 2(1) &= 973, \\ 97 - 2(3) &= 91, \\ 9 - 2(1) &= 7. \end{align*}

Since 7∣77 \mid 7, we conclude 7∣975317 \mid 97531 (indeed 97531=7×1393397531 = 7 \times 13933). Therefore, only 9753197531 is divisible by 77. You could also have stopped at 9191 if you recognised 91=7×1391 = 7 \times 13; stop as soon as you can see the answer.

Unlike the digit-sum test, this test does not preserve remainders; it only preserves whether the remainder is zero. For example, take N=16N = 16: the test gives 1−2(6)=−11≡3(mod7)1 - 2(6) = -11 \equiv 3 \pmod 7, but 16≡2(mod7)16 \equiv 2 \pmod 7. The culprit is that we multiplied by 22 along the way, which scrambles every nonzero residue while fixing 00. So use this for yes/no questions only; if an exam question wants N mod 7N \bmod 7, do it directly.

Divisibility by 13#

Note

Lemma (divisibility by 13)
An integer is divisible by 1313 if and only if the result of removing its last digit and adding 44 times that digit is divisible by 1313.

Same idea as for 77: we hunt for a small multiple of 1313 adjacent to a multiple of 1010, and this time 39=4×10−139 = 4 \times 10 - 1 works, giving 40≡1(mod13)40 \equiv 1 \pmod{13}.

Proof. Let N=10a+bN = 10a + b. Multiplying by 44,

4N=40a+4b≡a+4b(mod13), (as 40=39+1≡1).\begin{align*} 4N &= 40a + 4b \\ &\equiv a + 4b \pmod{13}, \text{ (as } 40 = 39 + 1 \equiv 1\text{)}. \end{align*}

Since gcd⁡(4,13)=1\gcd(4,13) = 1, we get 13∣N  ⟺  13∣4N  ⟺  13∣a+4b13 \mid N \iff 13 \mid 4N \iff 13 \mid a + 4b. ■\blacksquare

Example. Which of 1234512345 and 1238912389 are divisible by 1313?
For 1234512345, repeatedly remove the last digit and add four times it;

1234+4(5)=1254,125+4(4)=141,14+4(1)=18.\begin{align*} 1234 + 4(5) &= 1254, \\ 125 + 4(4) &= 141, \\ 14 + 4(1) &= 18. \end{align*}

Since 13∤1813 \nmid 18, we conclude 13∤1234513 \nmid 12345.
For 1238912389,

1238+4(9)=1274,127+4(4)=143,14+4(3)=26.\begin{align*} 1238 + 4(9) &= 1274, \\ 127 + 4(4) &= 143, \\ 14 + 4(3) &= 26. \end{align*}

Since 26=13×226 = 13 \times 2, we conclude 13∣1238913 \mid 12389 (indeed 12389=13×95312389 = 13 \times 953). Therefore, only 1238912389 is divisible by 1313. The same remainder warning applies here: because we multiplied by 44, the final number 2626 tells you nothing about N mod 13N \bmod 13 beyond the fact that it is 00.

Tests for Composite Numbers#

What about 66, 1212, 1515, and friends? We do not need new tests; we split the modulus into pieces we already know how to test, provided the pieces share no common factor.

Note

Proposition (coprime factors)
Let m,nm, n be integers with gcd⁡(m,n)=1\gcd(m,n) = 1. Then for any integer NN,

mn∣N  ⟺  m∣N and n∣N.mn \mid N \iff m \mid N \text{ and } n \mid N.

Proof. (⇒\Rightarrow) If mn∣Nmn \mid N, then since m∣mnm \mid mn and n∣mnn \mid mn, transitivity of divisibility (Lemma 1.2 in Divisibility and Primes) gives m∣Nm \mid N and n∣Nn \mid N.
(⇐\Leftarrow) Suppose m∣Nm \mid N and n∣Nn \mid N, so N=mkN = mk for some k∈Zk \in \mathbb{Z}. Then n∣mkn \mid mk, and since gcd⁡(n,m)=1\gcd(n,m) = 1, nn has no factors in common with mm, so nn must divide kk; write k=nℓk = n\ell. Then N=mnℓN = mn\ell, so mn∣Nmn \mid N. ■\blacksquare

Basically, a number is divisible by 66 exactly when it passes the 22 test and the 33 test, and divisible by 1212 exactly when it passes the 44 test and the 33 test.

You must split into coprime pieces; being divisible by 44 and by 66 does not make a number divisible by 2424, because gcd⁡(4,6)=2≠1\gcd(4,6) = 2 \neq 1. For example, 612612 is divisible by 44 (last two digits 1212) and by 66 (even, digit sum 99), yet 612=24×25+12612 = 24 \times 25 + 12, so 24∤61224 \nmid 612. The correct split is 24=8×324 = 8 \times 3; the last three digits give 612=8×76+4612 = 8 \times 76 + 4, so the 88 test fails and 24∤61224 \nmid 612, as expected.

Example. Is 123450123450 divisible by 66? Is 1234567812345678 divisible by 1212?
For 6=2×36 = 2 \times 3 (coprime pieces): 123450123450 ends in 00, so 2∣1234502 \mid 123450; its digit sum is 1515, so 3∣1234503 \mid 123450. Both tests pass, therefore 6∣1234506 \mid 123450.
For 12=4×312 = 4 \times 3 (coprime pieces): the digit sum of 1234567812345678 is 3636, so 3∣123456783 \mid 12345678; but the last two digits give

78=4(19)+2,\begin{align*} 78 &= 4(19) + 2, \end{align*}

so 4∤123456784 \nmid 12345678. Therefore 12∤1234567812 \nmid 12345678, even though it is divisible by 99; one failed piece is enough to kill the whole test.

Example. Is 40,740,71140{,}740{,}711 divisible by 3333? By 9999?
Since 33=3×1133 = 3 \times 11 and gcd⁡(3,11)=1\gcd(3,11)=1, we run the 33 test and the 1111 test and combine them using the coprime factor proposition.
Digit sum:

4+0+7+4+0+7+1+1=24=3(8),\begin{align*} 4+0+7+4+0+7+1+1 &= 24 \\ &= 3(8), \end{align*}

so 3∣407407113 \mid 40740711, but 9∤249 \nmid 24 so 9∤407407119 \nmid 40740711.
Alternating sum (units digit first):

1−1+7−0+4−7+0−4=0,\begin{align*} 1 - 1 + 7 - 0 + 4 - 7 + 0 - 4 &= 0, \end{align*}

so 11∣4074071111 \mid 40740711. Both the 33 test and 1111 test pass, therefore 33∣4074071133 \mid 40740711 (in fact 40740711=33×123456740740711 = 33 \times 1234567). However 99=9×1199 = 9 \times 11 needs the 99 test to pass, and it fails; therefore 99∤4074071199 \nmid 40740711. Notice how one digit sum and one alternating sum settled divisibility by 3,9,11,333, 9, 11, 33 and 9999 all at once; this is why you factorise the modulus first instead of long-dividing.

Designing Your Own Tests#

The tests for 77 and 1313 came from one recipe, and the lecture notes point out it generalises: for any prime pp other than 22 and 55, look for a small multiple of pp that is one more or one less than a multiple of 1010. We can package the whole recipe as a proposition.

Note

Proposition (build-your-own truncation test)
Let pp be a prime with gcd⁡(p,10)=1\gcd(p, 10) = 1, and suppose 10m≡ε(modp)10m \equiv \varepsilon \pmod p for some integer mm and ε∈{1,−1}\varepsilon \in \{1, -1\}. Then for any integer N=10a+bN = 10a + b with last digit bb,

p∣N  ⟺  p∣a+εmb.p \mid N \iff p \mid a + \varepsilon m b.

Proof. Multiply NN by mm;

mN=(10m)a+mb≡εa+mb(modp), (as 10m≡ε)≡ε(a+εmb)(modp), (as ε2=1).\begin{align*} mN &= (10m)a + mb \\ &\equiv \varepsilon a + mb \pmod p, \text{ (as } 10m \equiv \varepsilon\text{)} \\ &\equiv \varepsilon(a + \varepsilon m b) \pmod p, \text{ (as } \varepsilon^2 = 1\text{)}. \end{align*}

Now p∤mp \nmid m (otherwise 10m≡0≢±1(modp)10m \equiv 0 \not\equiv \pm 1 \pmod p), so multiplying by mm preserves divisibility by pp; and multiplying by ε=±1\varepsilon = \pm 1 obviously does too. Hence p∣N  ⟺  p∣mN  ⟺  p∣a+εmbp \mid N \iff p \mid mN \iff p \mid a + \varepsilon m b. ■\blacksquare

Sanity check against what we already have: for p=7p = 7, 21≡021 \equiv 0 gives 10(2)≡−110(2) \equiv -1, so m=2m = 2, ε=−1\varepsilon = -1 and the test is a−2ba - 2b; for p=13p = 13, 39≡039 \equiv 0 gives 10(4)≡110(4) \equiv 1, so m=4m = 4, ε=1\varepsilon = 1 and the test is a+4ba + 4b. Both match the lemmas above.

Example. Build a truncation test for 1717, and use it to decide whether 17∣202317 \mid 2023.
We want a multiple of 1717 next to a multiple of 1010; 51=3×1751 = 3 \times 17 works, since 51=50+151 = 50 + 1 gives

50≡−1(mod17)10(5)≡−1(mod17),\begin{align*} 50 &\equiv -1 \pmod{17} \\ 10(5) &\equiv -1 \pmod{17}, \end{align*}

so m=5m = 5, ε=−1\varepsilon = -1: remove the last digit and subtract 5 times it. Applying this to 20232023;

202−5(3)=187,18−5(7)=−17.\begin{align*} 202 - 5(3) &= 187, \\ 18 - 5(7) &= -17. \end{align*}

Since 17∣−1717 \mid -17, we conclude 17∣202317 \mid 2023. Therefore 20232023 is divisible by 1717; indeed 2023=7×1722023 = 7 \times 17^2. (Negative outputs are fine; divisibility doesn't care about sign.)

Example. Build a truncation test for 1919, and use it to decide whether 19∣226119 \mid 2261.
Here 1919 itself is one less than 2020, so

20≡1(mod19)10(2)≡1(mod19),\begin{align*} 20 &\equiv 1 \pmod{19} \\ 10(2) &\equiv 1 \pmod{19}, \end{align*}

giving m=2m = 2, ε=1\varepsilon = 1: remove the last digit and add 2 times it. Applying this to 22612261;

226+2(1)=228,22+2(8)=38.\begin{align*} 226 + 2(1) &= 228, \\ 22 + 2(8) &= 38. \end{align*}

Since 38=19×238 = 19 \times 2, we conclude 19∣226119 \mid 2261. Therefore 22612261 is divisible by 1919; indeed 2261=19×1192261 = 19 \times 119.

Block Tests#

The digit-sum idea also generalises: instead of asking how 1010 behaves mod pp, ask how 10k10^k behaves for small kk. If 10k≡1(modp)10^k \equiv 1 \pmod p, then chopping NN into blocks of kk digits (from the right) and summing the blocks preserves the value of NN mod pp — exactly the digit-sum proof with 10k10^k playing the role of 1010. And because we never multiply by anything, block tests preserve remainders, unlike the truncation tests.

Example. Build a test for 3737, and use it to decide whether 37∣45676537 \mid 456765.
Notice 999=27×37999 = 27 \times 37, so

103≡1(mod37),\begin{align*} 10^3 &\equiv 1 \pmod{37}, \end{align*}

which means every 3-digit block contributes itself: chop into blocks of three and add. For 456765456765 the blocks are 456456 and 765765;

456+765=1221=37(33).\begin{align*} 456 + 765 &= 1221 \\ &= 37(33). \end{align*}

Since 37∣122137 \mid 1221, we conclude 37∣45676537 \mid 456765 (indeed 456765=37×12345456765 = 37 \times 12345). Therefore 456765456765 is divisible by 3737. As a bonus, the same fact 999=27×37999 = 27 \times 37 gives an identical 3-digit block test for 2727; here 1221=27×45+61221 = 27 \times 45 + 6, so 27∤45676527 \nmid 456765.

Example. Use 1001=7×11×131001 = 7 \times 11 \times 13 to test 1234512345 for divisibility by 77, 1111 and 1313 simultaneously.
Since 1001≡01001 \equiv 0 modulo each of 7,11,137, 11, 13, we get

103≡−1(mod7),(mod11) and(mod13),\begin{align*} 10^3 &\equiv -1 \pmod{7}, \pmod{11} \text{ and} \pmod{13}, \end{align*}

so the 3-digit blocks alternate in sign, just like the 1111 test but with blocks instead of digits. For 1234512345 the blocks from the right are 345345 and 1212;

345−12=333.\begin{align*} 345 - 12 &= 333. \end{align*}

Now reduce 333333 by each prime: 333=7(47)+4333 = 7(47) + 4, so 12345≡4(mod7)12345 \equiv 4 \pmod 7; 333=11(30)+3333 = 11(30) + 3, so 12345≡3(mod11)12345 \equiv 3 \pmod{11}; 333=13(25)+8333 = 13(25) + 8, so 12345≡8(mod13)12345 \equiv 8 \pmod{13}. Therefore 1234512345 is divisible by none of 77, 1111 or 1313 — and notice how the mod 77 and mod 1313 remainders here are the actual remainders, consistent with (and more informative than) the truncation tests we ran on 1234512345 earlier. For a satisfying yes-instance, take 999999999999: its blocks give 999−999=0999 - 999 = 0, so 999999999999 is divisible by 77, 1111 and 1313 all at once.

So when you're handed an unfamiliar divisor on an exam, the decision goes:

  • n=2a5bn = 2^a 5^b: look at the last max⁡(a,b)\max(a,b) digits.
  • n∈{3,9,11}n \in \{3, 9, 11\} (or you want a remainder cheaply): digit sums and alternating sums.
  • nn composite: factorise into coprime prime powers and test each piece.
  • nn some other prime: find a multiple of nn near a multiple of 1010 for a quick yes/no truncation test, or a power 10k≡±1(modn)10^k \equiv \pm 1 \pmod n if you also want remainders.

Table of Divisibility Tests#

The lecture's full table, for quick revision:

nn Test for divisibility by nn
22 Last digit is divisible by 22.
33 Sum of digits is divisible by 33.
44 Last 22 digits is divisible by 44.
55 Last digit is divisible by 55.
66 Divisible by 22 and by 33.
77 Removing last digit and subtracting 22 times that digit is divisible by 77.
88 Last 33 digits is divisible by 88.
99 Sum of digits is divisible by 99.
1010 Last digit is 00 (divisible by both 22 and 55).
1111 Alternating signed sum of digits is divisible by 1111.
1212 Divisible by 44 and by 33.
1313 Removing last digit and adding 44 times that digit is divisible by 1313.

Tests for primes (or prime powers) beyond 1313 are built exactly as in the previous section: seek small multiples of the prime that are 11 more or 11 less than a multiple of 1010, or a small power of 1010 that is ±1\pm 1 mod the prime.