Lecture 4 3
Lecture 4 3
Lecture 4 3
Section 4.3
Anna-Simone Frank1
MNF130
Spring 2024
1
Slides created by Tom Michoel and modified by Erik Maartensson and
Anna-Simone Frank
Prime numbers and integer factorization
I An integer p > 1 is called prime if it is divisible only by 1 and itself
(its only factors are 1 and p). Otherwise it is called composite.
I The fundamental theorem of arithmetic states that every integer
n greater than 1 can be written uniquely as a prime or as the
product of two or more primes, where the prime factors are written
in nondecreasing order. In other words
14 = 2 · 7
30 = 2 · 3 · 5
100 = 2 · 2 · 5 · 5 = 22 · 52
2 https://en.wikipedia.org/wiki/RSA_numbers#RSA-250
4
Theorem √
√ a prime divisor ≤ n.
If n is a composite integer, then n has
(Hence, if n has no prime divisor ≤ n, then n is prime.)
Proof.
I If n is composite, it has factors 1 < a, b < n such that n = ab.
√ √ √ √
I If a > n and b > n, then ab > n n = n, which is a
contradiction.
√ √ √
I Hence a ≤ n or b ≤ n, and n has a divisor ≤ n.
I This divisor is either prime, or has a prime divisor less than itself. In
√
both cases n has a prime divisor ≤ n.
Example
Is 149 prime?
√
The only primes ≤ 149 = 12.0266 are 2, 3, 5, 7, 11. 149 is not divisible
by any of those and hence it must be prime.
5
Theorem
There are infinitely many primes.
7
Greatest common divisors
Definition
The greatest common divisor of two integers a and b not both zero,
written gcd(a, b), is the largest integer d such that d | a and d | b. If
gcd(a, b) = 1, then we say that a and b are relatively prime.
Example
gcd(36, 90) = gcd(22 · 32 , 2 · 32 · 5) = 2 · 32 = 18.
Theorem
Given two integers with integer factorizations a = p1e1 · · · pkek and
min(e ,f ) min(e ,f )
b = p1f1 · · · pkfk , we have gcd(a, b) = p1 1 1 · · · pk k k .
8
Least common multiple
Definition
The least common multiple of two non-zero integers a and b is the
smallest positive integer that is divisible by both a and b.
Example
lcm(36, 90) = lcm(22 · 32 , 2 · 32 · 5) = 22 · 32 · 5 = 180.
1 1 5 2 7
+ = + = .
36 90 180 180 180
Theorem
Given two integers with integer factorizations a = p1e1 · · · pkek and
max(e1 ,f1 ) max(ek ,fk )
b = p1f1 · · · pkfk , we have lcm(a, b) = p1 · · · pk .
9
Greatest common divisor and least common multiple
Theorem
Given two positive integers a and b we have ab = gcd(a, b) lcm(a, b).
Proof.
Let a = p1e1 · · · pkek and b = p1f1 · · · pkfk be the integer factorizations of the
two integers. Then
min(e1 ,f1 ) min(ek ,fk ) max(e1 ,f1 ) max(ek ,fk )
gcd(a, b) lcm(a, b) = p1 · · · pk · p1 · · · pk
min(e1 ,f1 )+max(e1 ,f1 ) min(ek ,fk )+max(ek ,fk )
= p1 · · · pk
= p1e1 +f1 · · · pkek +fk = p1e1 p1f1 · · · pkek pkfk
= p1e1 · · · pkek · p1f1 · · · pkfk = ab.
Example
gcd(36, 90) · lcm(36, 90) = 18 · 180 = 3240 = 36 · 90.
A result of this theorem is that if we can calculate gcd(a, b), then we get
lcm(a, b) = ab/ gcd(a, b) for free. 10
On efficient computation of the greatest common divisor
11
The Euclidean algorithm
Theorem
For a, b integers with a ≥ b, gcd(a, b) = gcd(b, a mod b).
Proof.
I Let r = a mod b, q = a div b, and
S = {d | d divides a and b}
T = {d | d divides b and r }
12
Repeated application of this theorem produces gcd(a, b):
procedure gcd(a, b : positive integers, a ≥ b)
x := a
y := b
while y 6= 0 do
r := x mod y
x := y
y := r
return x
13
Remember gcd(x, y ) = gcd(y , x mod y ) when x ≥ y
Example
Apply the Euclidean algorithm to find gcd(312, 42):
I
14
Bézout’s theorem
Theorem
If a and b are positive integers, then there exist integers s and t such that
gcd(a, b) = sa + tb.
Algorithm
I The Euclidean algorithm generates x1 = a, y1 = b, and for i = 1, . . . , n − 1
xi+1 = yi (2)
yi+1 = xi mod yi = xi − qi yi (3)
16
Exercise
Find gcd(252, 198) and write the result as a linear combination of 252
and 198. Let’s do it on the blackboard!
17