TExcelTAD
Home › Discrete Mathematics › Divisibility

Divisibility

Number theory studies the integers, and its starting point is divisibility. Once a pure subject, it now protects every HTTPS connection: RSA and elliptic-curve cryptography, hash functions and checksums are built on the facts in this part of the course. If primes and remainders are new to you, the school-level lessons Factors and Multiples and Divisibility Rules are a gentle start.

Definition and basic facts

For integers aa and bb, aa divides bb, written a∣ba \mid b, if b=kab = ka for some integer kk. Then aa is a divisor (factor) of bb and bb is a multiple of aa. For example 3∣123 \mid 12, −4∣12-4 \mid 12, 7∤127 \nmid 12, and every integer divides 0.

Facts that follow straight from the definition (for integers a,b,ca, b, c):

Fact Reason
if a∣ba \mid b and b∣cb \mid c then a∣ca \mid c c=jb=j(ka)c = jb = j(ka)
if a∣ba \mid b and a∣ca \mid c then a∣sb+tca \mid sb + tc for all s,ts, t sb+tc=(sk+tl)asb + tc = (sk + tl)a
if a∣ba \mid b and b≠0b \ne 0 then ∣a∣≤∣b∣\lvert a \rvert \le \lvert b \rvert ∣b∣=∣k∣∣a∣\lvert b \rvert = \lvert k \rvert \lvert a \rvert with ∣k∣≥1\lvert k \rvert \ge 1

The second fact, "a divisor of two numbers divides every integer linear combination of them", is the engine of the next lesson.

The division theorem

Theorem. For every integer nn and every integer d>0d > 0 there are unique integers qq (quotient) and rr (remainder) with

n=q⋅d+r,0≤r<dn = q \cdot d + r, \qquad 0 \le r < d

We write q=qcnt⁡(n,d)q = \operatorname{qcnt}(n, d) and r=rem⁡(n,d)r = \operatorname{rem}(n, d), or n mod dn \bmod d. Existence: take rr to be the smallest non-negative number of the form n−qdn - qd (the Well Ordering Principle); if r≥dr \ge d then r−dr - d would be smaller. Uniqueness: if qd+r=q′d+r′qd + r = q'd + r' with both remainders in [0,d)[0, d), then d∣r−r′d \mid r - r' with ∣r−r′∣<d\lvert r - r' \rvert < d, so r=r′r = r'.

Python's divmod(n, d) returns exactly this pair for d>0d > 0, including for negative nn. C, Java and JavaScript truncate toward zero instead, giving a negative remainder for negative nn:

python · try it yourself
import math

for n, d in [(17, 5), (-17, 5), (100, 7)]:
    q, r = divmod(n, d)
    print(f"divmod({n}, {d}) = ({q}, {r})   check: {q}*{d}+{r} = {q * d + r}")
print("C-style truncation for -17 / 5:", int(-17 / 5), "remainder", int(math.fmod(-17, 5)))
divmod(17, 5) = (3, 2)   check: 3*5+2 = 17
divmod(-17, 5) = (-4, 3)   check: -4*5+3 = -17
divmod(100, 7) = (14, 2)   check: 14*7+2 = 100
C-style truncation for -17 / 5: -3 remainder -2

Digit-sum tests

Why does "3 divides a number exactly when it divides its digit sum" work? A number with digits dk…d1d0d_k \ldots d_1 d_0 is ∑di10i\sum d_i 10^i, and 10i−1=99…910^i - 1 = 99\ldots9 is a multiple of 9 (hence of 3). So

n−(d0+d1+⋯+dk)=∑idi(10i−1)n - (d_0 + d_1 + \cdots + d_k) = \sum_i d_i (10^i - 1)

is a multiple of 9, and nn and its digit sum leave the same remainder modulo 9 and modulo 3. The rule for 11 uses the alternating digit sum, because 10i−(−1)i10^i - (-1)^i is a multiple of 11.

python · try it yourself
def digit_sum(n):
    return sum(int(c) for c in str(n))

def alt_digit_sum(n):
    return sum((-1) ** i * int(c) for i, c in enumerate(reversed(str(n))))

for n in [123456789, 918273645, 121, 9174, 31415926]:
    print(f"{n}: n mod 9 = {n % 9}, digit sum mod 9 = {digit_sum(n) % 9};"
          f" n mod 11 = {n % 11}, alt sum mod 11 = {alt_digit_sum(n) % 11}")
123456789: n mod 9 = 0, digit sum mod 9 = 0; n mod 11 = 5, alt sum mod 11 = 5
918273645: n mod 9 = 0, digit sum mod 9 = 0; n mod 11 = 3, alt sum mod 11 = 3
121: n mod 9 = 4, digit sum mod 9 = 4; n mod 11 = 0, alt sum mod 11 = 0
9174: n mod 9 = 3, digit sum mod 9 = 3; n mod 11 = 0, alt sum mod 11 = 0
31415926: n mod 9 = 4, digit sum mod 9 = 4; n mod 11 = 3, alt sum mod 11 = 3

The die-hard water jug puzzle

You have a 3-gallon jug and a 5-gallon jug and an unlimited water supply. Can you measure exactly 4 gallons? With 21- and 26-gallon jugs, can you measure 3? With 6- and 9-gallon jugs, can you measure 4?

The invariant: every amount in either jug is always a linear combination 3s+5t3s + 5t (a fact about state machines). So any common divisor of the two jug sizes divides every amount you can measure. With 6 and 9, every amount is a multiple of 3, so 4 is impossible. The next lesson shows the converse: every multiple of gcd⁡(a,b)\gcd(a, b) up to bb is reachable.

In software

The remainder operator is everywhere: i % n wraps indices in circular buffers, hash(key) % size picks a hash-table bucket, x % 2 == 0 tests parity, and check digits (ISBN, credit cards with Luhn's algorithm, IBAN mod 97) catch typos. Be careful with negative numbers when porting code between Python and C-family languages.

Exercises

Exercise 1: True or false

(a) 6∣426 \mid 42; (b) −5∣35-5 \mid 35; (c) 0∣50 \mid 5; (d) 5∣05 \mid 0; (e) if 4∣a4 \mid a and 4∣b4 \mid b then 4∣3a−7b4 \mid 3a - 7b.

Answer

(a) T, (b) T, (c) F, (d) T, (e) T.

Solution

(a) 42=7⋅642 = 7 \cdot 6. (b) 35=(−7)(−5)35 = (-7)(-5). (c) 5=k⋅05 = k \cdot 0 has no solution. (d) 0=0⋅50 = 0 \cdot 5. (e) a divisor of aa and bb divides every linear combination.

Exercise 2: Quotient and remainder

Find qq and rr with 0≤r<d0 \le r < d for (a) n=100n = 100, d=9d = 9; (b) n=−100n = -100, d=9d = 9.

Answer

(a) q=11q = 11, r=1r = 1; (b) q=−12q = -12, r=8r = 8.

Solution

(a) 100=11⋅9+1100 = 11 \cdot 9 + 1. (b) −100=−12⋅9+8-100 = -12 \cdot 9 + 8; note q=−11q = -11 would give r=−1r = -1, which is not allowed.

Exercise 3: Divisible by 11

Is 8,291,072 divisible by 11? Use the alternating digit sum and check in Python.

Answer

No, the remainder is 9.

Solution

From the right: 2−7+0−1+9−2+8=92 - 7 + 0 - 1 + 9 - 2 + 8 = 9. The alternating sum is not a multiple of 11, so neither is the number; both leave remainder 9.

python · try it yourself
n = 8291072
s = sum((-1) ** i * int(c) for i, c in enumerate(reversed(str(n))))
print("alternating sum:", s, " mod 11:", s % 11, " n mod 11:", n % 11)
alternating sum: 9  mod 11: 9  n mod 11: 9

Test yourself

What does divmod(-7, 2) return in Python?

  1. (-3, -1)
  2. (-4, 1)
  3. (-3, 1)
  4. (-4, -1)

If d∣ad \mid a and d∣bd \mid b, which number must dd also divide?

  1. a/ba / b
  2. 5a−2b5a - 2b
  3. a+1a + 1
  4. ab+1ab + 1

With jugs of 6 and 10 gallons, which amount can never be measured?

  1. 2
  2. 4
  3. 6
  4. 5