gcds and relatively prime numbers - center for lifelong...

26
GCDs and Relatively Prime Numbers CSCI 2824, Fall 2014

Upload: buinhu

Post on 05-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

GCDs and Relatively Prime Numbers!

CSCI 2824, Fall 2014! !!!

Challenge Problem 2 (Mastermind) due Fri. 9/26

Find a fourth guess whose scoring will allow you to determine the secret code (repetitions are allowed in the code):

O Y P R ! !xx!Y B O W ! !xo!B W Y Y ! !o!

Divisors

The set of divisors of a positive integer n are all those integers d whose absolute value is less than or equal to n and that have the property: d|n

So, for example, the divisors of 18 are: +/- 1, +/- 2, +/- 3, +/- 6, +/- 9, +/- 18

The divisors of 42 are: +/- 1, +/- 2, +/- 3, +/- 6, +/- 7, +/- 14, +/- 21, +/- 42

Primes and GCDs A prime number is a positive integer >= 2

whose only divisors are 1 and itself. For any two numbers m and n, gcd(m, n) is the

greatest common divisor shared by m and n. gcd(14, 35) = 7 gcd(6, 0) = 6 gcd(8, 9) = 1 gcd(7, 5) = 1

Relatively Prime Numbers

Two numbers are relatively prime if their gcd is 1:

8 and 9 are relatively prime. 12 and 15 are not relatively prime. Any two distinct primes (e.g., 5 and 11) are

relatively prime. 1 and any number are relatively prime.

A couple of questions

What numbers can be expressed in the form: 12 x + 27 y where x and y are integers?

A couple of questions

What numbers can be expressed in the form: 13 x + 27 y where x and y are integers?

A hypothesis

If two numbers m and n are relatively prime, then we can find integers x and y such that: m*x + n*y = 1 This implies in turn that any integer can be

expressed in combinations of m and n.

Bezout’s Theorem

If two numbers m and n are relatively prime, then we can find integers x and y such that: m*x + n*y = 1

Proof by contradiction: • Suppose that d is the smallest positive integer such

that m*x + n*y = d, and suppose that d > 1. We’ll find a positive integer smaller than d that can be expressed as a combination of m and n.

Okay, let’s get started… m * x + n * y = d where d > 1

• Now, d can’t divide both m and n, since then they wouldn’t be relatively prime.

• So let’s say d does not divide m. Then we can write: m = d*q + r where 0 < r < d

• So now we can write: r = m – d*q = m – (m*x + n*y)*q = m(1 – q*x) - n * (q*y)

Okay, let’s get started… m * x + n * y = d where d > 1

• So now we can write: r = m – d*q = m – (m*x + n*y)*q = m(1 – q*x) - n * (q*y)

• But hold it! We just showed that r is a combination of m and n. And we know that r is less than d! So our original premise can’t be right.

Bezout’s Theorem, Again

If two numbers m and n are relatively prime, then we can find x and y such that: m*x + n*y = 1 Try it for any two relatively prime numbers: 7 and 26 9 and 11

A Corollary to Bezout’s Theorem

If gcd(m, n) = d where d > 1, then we can find an integer combination of m and n that sums to d:

m * x + n * y = d Why? Note that gcd(m/d, n/d) = 1, and then

we can find a combination such that: (m/d) * x + (n/d) * y = 1

How to find the gcd? ���(Euclid again!)

We want to find gcd(a, b) where a >= b. First, we note that if a = b, the answer is a. Also, if b = 0, the answer is a. Otherwise, we can find gcd(a, b) by noting

that the greatest common divisor of a and b is also the greatest common divisor of b and the remainder r when a is divided by b.

Why does this work?

Suppose d|a and d|b. (In prose, d goes into both a and b.) And suppose a = b*q + r. Then d|b and d|(a – b*q).

Two examples: gcd (99, 18) = gcd(18, 9) = gcd(9, 0) = 9 gcd(70, 49) = gcd(49, 21) = gcd(21, 7) = gcd(7, 0) = 7

Scheme version of the algorithm for finding the GCD

!(define (gcd a b)! (if !(= b 0) !! ! !a!! ! !(gcd b (remainder a b))))!

!

Not only is this idea super-fast, but it’s also useful:

(18, 7) = (7, 4) = (4, 3) = (3, 1) = (1, 0) = 1 18 = (2 * 7) + 4 7 = (1* 4) + 3 4 = (1 * 3) + 1 Now, unwind those statements: 1 = 4 – (1 * 3) = 4 - (1 * (7 – (1 * 4))) = 4 – (7 – 4) = (2 * 4) - 7 = ((2 * (18 – (2 * 7))) – 7 = (2 * 18) – (5 * 7)

The Structure of Divisors

Let’s try to “break down” a number’s divisors as far as we can:

84 = 2 * 42 = 2 * 2 * 21 = 2 * 2 * 3 * 7

We can’t go any further than this, since we have only prime factors.

The Fundamental Theorem of Arithmetic

Every number n has a unique factorization into primes.

A Lemma

Suppose d|(ab) and gcd(d,a) = 1 In prose: d goes into the product of a and b, but d and a are relatively prime.

Then d|b

Let’s prove the lemma. Since d|ab We can write: ab = dq

And since gcd(d, a) = 1 We can find x and y such that: dx + ay = 1

So: dbx + aby = b dbx + dqy = b d(bx + qy) = b

Lemma 2 (a corollary to Lemma 1) We now know that if d|(ab) and gcd(d,a) = 1,

then d|b. Suppose d is a prime number (let’s call it p for prime). Then if p|(ab), we know that either p|a or p|b.

Proof: Suppose p doesn’t go into a. Then by the definition of greatest common divisor (and the definition of a “prime number”), gcd(p,a) = 1. Which means, from our previous lemma, that p|b.

More generally, we have the following: p|q1q2q3…qn

means that p must go into at least one factor in the product.

Now we’re ready to prove the Fundamental Theorem of

Arithmetic First we show that every number can be factored into primes.

(We’ll leave uniqueness for the next step.) Step 1. We know that 2 can be factored into primes (i.e., 2

itself). Step 2. Suppose that all numbers from 2…n can be factored

into primes. Consider n+1. If it’s prime, we’re done. Step 3. Suppose it isn’t prime. Then it must have some factor

q. So qx = (n+1). But we know that both q and x are smaller than n+1, so they can be factored into primes.

A note: this technique (step 2) is called strong induction.

The factorization is unique (proof by contradiction).

Suppose the factorization weren’t unique. Let m be the smallest number with two distinct prime factorizations, with factors ordered low to high: p1p2p3..pj = m = q1q2q3…qk

If p is equal to q, then we can divide both factorizations by

that number, and we have a smaller value with two distinct factorizations (contrary to our assumption). So choose the smaller of p, q: let’s say it’s p. Then by our earlier lemma 2, p must go into one of the q factors, contrary to our assumption that they’re all prime! Either way, we have a contradiction.