Euclid’s Algorithm
Euclid’s Algorithm
Algorithm-2
Number Theory
Divisors
Let a, b and c be integers such that
a = b ·c .
Then b and c are said to divide (or are
factors) of a, while a is said to be a
multiple of b (as well as of c). The
pipe symbol “|” denotes “divides” so
the situation is summarized by:
b|a c|a.
Divisors Examples
Q: Which of the following is true?
1. 77 | 7
2. 7 | 77
3. 24 | 24
4. 0 | 24
5. 24 | 0
Division & Remainders
31.1
Common Divisors
Greatest Common Divisor
Relatively Prime
• Let a,b be integers, not both zero. The
greatest common divisor of a and b (or
gcd(a,b) ) is the biggest number d which
divides both a and b.
• a and b are said to be relatively prime if
gcd(a,b) = 1, so no prime common divisors.
Greatest Common Divisor
Relatively Prime
Q: Find the following gcd’s:
1. gcd(11,77)
2. gcd(33,77)
3. gcd(24,36)
4. gcd(24,25)
Greatest Common Divisor
Relatively Prime
A:
1. gcd(11,77) = 11
2. gcd(33,77) = 11
3. gcd(24,36) = 12
4. gcd(24,25) = 1. Therefore 24 and 25
are relatively prime.
NOTE: A prime number are relatively
prime to all other numbers which it
doesn’t divide.
Greatest Common Divisor
Relatively Prime
EG: More realistic. Find gcd(98,420).
Find prime decomposition of each number
and find all the common factors:
98 = 2·49 = 2·7·7
420 = 2·210 = 2·2·105 = 2·2·3·35
= 2·2·3·5·7
Underline common factors: 2·7·7,
2·2·3·5·7
Therefore, gcd(98,420) = 14
Greatest Common Divisor
The GCD and Linear Combinations
Theorem 31.2:
If a and b are integers not both 0, then
gcd(a, b) is the smallest positive element of the set {ax
+ by : x, y are integers} of linear combinations of a
and b.
The GCD and Linear Combinations (2)
Corollaries:
For any integers a and b, if d|a and d|b then
d|gcd(a, b).
For all integers a and b and any nonegative integer n,
gcd(an, bn) = n gcd(a, b).
For all positive integers n, a, and b, if n|ab and
gcd(a, n) = 1, then n|b.
Relatively Prime Integers
Two integers a and b are relatively prime if and only if
their only common divisor is 1
(i.e., gcd(a, b) = 1).
Theorem 31.6:
For any integers a, b, and p, if both
gcd(a, p) = 1 and gcd(b, p) = 1, then
gcd(ab, p) = 1.
Divisibility by Primes
Theorem 31.7:
For all primes p and all integers a, b, if p|ab, then p|a
or p|b (or both).
The Unique Factorization Theorem
Theorem 31.8 (Unique Factorization):
A composite number a can be written in exactly one
way as a product of the form
e1 e2 er
a p p ... p
1 2 r
where the pi are prime, p1 < p2 < …< pr, and the ei
are positive integers.
The GCD Recursion Theorem
Theorem 31.9 (GCD recursion theorem):
For any nonnegative integer a and any positive integer
b,
d = gcd(a, b) = ax + by
Extended Euclid’s Algorithm
Suppose a and b are given. Find x and y such that,
ax+by=gcd (a,b)
Let, d=gcd(a,b)
Then, ax+by=d [by Thm 31.2]
Again, gcd(a,b)=gcd(b, a mod b)
So, ax+by = d = bx´+(a mod b)y´
We know that, a = b.a/b+a mod b
So, a mod b = a – b.a/b
Thus, ax+by = bx´+(a – b.a/b).y´
= ay´+b(x´- a/b.y´)
Finally, x = y´
y =x´- a/b.y´