MAT2033 · Week 7 · divisibility, modular arithmetic, primes

Divisibility, modular arithmetic and primes

a | b, the division algorithm (div and mod), congruence, prime factorization, GCD/LCM and the Euclidean algorithm. The foundation of the cryptography week, and a final exam topic.

dividesdivision algorithmquotientremainderdivmodcongruence modulo mprimecompositeFundamental Theorem of Arithmeticgreatest common divisor (gcd)least common multiple (lcm)Euclidean algorithmBezout's identityrelatively prime

This week we begin number theory. It is not on the midterm, it is a final exam topic. But next week (cryptography, RSA) is built entirely on it: GCD, taking mods and the Euclidean algorithm are needed in every question there. Golden rules:

  1. a∣ba \mid b is a proposition, not a number. “3 divides 12” is true or false; 12/3=412/3 = 4 is a number.
  2. The remainder is never negative: 0≤r<d0 \le r < d. For a negative number the quotient goes one lower.
  3. In mod m, shrink the numbers before adding and multiplying. You never have to compute the big product.
  4. For the GCD use Euclid: gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b), until the remainder is 0.
  5. In a proof question, write the definition: if a∣ba \mid b then b=akb = ak. That line is half of the proof.

Divisibility

Let a≠0a \ne 0. a divides b (a∣ba \mid b) means there is an integer cc with b=acb = ac. aa is called a factor (divisor) of bb, and bb a multiple of aa.

Examples: 4∣284 \mid 28 because 28=4⋅728 = 4 \cdot 7. 5∤285 \nmid 28 because 28/5=5.628/5 = 5.6 is not an integer. 7∣07 \mid 0 because 0=7⋅00 = 7 \cdot 0.

Basic properties (a,b,ca, b, c integers):

PropertyMeaning
If a∣ba \mid b and a∣ca \mid c then a∣(b+c)a \mid (b + c)A common divisor divides the sum too
If a∣ba \mid b then a∣bca \mid bc for every ccA divisor also divides multiples
If a∣ba \mid b and b∣cb \mid c then a∣ca \mid cTransitivity
If a∣ba \mid b and a∣ca \mid c then a∣(mb+nc)a \mid (mb + nc)It divides every linear combination

A fully written proof

Let’s prove transitivity the way you would write it on the exam.

Claim. If a | b and b | c, then a | c.
Proof. Suppose a | b and b | c. Then there are integers k and l
such that b = ak and c = bl.
Substituting, c = bl = (ak)l = a(kl).
Since kl is an integer, a | c by the definition of divisibility. ∎

Note: for the two separate divisibilities we used two separate letters (kk and ll). Using the same letter would mean ”bb and cc have the same coefficient”, and the proof would break.

The division algorithm

For every integer aa and positive integer dd there are unique qq and rr with:

a=dq+r,0≤r<d.a = dq + r, \qquad 0 \le r < d.

q=a div dq = a \textbf{ div } d (quotient), r=a mod dr = a \bmod d (remainder).

101 = 7 * 14 + 3      ->  101 div 7 = 14,  101 mod 7 = 3
-23 = 6 * (-4) + 1    ->  -23 div 6 = -4,  -23 mod 6 = 1

PITFALL · the remainder of a negative number. Writing −23=6⋅(−3)−5-23 = 6 \cdot (-3) - 5 and saying the remainder is −5-5 is wrong: the remainder must satisfy 0≤r<60 \le r < 6. Take the quotient one lower (−4-4) and the remainder comes out positive (11). Java’s % operator gives −5-5 here, while the mathematical mod gives 11.

Congruence and modular arithmetic

a≡b(modm)a \equiv b \pmod m (a is congruent to b modulo m) means m∣(a−b)m \mid (a - b). The same thing: aa and bb leave the same remainder when divided by mm.

Example: 38≡14(mod12)38 \equiv 14 \pmod{12}, because 38−14=24=12⋅238 - 14 = 24 = 12 \cdot 2. A clock shows 2 at hour 38 and at hour 14.

Rules: if a≡ba \equiv b and c≡d(modm)c \equiv d \pmod m then

a+c≡b+d(modm),ac≡bd(modm).a + c \equiv b + d \pmod m, \qquad ac \equiv bd \pmod m.

So when computing big expressions, you shrink each part first:

(17 * 29 + 8) mod 5
17 mod 5 = 2,  29 mod 5 = 4,  8 mod 5 = 3
2 * 4 + 3 = 11,  11 mod 5 = 1
Check: 17 * 29 + 8 = 501 = 5 * 100 + 1

TIP · big powers. In an an mod ma^n \bmod m question, first look for a small kk with ak≡1a^k \equiv 1. If you find it, divide nn by kk and compute the power of the remainder. If there is no such kk, use the fast exponentiation (repeated squaring) method from the cryptography week.

Prime numbers

If p>1p > 1 and its only positive divisors are 11 and pp, then pp is prime. A number greater than 1 that is not prime is composite. 1 is neither prime nor composite.

Fundamental Theorem of Arithmetic: every integer greater than 1 can be written as a product of primes in exactly one way, apart from the order. Example: 600=23⋅3⋅52600 = 2^3 \cdot 3 \cdot 5^2.

Trial division: if nn is composite, it has a prime divisor less than or equal to n\sqrt{n}. The reason: if n=abn = ab and both were greater than n\sqrt{n}, then ab>nab > n. So to show that 97 is prime, it is enough to try only 2, 3, 5, 7 (97≈9.8\sqrt{97} \approx 9.8). None divides it, so 97 is prime.

There are infinitely many primes (Euclid). Short proof idea: suppose there are finitely many, p1,…,pnp_1, \dots, p_n. The number Q=p1p2⋯pn+1Q = p_1 p_2 \cdots p_n + 1 is divisible by none of the pip_i (the remainder is always 1). But QQ must have a prime divisor. We found a prime not in the list: contradiction.

GCD and LCM

gcd⁡(a,b)\gcd(a, b): the largest integer that divides both. lcm⁡(a,b)\operatorname{lcm}(a, b): the smallest positive integer that both divide. If gcd⁡(a,b)=1\gcd(a, b) = 1, then aa and bb are relatively prime.

With factorization: for the GCD take the smaller exponent of each prime, for the LCM the larger exponent. And always

gcd⁡(a,b)⋅lcm⁡(a,b)=ab.\gcd(a, b) \cdot \operatorname{lcm}(a, b) = ab.

Example: gcd⁡(12,18)=6\gcd(12, 18) = 6, lcm⁡(12,18)=36\operatorname{lcm}(12, 18) = 36, 6⋅36=216=12⋅186 \cdot 36 = 216 = 12 \cdot 18.

The Euclidean algorithm

Factoring large numbers is hard. The Euclidean algorithm is based on this fact: if a=bq+ra = bq + r then gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r). Divide until the remainder is zero; the last remainder before zero is the GCD.

gcd(2024, 748)
2024 = 2 * 748 + 528
 748 = 1 * 528 + 220
 528 = 2 * 220 + 88
 220 = 2 * 88  + 44
  88 = 2 * 44  + 0     ->  gcd = 44
lcm = 2024 * 748 / 44 = 34408

Bezout’s identity

There are always integers s,ts, t with gcd⁡(a,b)=sa+tb\gcd(a, b) = sa + tb. To find them, you substitute the Euclid steps backwards:

44 = 220 - 2*88
   = 220 - 2*(528 - 2*220)        = 5*220 - 2*528
   = 5*(748 - 528) - 2*528        = 5*748 - 7*528
   = 5*748 - 7*(2024 - 2*748)     = 19*748 - 7*2024
s = -7, t = 19.  Check: -14168 + 14212 = 44

Next week this computation will be exactly the method for finding the modular inverse. Spend time on it here.

Common mistakes

Wrong ideaThe truth
a∣ba \mid b and a/ba / b are the same thinga∣ba \mid b is a proposition (T/F), a/ba/b is a number
−23 mod 6=−5-23 \bmod 6 = -5The remainder satisfies 0≤r<60 \le r < 6: −23 mod 6=1-23 \bmod 6 = 1
11 is prime1 is neither prime nor composite
For primality, try up to n−1n-1Primes up to n\sqrt{n} are enough
For the GCD take the larger exponentsGCD takes the smaller exponent, LCM the larger
In Euclid the last divisor is the GCDThe last remainder before zero is the GCD
In a proof, b=akb = ak, c=akc = akDifferent letters: b=akb = ak, c=alc = al
For (a⋅b) mod m(a \cdot b) \bmod m, first the full productFirst a mod ma \bmod m and b mod mb \bmod m, then multiply

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Find the quotient and remainder when 157 is divided by 12.

Show answer
q = 13, r = 1

The largest multiple of 12 that fits into 157 is 12⋅13=15612 \cdot 13 = 156. The remainder is 157−156=1157 - 156 = 1. So 157=12⋅13+1157 = 12 \cdot 13 + 1, and the condition 0≤1<120 \le 1 < 12 holds.

Question 2medium

Find −47 div 9-47 \textbf{ div } 9 and −47 mod 9-47 \bmod 9.

Show answer
-6 and 7

The remainder cannot be negative: 0≤r<90 \le r < 9. If you try 9⋅(−5)=−459 \cdot (-5) = -45 then −47−(−45)=−2-47 - (-45) = -2, which is negative, not allowed. Go one lower: 9⋅(−6)=−549 \cdot (-6) = -54, −47−(−54)=7-47 - (-54) = 7. So −47=9⋅(−6)+7-47 = 9 \cdot (-6) + 7: div =−6= -6, mod =7= 7.

Question 3easy

Is 17≡−8(mod5)17 \equiv -8 \pmod 5? Justify.

Show answer
Yes

Definition: a≡b(modm)  ⟺  m∣(a−b)a \equiv b \pmod m \iff m \mid (a - b). 17−(−8)=25=5⋅517 - (-8) = 25 = 5 \cdot 5. 5 divides 25, so yes. Check: 17 mod 5=217 \bmod 5 = 2 and −8 mod 5=2-8 \bmod 5 = 2, the remainders are the same.

Question 4medium

Without multiplying out, compute (73⋅58+41) mod 11(73 \cdot 58 + 41) \bmod 11.

Show answer
7

First reduce each number mod 11: 73 mod 11=773 \bmod 11 = 7, 58 mod 11=358 \bmod 11 = 3, 41 mod 11=841 \bmod 11 = 8. Then 7⋅3+8=297 \cdot 3 + 8 = 29, 29 mod 11=729 \bmod 11 = 7. Check: 73⋅58+41=4275=11⋅388+773 \cdot 58 + 41 = 4275 = 11 \cdot 388 + 7.

Question 5easy

Find the prime factorization of 1260.

Show answer
2^2 · 3^2 · 5 · 7

Start with the small primes: 1260=2⋅630=22⋅3151260 = 2 \cdot 630 = 2^2 \cdot 315. 315=3⋅105=32⋅35315 = 3 \cdot 105 = 3^2 \cdot 35. 35=5⋅735 = 5 \cdot 7. Result 1260=22⋅32⋅5⋅71260 = 2^2 \cdot 3^2 \cdot 5 \cdot 7.

Question 6medium

Is 221 prime? Justify by trial division.

Show answer
No, 221 = 13 · 17

221≈14.9\sqrt{221} \approx 14.9, so it is enough to try the primes up to 14: 2, 3, 5, 7, 11, 13. 2, 3, 5, 7, 11 do not divide it; 221/13=17221 / 13 = 17. Composite: 221=13⋅17221 = 13 \cdot 17.

Question 7medium

Use the Euclidean algorithm to find gcd⁡(1071,462)\gcd(1071, 462). Then find lcm⁡(1071,462)\operatorname{lcm}(1071, 462).

Show answer
gcd = 21, lcm = 23562

1071=2⋅462+1471071 = 2 \cdot 462 + 147, 462=3⋅147+21462 = 3 \cdot 147 + 21, 147=7⋅21+0147 = 7 \cdot 21 + 0. The last nonzero remainder: 21. For the LCM use gcd⁡⋅lcm⁡=ab\gcd \cdot \operatorname{lcm} = ab: lcm⁡=1071⋅46221=51⋅462=23562\operatorname{lcm} = \frac{1071 \cdot 462}{21} = 51 \cdot 462 = 23562.

Question 8medium

Let a=23⋅32⋅7a = 2^3 \cdot 3^2 \cdot 7 and b=22⋅35⋅5b = 2^2 \cdot 3^5 \cdot 5. Find gcd⁡(a,b)\gcd(a,b) and lcm⁡(a,b)\operatorname{lcm}(a,b).

Show answer
gcd = 36, lcm = 68040

GCD: the smaller exponent of each prime: 2min⁡(3,2)⋅3min⁡(2,5)⋅50⋅70=22⋅32=362^{\min(3,2)} \cdot 3^{\min(2,5)} \cdot 5^0 \cdot 7^0 = 2^2 \cdot 3^2 = 36. LCM: the larger exponent of each prime: 23⋅35⋅5⋅7=8⋅243⋅35=680402^3 \cdot 3^5 \cdot 5 \cdot 7 = 8 \cdot 243 \cdot 35 = 68040.

Question 9hard

Find integers ss and tt such that gcd⁡(252,198)=252s+198t\gcd(252, 198) = 252s + 198t.

Show answer
s = 4, t = -5 (gcd = 18)

Euclid: 252=1⋅198+54252 = 1 \cdot 198 + 54, 198=3⋅54+36198 = 3 \cdot 54 + 36, 54=1⋅36+1854 = 1 \cdot 36 + 18, 36=2⋅1836 = 2 \cdot 18. The GCD is 18. Substitute back: 18=54−36=54−(198−3⋅54)=4⋅54−198=4(252−198)−198=4⋅252−5⋅19818 = 54 - 36 = 54 - (198 - 3 \cdot 54) = 4 \cdot 54 - 198 = 4(252 - 198) - 198 = 4 \cdot 252 - 5 \cdot 198. Check: 1008−990=181008 - 990 = 18.

Question 10hard

Prove: if a∣ba \mid b and a∣ca \mid c, then a∣(3b−2c)a \mid (3b - 2c).

Show answer
3b - 2c = a(3k - 2l) with k, l integers

Write the definition: a∣ba \mid b gives b=akb = ak, a∣ca \mid c gives c=alc = al (k,lk, l integers, different letters!). Then 3b−2c=3ak−2al=a(3k−2l)3b - 2c = 3ak - 2al = a(3k - 2l). Since 3k−2l3k - 2l is an integer, by the definition a∣(3b−2c)a \mid (3b - 2c). ∎

Question 11medium

Compute 7100 mod 57^{100} \bmod 5.

Show answer
1

7≡2(mod5)7 \equiv 2 \pmod 5, so 7100≡21007^{100} \equiv 2^{100}. Look at small powers: 24=16≡1(mod5)2^4 = 16 \equiv 1 \pmod 5. 100=4⋅25100 = 4 \cdot 25, so 2100=(24)25≡125=12^{100} = (2^4)^{25} \equiv 1^{25} = 1.

Question 12easy

How many positive divisors does 360 have?

Show answer
24

360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5. A divisor has the form 2a3b5c2^a 3^b 5^c with a∈{0..3}a \in \{0..3\}, b∈{0..2}b \in \{0..2\}, c∈{0,1}c \in \{0,1\}. The number of choices is (3+1)(2+1)(1+1)=24(3+1)(2+1)(1+1) = 24.

Verification: All div/mod values, factorizations, primality tests, gcd/lcm (by Euclid and by brute force, two ways), Bezout coefficients (extended Euclid), divisor counts and 7^100 mod 5 (BigInt) were computed with node; a|b, a|c ⇒ a|(3b-2c) was checked by scanning a small range (verify/MAT2033/07.mjs).

Review card

a | b <=> b = a*c (c an integer) 3 | 12 true, 12 | 3 false Division algorithm: a = d*q + r, 0 <= r < d (q = a div d, r = a mod d) NEGATIVE: -23 div 6 = -4, -23 mod 6 = 1 (the remainder is never negative) a = b (mod m) <=> m | (a - b) <=> a mod m = b mod m For addition/multiplication mod m, reduce first, then compute Primality test: try the primes up to sqrt(n) Every n > 1 is a product of primes in exactly one way (Fundamental Theorem) gcd = smaller exponents, lcm = larger exponents, gcd*lcm = a*b Euclid: gcd(a,b) = gcd(b, a mod b); the last remainder before zero Bezout: gcd(a,b) = sa + tb (substitute Euclid backwards) gcd(a,b) = 1 -> relatively prime Proof: a|b, a|c -> b = ak, c = al (DIFFERENT letters) -> factor out a