Top Banner
Prime Numbers, GCD, Euclidean Algorithm and LCM
17

Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Jul 16, 2018

Download

Documents

duongnhu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Prime Numbers, GCD, Euclidean Algorithm and LCM

Page 2: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Definition and Examples

A prime number is a positive integer that has exactly two positive divisors. Equivalently, it is an integer greater than 1 that has only 1 and itself as positive divisors. (The number 1 is not prime).

A positive integer that is greater than 1 and is not prime is called composite.

The first 10 prime numbers are: 2,3,5,7,11,13,17,19,23,29

Observe that 2 is the only even prime. All other positive even numbers are multiples of 2 and therefore composite.

Page 3: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Factors and Cofactors of a Composite IntegerBy definition, if ๐‘› is composite integer, then it must have a positive factor ๐‘˜ other than 1 or ๐‘›. This factor must then satisfy 1 < ๐‘˜ < ๐‘›, because a divisor of an integer cannot be greater than the integer itself. We then have the following theorem:

If ๐‘› is composite integer, ๐‘˜ is one of its factors such that 1 < ๐‘˜ < ๐‘›, and ๐‘š the cofactor ๐‘š with ๐‘› =๐‘˜๐‘š, then

1. ๐‘š satisfies the same inequality as ๐‘˜: 1 < ๐‘š < ๐‘›.

2. if ๐‘˜ โ‰ค ๐‘›, then ๐‘š โ‰ฅ ๐‘› .

Proof: By substituting ๐‘˜ =๐‘›

๐‘šinto 1 < ๐‘˜ < ๐‘›, we get 1 <

๐‘›

๐‘š< ๐‘›. By taking reciprocals and

remembering that less than relationships between positive numbers correspond to greater

than relationships between the reciprocals, we get 1

๐‘›<

๐‘š

๐‘›< 1. We multiply by the positive number

๐‘› to get 1 < ๐‘š < ๐‘›.

Now suppose ๐‘˜ โ‰ค ๐‘›. Then if ๐‘š < ๐‘› was true, we could multiply the inequalities to get ๐‘› =๐‘˜๐‘š < ๐‘› โˆ™ ๐‘› = ๐‘›. ๐‘› < ๐‘› is a contradiction, therefore ๐‘š < ๐‘› must have been false and ๐‘š โ‰ฅ ๐‘›must be true.

Page 4: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

ConsequencesWhat the theorem on the previous page tells us is that the nontrivial (other than 1 and ๐‘›) factor-cofactor pairs of a composite integer ๐‘› lie strictly between 1 and ๐‘›, and each factorโ€™s cofactor lies on the โ€œopposite sideโ€ of the square root of ๐‘›, except when the square root itself is a factor, in which case it is equal to its own cofactor.

Example. The positive factors of 100 are:

1 2 4 5 10 20 25 50 100100

Page 5: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Testing for PrimalityWe learned that factors of a composite number ๐‘› come in pairs that lie on opposite sides of the square root, i.e. for each factor that is less than or equal to ๐‘›, there is one that is greater than or equal to ๐‘› and vice versa.

Therefore, to prove that an integer ๐‘› > 1 is a prime, we only need to test divisibility of ๐‘› by potential factors ๐‘˜ with 1 < ๐‘˜ โ‰ค ๐‘›. If ๐‘› is divisible by none of those ๐‘˜, then ๐‘› is prime.

If ๐‘› is divisible by a composite number ๐‘˜, then ๐‘› is also divisible by any prime factor ๐‘ of ๐‘˜. For such a ๐‘ , we have 1 < ๐‘ < ๐‘˜ โ‰ค ๐‘›. Thus, we can restrict our divisibility testing to prime numbers ๐‘, 1 <๐‘ โ‰ค ๐‘›.

Example: we will prove that 47 is prime. Since 36 < 47 < 49, 6 < 47 < 7, so we only have to test divide 47 by the prime numbers ๐‘, 1 < ๐‘ โ‰ค 6. There are only three such prime numbers: 2,3,5.

47 is not divisible by 2 because it is even. 47 is not divisible by 3 because the sum of the digits of 47, 11, is not divisible by 3. 47 is not divisible by 5 because its last digit is neither 0 nor 5. Therefore, 47 is prime.

Observe that if we do not have a list of prime numbers available, we can also establish primality of ๐‘› by test dividing ๐‘› by all integers ๐‘˜ with 1 < ๐‘˜ โ‰ค ๐‘›. This slightly cruder algorithm takes a little longer โ€“ it requires roughly ๐‘› test divisions โ€“ but it has the advantage that it does not require a list of primes in the range 2 to ๐‘› as input.

Page 6: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

A crude primality test by trial division

This is the algorithm for primality testing discussed on the last page in pseudocode:

primality_test(int n) {

int k = 2;

int upper_limit = floor(sqrt(n));

while (k <= upper_limit AND n mod k > 0)

k++;

if (k==upper_limit)

return(n is prime);

else

return(n is not prime);

}

Page 7: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Prime Factorization

According to what we just learned, a composite number ๐‘› can be factored into a product of two positive integers that are both smaller than ๐‘›. Then each of those numbers is either prime itself, or composite, in which case it can be factored again into a product of smaller integers.

This factorization process must end after finitely many steps with a prime factorization โ€“ a representation of ๐‘› as a product of prime numbers. It is a theorem, called the fundamental theorem of arithmetic, that each composite number ๐‘› has a prime factorization, and that this prime factorization is unique except for rearrangement, i.e. two different prime factorizations of ๐‘›must contain the same factors, only possibly in a different order.

An equivalent statement of the fundamental theorem of arithmetic is that every integer greater than 1 is either prime, or a product of primes, where the product is unique except for rearrangement.

Since the prime factorization may contain the same prime factor more than once, the general form of a prime factorization is ๐‘1

๐‘˜1๐‘2๐‘˜2 โ€ฆ๐‘๐‘›

๐‘˜๐‘›where ๐‘1, โ€ฆ , ๐‘๐‘› are prime numbers and ๐‘˜1, โ€ฆ , ๐‘˜๐‘› are positive integers.

Page 8: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

Example of Prime Factorization

The recursive factorization process we described on the previous slide can be visualized with a tree diagram like the one on the right.

120 = 2 โˆ™ 2 โˆ™ 2 โˆ™ 3 โˆ™ 5 = 23 โˆ™ 3 โˆ™ 5

We could have used a different sequence of factorizations, such as 120 = 2 โˆ™ 60 = 2 โˆ™ 2 โˆ™ 30 = 2 โˆ™ 2 โˆ™ 2 โˆ™15 = 2 โˆ™ 2 โˆ™ 2 โˆ™ 3 โˆ™ 5 and arrived at the same prime factorization.

Page 9: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

There are Infinitely Many PrimesIt has been known since ancient times that there are infinitely many primes. We will discuss Euclidโ€™s famous proof by contradiction here.

We will assume, to get a contradiction, that there are only finitely many primes. In that case, we can list all of them: ๐‘1, ๐‘2, โ€ฆ , ๐‘๐‘›. We now consider the number

๐‘ƒ = ๐‘1๐‘2โ€ฆ๐‘๐‘› + 1.

Each ๐‘๐‘˜ is greater than 1, so ๐‘ƒ > ๐‘๐‘˜ for each ๐‘˜ = 1. . ๐‘›. Since the ๐‘๐‘˜ are all the prime numbers, and ๐‘ƒ is not equal to any of them, ๐‘ƒ is not prime. Thus ๐‘ƒ is composite. By the fundamental theorem of arithmetic, ๐‘ƒ must then be a product of primes. But ๐‘ƒ has a remainder of 1 with respect to division by each of the prime numbers ๐‘๐‘˜ , hence ๐‘ƒ is not divisible by any prime number. That is a contradiction.

Therefore, the assumption that there are only finitely many primes was false. This completes the proof that there are infinitely many primes.

Page 10: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Distribution of Primes and the Prime Number TheoremThere is no known formula that outputs prime numbers on demand. In the sequence of integers, prime numbers seem to appear โ€œrandomlyโ€. Yet at the same time, the total number of primes less than or equal an integer ๐‘› approximately follows a law that is given by the prime number theorem.

The prime number theorem, non-rigorously stated, says that the number of primes not exceeding ๐‘› is approximately ๐‘›/ ln ๐‘›.

For example, there are 25 primes that are less than or equal to ๐‘› =100, and

100

ln 100= 21.7.

Page 11: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Greatest Common Divisor (I)The greatest common divisor (gcd) of two positive integers ๐‘Ž, ๐‘, written gcd(๐‘Ž, ๐‘), is the largest integer that divides both ๐‘Ž and ๐‘. Examples:

gcd 12,18 = 6gcd 9,8 = 1

When gcd ๐‘Ž, ๐‘ = 1, we call ๐‘Ž and ๐‘ relatively prime (to each other).

We can find gcd(๐‘Ž, ๐‘) from the prime factorizations of ๐‘Ž and ๐‘. We will explain that by example first. Take

๐‘Ž = 24 = 2 โˆ™ 2 โˆ™ 2 โˆ™ 3๐‘ = 18 = 2 โˆ™ 3 โˆ™ 3

The gcd of ๐‘Ž and ๐‘ can only have as many of each prime factor as the two numbers have in common. Since ๐‘Ž has three factors 2, but ๐‘ only one, gcd(๐‘Ž, ๐‘) has only one factor 2. Since ๐‘Ž has only one factor 3, and ๐‘ has two, gcd(๐‘Ž, ๐‘) has only one factor 3. Therefore, gcd ๐‘Ž, ๐‘ = 2 โˆ™ 3 = 6.

When ๐‘Ž divides ๐‘, then gcd ๐‘Ž, ๐‘ = ๐‘Ž .

Page 12: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Greatest Common Divisor (II)

Let us generalize what we just learned. Suppose

๐‘Ž = ๐‘1๐‘˜1 โ€ฆ๐‘๐‘›

๐‘˜๐‘›

๐‘ = ๐‘1๐‘š1 โ€ฆ๐‘๐‘›

๐‘š๐‘›

where ๐‘1, โ€ฆ , ๐‘๐‘› are prime numbers and the exponents ๐‘˜1, โ€ฆ , ๐‘˜๐‘› , ๐‘š1, โ€ฆ๐‘š๐‘› are non-negative integers. We allow for some of the exponents to be zero to handle the case of prime factors that are exclusive to ๐‘Ž or ๐‘.

Then gcd ๐‘Ž, ๐‘ = ๐‘1min{๐‘˜1,๐‘š1}โ€ฆ๐‘๐‘›

min{๐‘˜๐‘›,๐‘š๐‘›} .

The notation min{ ๐‘ฅ, ๐‘ฆ} stands for the smaller of the two numbers ๐‘ฅ, ๐‘ฆ.

Page 13: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Least Common MultipleThe least common multiple (lcm) of two positive integers ๐‘Ž, ๐‘, written lcm(๐‘Ž, ๐‘), is the smallest integer that is a multiple of both ๐‘Žand ๐‘. Examples:

lcm 6,4 = 12lcm 2,3 = 6

We can find lcm(๐‘Ž, ๐‘) from the prime factorizations of ๐‘Ž and ๐‘. Let us take an example first:

๐‘Ž = 12 = 2 โˆ™ 2 โˆ™ 3 = 22 โˆ™ 3๐‘ = 18 = 2 โˆ™ 3 โˆ™ 3 = 2 โˆ™ 32

Then any common multiple of ๐‘Ž and ๐‘ must have at least two powers of 2, and at least two powers of 3. Since 2232 is a common multiple of ๐‘Ž and ๐‘, it

must be the least common one. Therefore, lcm ๐‘Ž, ๐‘ = 2232 = 36. This illustrates the rule that we find the lcm of two positive integers from their prime factorization by selecting the maximum of the two exponents for each prime factor.

Let us state this rule more formally. Suppose again like on the previous slide that the prime factorizations of ๐‘Ž and ๐‘ are

๐‘Ž = ๐‘1๐‘˜1 โ€ฆ๐‘๐‘›

๐‘˜๐‘›

๐‘ = ๐‘1๐‘š1 โ€ฆ๐‘๐‘›

๐‘š๐‘›

Then any multiple of ๐‘Ž and ๐‘ must have at least as much of each prime factor as ๐‘Ž and ๐‘ individually. Therefore,

lcm(๐‘Ž, ๐‘) = ๐‘1m๐‘Ž๐‘ฅ{๐‘˜1,๐‘š1}โ€ฆ๐‘๐‘›

m๐‘Ž๐‘ฅ{๐‘˜๐‘›,๐‘š๐‘›}

The notation max{ ๐‘ฅ, ๐‘ฆ} stands for the smaller of the two numbers ๐‘ฅ, ๐‘ฆ.

Page 14: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Relationship between gcd and lcmWe have shown that if

๐‘Ž = ๐‘1๐‘˜1 โ€ฆ๐‘๐‘›

๐‘˜๐‘›

๐‘ = ๐‘1๐‘š1 โ€ฆ๐‘๐‘›

๐‘š๐‘›

then

gcd ๐‘Ž, ๐‘ = ๐‘1min{๐‘˜1,๐‘š1}โ€ฆ๐‘๐‘›

min{๐‘˜๐‘›,๐‘š๐‘›}

lcm(๐‘Ž, ๐‘) = ๐‘1m๐‘Ž๐‘ฅ{๐‘˜1,๐‘š1}โ€ฆ๐‘๐‘›

m๐‘Ž๐‘ฅ{๐‘˜๐‘›,๐‘š๐‘›}

When we multiply the two equations, we get

gcd ๐‘Ž, ๐‘ lcm ๐‘Ž, ๐‘ = ๐‘1min{๐‘˜1,๐‘š1}+max{๐‘˜1,๐‘š1}โ€ฆ๐‘๐‘›

min{๐‘˜๐‘›,๐‘š๐‘›}+max{๐‘˜๐‘›,๐‘š๐‘›}

For any two numbers ๐‘Ž and ๐‘, min a, b + max a, b = a + b.

Therefore, gcd ๐‘Ž, ๐‘ lcm ๐‘Ž, ๐‘ = ๐‘1๐‘˜1+๐‘š1 โ€ฆ๐‘๐‘›

๐‘˜๐‘›+๐‘š๐‘› = ๐‘Ž๐‘.

We have just proved that gcd ๐‘Ž, ๐‘ lcm ๐‘Ž, ๐‘ = ๐‘Ž๐‘.

One consequence of this equation is that if ๐‘Ž and ๐‘ are relatively prime, lcm ๐‘Ž, ๐‘ = ๐‘Ž๐‘.

Page 15: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Euclidean Algorithm (I)For large numbers ๐‘Ž, ๐‘, it may be computationally prohibitive to find the prime factorizations. Fortunately, there is an efficient way to compute gcd ๐‘Ž, ๐‘ that does not rely on the prime factorizations, called the Euclidean Algorithm.

To understand the algorithm, we will have to recall a fact from a previous presentation: If ๐‘Ž, ๐‘ and ๐‘ are integers, then, if ๐‘ and ๐‘ are both multiples of ๐‘Ž, so is ๐‘ + ๐‘, and if ๐‘ is a multiple of ๐‘Ž, so is any multiple of ๐‘.

The Euclidean Algorithm is based on the following theorem: If ๐‘Ž, ๐‘, ๐‘ž, ๐‘Ÿ are integers and ๐‘Ž = ๐‘๐‘ž + ๐‘Ÿ, then gcd ๐‘Ž, ๐‘ = gcd ๐‘, ๐‘Ÿ .

Proof: suppose ๐‘ is a common divisor of ๐‘ and ๐‘Ÿ. Then ๐‘ also divides ๐‘๐‘ž, and therefore divides ๐‘Ž = ๐‘๐‘ž + ๐‘Ÿ. Thus ๐‘ is a common divisor of ๐‘Ž and ๐‘.

Now suppose ๐‘ is a common divisor of ๐‘Ž and ๐‘. Then ๐‘ also divides โˆ’๐‘๐‘ž, and therefore also divides ๐‘Ÿ = ๐‘Ž โˆ’ ๐‘๐‘ž. Thus ๐‘ is a common divisor of ๐‘ and ๐‘Ÿ.

We have proved that the set of common divisors of ๐‘Ž and ๐‘ and the set of common divisors of ๐‘ and ๐‘Ÿ are equal. Therefore, the greatest common divisor of ๐‘Ž and ๐‘ and the greatest common divisor of ๐‘ and ๐‘Ÿ must be equal as well.

Page 16: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Euclidean Algorithm (II)The Euclidean Algorithm to find gcd ๐‘Ž, ๐‘ consists of repeated application of the division algorithm ๐‘Ž = ๐‘๐‘ž + ๐‘Ÿ, where after each step, ๐‘ becomes the new ๐‘Ž, and ๐‘Ÿ becomes the new ๐‘.

Example: the Euclidean Algorithm with ๐‘Ž = 102 and ๐‘ = 30.

102 = 3 โˆ™ 30 + 12

After one step, we know that gcd 102,30 = gcd(30,12) .

30 = 2 โˆ™ 12 + 6

Now we know that gcd 102,30 = gcd 30,12 = gcd(12,6) .

At this point, we can stop because 12 is a multiple of 6, so we know gcd(12,6) = 6. Since 12 is a multiple of 6, the remainder in the next division step is zero:

12 = 2 โˆ™ 6 + 0

This teaches us the halting condition of the Euclidean Algorithm: when we reach a remainder of zero, the previous (non-zero) remainder is the gcd.

Page 17: Prime Numbers, GCD, Euclidean Algorithm and LCMboerner/mat243/4.3 Primes...There are only three such prime numbers: 2,3,5. 47 is not divisible by 2 because it is even. 47 is not divisible

The Euclidean Algorithm (III)Let us now give a general description of the Euclidean Algorithm and explain why it always terminates after finitely many steps.

Let us set ๐‘Ž = ๐‘Ÿ0 and ๐‘ = ๐‘Ÿ1. The n-th step (for ๐‘› โ‰ฅ 1) of the algorithm consists of application of the division algorithm

๐‘Ÿ๐‘›โˆ’1 = ๐‘Ÿ๐‘›๐‘ž๐‘› + ๐‘Ÿ๐‘›+1

Each remainder ๐‘Ÿ๐‘›+1 satisfies 0 โ‰ค ๐‘Ÿ๐‘›+1 < ๐‘Ÿ๐‘› because ๐‘Ÿ๐‘› acts as the divisor of the division algorithm. Since the remainders are also integers, ๐‘Ÿ๐‘›+1 < ๐‘Ÿ๐‘› implies that the remainders decrease by at least 1 with each step: ๐‘Ÿ๐‘›+1 โ‰ค ๐‘Ÿ๐‘› โˆ’ 1. On the other hand, all remainders are non-negative. Hence, in finitely many steps, one remainder must reach the value zero: ๐‘Ÿ๐‘›+1 = 0. For that ๐‘›, ๐‘Ÿ๐‘›โˆ’1 =๐‘Ÿ๐‘›๐‘ž๐‘›. Thus

gcd ๐‘Ž, ๐‘ = gcd ๐‘Ÿ๐‘›โˆ’1, ๐‘Ÿ๐‘› = ๐‘Ÿ๐‘› .

That confirms that the last non-zero remainder is the gcd.

๐‘Ÿ0 = ๐‘Ÿ1๐‘ž1 + ๐‘Ÿ2๐‘Ÿ1 = ๐‘Ÿ2๐‘ž2 + ๐‘Ÿ3๐‘Ÿ2 = ๐‘Ÿ3๐‘ž3 + ๐‘Ÿ4๐‘Ÿ3 = ๐‘Ÿ4๐‘ž4 + ๐‘Ÿ5

โ‹ฎ