Divisibility Tests

How Divisibility Tests Work

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

where each digit . Also recall that congruences respect addition and multiplication; if we reduce everything mod , the digits stay exactly where they are and only the powers of change. This gives us the master congruence that every test in this lecture falls out of:

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

  • from some point onwards (this happens exactly when divides a power of , i.e. ); then only the last few digits matter.
  • (this is or ); then every power of is and we just sum the digits.
  • (this is ); then the powers alternate between and and we alternately add and subtract digits.

For everything else (like and ), the powers of 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 if and only if the number formed by its last digits is divisible by .

Note

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

Basically, and are the prime factors of , so high enough powers of absorb any power of or ; everything past the last digits is invisible mod and mod , so you can just throw it away.

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

Since , we have , i.e. . Reducing mod ,

Therefore if and only if . The exact same argument with proves the second lemma.

The small cases are the ones you use constantly:

  • : last digit is even,
  • : last two digits divisible by ,
  • : last three digits divisible by ,
  • : last digit is or ,
  • : last digit is (since , this is really the test and the 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 and divide ?
Work through the last digits one layer at a time;

Notice how once fails there is no point testing ; if then , so the powers of stop dead at the first failure. Therefore, the only powers dividing are and ; in fact .

Example. Find the largest power of dividing .
The lemma works for any , not just the in the standard table; we keep grabbing one more digit until the test fails.

Therefore, the largest power of dividing is . It is important to note that trailing zeros hand you factors for free: three trailing zeros means 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 if and only if the sum of its digits is divisible by .

Note

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

This is the famous digit-sum test, and the reason it works is one line of modular arithmetic: , so every power of is congruent to , and each digit just contributes itself.

Proof. Let . Since , we have for every (congruences respect multiplication, so we may raise both sides to the ​th power). Then

So is congruent to its own digit sum mod ; in particular if and only if divides the digit sum. Since as well, the identical argument works mod .

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

The digit sum doesn't just tell you whether ; 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 and are divisible by or by ?
For ,

so , but so ; in fact .
For ,

so , and hence automatically too. Therefore, is divisible by only, while is divisible by both and .

Casting Out Nines

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

Example. A friend claims that . Check the claim by casting out nines.
Reduce everything mod using digit sums;

Since , the claimed answer is wrong without us ever doing the addition. Therefore, the sum cannot be ; the true answer is , which indeed satisfies .

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 test below, where position actually matters.

Divisibility by 11

Note

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

The mechanism is the mirror image of the digit-sum test: this time , so the powers of flip sign at every position, i.e. .

Proof. Let . Since , we have , so

Hence if and only if divides the alternating sum of the digits.

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 and are divisible by ?
For , starting from the units digit,

and , so (in fact ).
For ,

and every number divides , so . Therefore, only is divisible by ; unsurprising once you notice and .

Catching Digit Swaps

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

Example. A friend claims that . Check the claim mod and mod .
First cast out nines;

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

Since , the claim is false. Therefore, the product cannot be ; the true answer is (the last two digits were swapped), and you can check its alternating sum is as required.

Divisibility by 7

Note

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

In symbols: write where is the last digit and is the rest of the number; then if and only if . 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 mod cycle through — no nice pattern — so no digit-sum-style test exists. The trick from the lectures: look for a small multiple of that sits next to a multiple of . We find , so , and multiplying by makes everything collapse.

Proof. Let . Multiplying by ,

Since , multiplying by does not create or destroy factors of ; hence .

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

Since , we conclude .
For ,

Since , we conclude (indeed ). Therefore, only is divisible by . You could also have stopped at if you recognised ; 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 : the test gives , but . The culprit is that we multiplied by along the way, which scrambles every nonzero residue while fixing . So use this for yes/no questions only; if an exam question wants , do it directly.

Divisibility by 13

Note

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

Same idea as for : we hunt for a small multiple of adjacent to a multiple of , and this time works, giving .

Proof. Let . Multiplying by ,

Since , we get .

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

Since , we conclude .
For ,

Since , we conclude (indeed ). Therefore, only is divisible by . The same remainder warning applies here: because we multiplied by , the final number tells you nothing about beyond the fact that it is .

Tests for Composite Numbers

What about , , , 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 be integers with . Then for any integer ,

Proof. () If , then since and , transitivity of divisibility (Lemma 1.2 in Divisibility and Primes) gives and .
() Suppose and , so for some . Then , and since , has no factors in common with , so must divide ; write . Then , so .

Basically, a number is divisible by exactly when it passes the test and the test, and divisible by exactly when it passes the test and the test.

You must split into coprime pieces; being divisible by and by does not make a number divisible by , because . For example, is divisible by (last two digits ) and by (even, digit sum ), yet , so . The correct split is ; the last three digits give , so the test fails and , as expected.

Example. Is divisible by ? Is divisible by ?
For (coprime pieces): ends in , so ; its digit sum is , so . Both tests pass, therefore .
For (coprime pieces): the digit sum of is , so ; but the last two digits give

so . Therefore , even though it is divisible by ; one failed piece is enough to kill the whole test.

Example. Is divisible by ? By ?
Since and , we run the test and the test and combine them using the coprime factor proposition.
Digit sum:

so , but so .
Alternating sum (units digit first):

so . Both the test and test pass, therefore (in fact ). However needs the test to pass, and it fails; therefore . Notice how one digit sum and one alternating sum settled divisibility by and all at once; this is why you factorise the modulus first instead of long-dividing.

Designing Your Own Tests

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

Note

Proposition (build-your-own truncation test)
Let be a prime with , and suppose for some integer and . Then for any integer with last digit ,

Proof. Multiply by ;

Now (otherwise ), so multiplying by preserves divisibility by ; and multiplying by obviously does too. Hence .

Sanity check against what we already have: for , gives , so , and the test is ; for , gives , so , and the test is . Both match the lemmas above.

Example. Build a truncation test for , and use it to decide whether .
We want a multiple of next to a multiple of ; works, since gives

so , : remove the last digit and subtract 5 times it. Applying this to ;

Since , we conclude . Therefore is divisible by ; indeed . (Negative outputs are fine; divisibility doesn't care about sign.)

Example. Build a truncation test for , and use it to decide whether .
Here itself is one less than , so

giving , : remove the last digit and add 2 times it. Applying this to ;

Since , we conclude . Therefore is divisible by ; indeed .

Block Tests

The digit-sum idea also generalises: instead of asking how behaves mod , ask how behaves for small . If , then chopping into blocks of digits (from the right) and summing the blocks preserves the value of mod — exactly the digit-sum proof with playing the role of . And because we never multiply by anything, block tests preserve remainders, unlike the truncation tests.

Example. Build a test for , and use it to decide whether .
Notice , so

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

Since , we conclude (indeed ). Therefore is divisible by . As a bonus, the same fact gives an identical 3-digit block test for ; here , so .

Example. Use to test for divisibility by , and simultaneously.
Since modulo each of , we get

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

Now reduce by each prime: , so ; , so ; , so . Therefore is divisible by none of , or — and notice how the mod and mod remainders here are the actual remainders, consistent with (and more informative than) the truncation tests we ran on earlier. For a satisfying yes-instance, take : its blocks give , so is divisible by , and all at once.

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

  • : look at the last digits.
  • (or you want a remainder cheaply): digit sums and alternating sums.
  • composite: factorise into coprime prime powers and test each piece.
  • some other prime: find a multiple of near a multiple of for a quick yes/no truncation test, or a power if you also want remainders.

Table of Divisibility Tests

The lecture's full table, for quick revision:

Test for divisibility by
Last digit is divisible by .
Sum of digits is divisible by .
Last digits is divisible by .
Last digit is divisible by .
Divisible by and by .
Removing last digit and subtracting times that digit is divisible by .
Last digits is divisible by .
Sum of digits is divisible by .
Last digit is (divisible by both and ).
Alternating signed sum of digits is divisible by .
Divisible by and by .
Removing last digit and adding times that digit is divisible by .

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