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.
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:
- is a proposition, not a number. “3 divides 12” is true or false; is a number.
- The remainder is never negative: . For a negative number the quotient goes one lower.
- In mod m, shrink the numbers before adding and multiplying. You never have to compute the big product.
- For the GCD use Euclid: , until the remainder is 0.
- In a proof question, write the definition: if then . That line is half of the proof.
Divisibility
Let . a divides b () means there is an integer with . is called a factor (divisor) of , and a multiple of .
Examples: because . because is not an integer. because .
Basic properties ( integers):
| Property | Meaning |
|---|---|
| If and then | A common divisor divides the sum too |
| If then for every | A divisor also divides multiples |
| If and then | Transitivity |
| If and then | 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 ( and ). Using the same letter would mean ” and have the same coefficient”, and the proof would break.
The division algorithm
For every integer and positive integer there are unique and with:
(quotient), (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 and saying the remainder is is wrong: the remainder must satisfy . Take the quotient one lower () and the remainder comes out positive (). Java’s
%operator gives here, while the mathematical mod gives .
Congruence and modular arithmetic
(a is congruent to b modulo m) means . The same thing: and leave the same remainder when divided by .
Example: , because . A clock shows 2 at hour 38 and at hour 14.
Rules: if and then
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 question, first look for a small with . If you find it, divide by and compute the power of the remainder. If there is no such , use the fast exponentiation (repeated squaring) method from the cryptography week.
Prime numbers
If and its only positive divisors are and , then 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: .
Trial division: if is composite, it has a prime divisor less than or equal to . The reason: if and both were greater than , then . So to show that 97 is prime, it is enough to try only 2, 3, 5, 7 (). None divides it, so 97 is prime.
There are infinitely many primes (Euclid). Short proof idea: suppose there are finitely many, . The number is divisible by none of the (the remainder is always 1). But must have a prime divisor. We found a prime not in the list: contradiction.
GCD and LCM
: the largest integer that divides both. : the smallest positive integer that both divide. If , then and are relatively prime.
With factorization: for the GCD take the smaller exponent of each prime, for the LCM the larger exponent. And always
Example: , , .
The Euclidean algorithm
Factoring large numbers is hard. The Euclidean algorithm is based on this fact: if then . 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 with . 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 idea | The truth |
|---|---|
| and are the same thing | is a proposition (T/F), is a number |
| The remainder satisfies : | |
| is prime | 1 is neither prime nor composite |
| For primality, try up to | Primes up to are enough |
| For the GCD take the larger exponents | GCD takes the smaller exponent, LCM the larger |
| In Euclid the last divisor is the GCD | The last remainder before zero is the GCD |
| In a proof, , | Different letters: , |
| For , first the full product | First and , then multiply |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Find the quotient and remainder when 157 is divided by 12.
Show answer
The largest multiple of 12 that fits into 157 is . The remainder is . So , and the condition holds.
Find and .
Show answer
The remainder cannot be negative: . If you try then , which is negative, not allowed. Go one lower: , . So : div , mod .
Is ? Justify.
Show answer
Definition: . . 5 divides 25, so yes. Check: and , the remainders are the same.
Without multiplying out, compute .
Show answer
First reduce each number mod 11: , , . Then , . Check: .
Find the prime factorization of 1260.
Show answer
Start with the small primes: . . . Result .
Is 221 prime? Justify by trial division.
Show answer
, 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; . Composite: .
Use the Euclidean algorithm to find . Then find .
Show answer
, , . The last nonzero remainder: 21. For the LCM use : .
Let and . Find and .
Show answer
GCD: the smaller exponent of each prime: . LCM: the larger exponent of each prime: .
Find integers and such that .
Show answer
Euclid: , , , . The GCD is 18. Substitute back: . Check: .
Prove: if and , then .
Show answer
Write the definition: gives , gives ( integers, different letters!). Then . Since is an integer, by the definition . ∎
Compute .
Show answer
, so . Look at small powers: . , so .
How many positive divisors does 360 have?
Show answer
. A divisor has the form with , , . The number of choices is .
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).