Chapter 2 Discrete Structures Notes
| Links to Key Topic on Page | ||
| Lemma 1 | lcd | |
| Definition of divide | gcd | |
Linear Congruencies |
Chinese Remainder Theorem | Existence of inverses. |
| Linear Combination | Euclidean Algorithm | |
| Modular Arithmetic | Working with Large Numbers | Representation of Large Numbers |
| Using the better version of the Euclidean Algorithm | Working with Matrices |
Finding the Inverse in Exercise 7
|
| C program
to find inverse and gcd |
Sections 6.1, 6.3 and 7.3 have some uses of matrices. You may want to look at these sections as they also provide a review of some of the material in chapter. | Section 6.4 contains the definition
of mod's as well. R={(a,b) | a~b mod m} |
| This page is set up so that if you need to look up something, there is a link to it nearby. You should be able to look up the key points by clicking on links. A lot of material in this chapter refers back and forward. Proofs are given and numerical examples are provided. | ||
I would strongly urge
you to take a look at the links on the author's WEB
site for chapter 2. There are a lot of useful notes, programs and background material
at various different levels. This is a particularly good site for those that are
interested in getting some more advanced information.
Much of the material in these sections takes careful reading and work.
Also, if you play around enough you may discover a few theorems on your own! The material
in these sections is useful for encoding is such areas as encryption, compression,
number representation etc. This is an introduction to Number Theory. Number Theory
with integers and their properties. Some of these are extremely simple while other are so
complex that no solution exists. Others are good for impressing friends at parties. The
Chinese Remainder Theorem is a good example of this type of puzzle.
Note: I am going to skip over doing a number of the tables in class in
full and refer you to the WEB for the complete problem. That way I can spend more time
explaining than writing everything out in class.
| (all letters are integers) | Comments | ||
| Division | Definition | a|b implies b=ac this is read "a divides b" |
if integer a goes into integer b evenly, it also goes into bc. 4/2, 8/2,16/2 |
| Theorem 1a
|
if a|b and a|c then a|(b+c) | 7 goes into 14 and 7 goes into 21. Therefore 7 goes into
14+21. Outline of Proof: a|b implies b=as [from definition] Example: If 6|12 and 6|18 then 6|(12+18) |
|
| 1b | if a|b then a|bc | if a|b, then by the definition a=bc Example: If 6|12 then 6 | 12*7 |
|
| 1c | if a|b and b|c, a|c
|
Outline of Proof: a|b implies b=as if 6/3 goes evenly and 12/6 goes evenly |
|
| Ex. 2.3 5
|
if a|b and b|a a=b or a=-b (other exercises follow the same
pattern.) |
Outline of Proof: a|b implies a=bs [from the definition] Example: 6 |6 and 6 |-6
|
|
| Prime | Definition | A prime has only itself and 1 as factors | This is often a very helpful restriction. See prime numbers below for more information. Note: The concept of prime is not restricted to numbers. Polynomials, for example, can be factored into prime polynomials |
| Definition | if gcd(a,b)=1, a and b are relatively prime | If a and b have only 1 as a common factor, then they are
relatively prime. This is often helpful as a restriction. Example gcd(15,7)=1 15 and 7 are relatively prime since no numbers divides both. |
|
| Definition | if for a(1),a(2)a(3)..a(n) gcd(a(i),a(j))=1 fot 1<=i<j<=n
|
Every pair is relatively prime. This is useful in making sure
that each pair has a inverse. Example: 13,11,10 are relatively prime since gcd(13,10)=1, gcd(13,11)=1 and gcd(10,11)=1 |
|
| Division Algorithm | Definition | a=dq+r | d is the divisor, r is the remainder, q is the quotient. 6/5 = 1*5 + 1 |
| GCD | Definition gcd(a,b) |
The largest d such that d|a and d|b | d is the largest number that goes into a and b. Another way to look at this is to factor each number into a set of prime
factors. Those factors from each of the sets make up the gcd. Example: gcd(15,5)=5 since 5 |15 and 5 |5 and 5 is the largest number that does so. |
| Definition | if gcd(a,b)=1, a and b are relatively prime | If a and b have only 1 as a common factor, then
they are relatively prime. This is often helpful as a restriction. Example gcd(15,7)=1 15 and 7 are relatively prime since no numbers divides both. |
|
| Theorem for Euclidean algorithm. | if a=bq+r, gcd(a,b)=gcd(b,r) | Let d|a and d|b then d | bq and d|(a-bq) and d |r see Theorem 1 This means that d divides a, b and r gcd(a,b)=d=gcd(b,r) Example: gcd(252,198)=gcd(198,54) since Therefore since gcd(36,18)=18, Hence gcd(252,198)=18. This is the outline of the Euclidean algorithm. |
|
| Linear combinations | gcd(a,b)=sa + tb | See Linear
Combination for mode detailed information on how to calculate t and s. This is a
generalization and example of the Euclidean Algorithm Let
d=gcd(a,b), then |
|
| Lemma 1 | if gcd(a,b)=1 (relatively prime) and a|bc, a|c | Outline of Proof:: sa+tb=1 [def of gcd; see relatively prime] What this says is that if a and b are relatively prime and a goes in b*c, then a goes into c. If a and b were not relatively prime, there would not be a unique division in step 4.
Example: gcd(13,6)=1 and 6 | 13*2, then 6|2. |
|
| Theorem 2 Law of Cancellation |
If ac~bc mod m and
gcd(c,m)=1, then a~b mod b If a,m are relatively prime then a has an inverse |
Outline of Proof: ac~bc mod m means that Example (5 is prime) If a is relatively prime to m, a has an inverse; if m is prime, m is relatively prime to all a. Example (4 and 9 are relatively prime) |
|
| LCD/GCD | Theorem | ab= gcd(a,b)*lcm(a,b) | When all the factors are written out, they form two sets, GCD and LCM. |
| LCM | Definition lcm(a,b) |
The smallest d such that a|d and b|d. | d is the smallest number than can be divided by a and b. Another way to look at this is to factor each number into a set of prime factors. Those factors that are the union of the sets make up the lcd.Contrast this with gcd. |
| Modular Arithmetic | Definition | a mod m =r such that a=qm+r | When a is divided by m q times, the remainder is r. This definition is used in many proofs in the chapter.
Example 4 mod 5 means 5*0+4 or 5 goes into 4 zero times with 4 left over. NOTE: The trig functions are a mod like function. The sine 0 = sine 360. They, like the mod are periodic functions. In this case, q= the number of periods and r is the angle remaining. To determine the sun's position in the sky, it is important to know the angle with the horizon, not the number of revolutions. This is a use of a mod function. |
| Definition | Congruence(~): a ~ b mod m means that m | (a-b) or a = b + qm
Also, it can be seen as a and b have the same remainder when divided by m. |
~ is the same as the 3 equal signs (I don't have
the symbol on my key board.) Outline of Poof: Given a and b have the same remainders when divided by m:
Example 4 mod 5 means 5*0+4 or 5 goes into 4 zero times with 4 left over. 6 mod 3 means 3*2+0 or 3 goes into 6 with zero left over |
|
| Inverses | if a*b~ 1 mod m,
and m is prime, then for any a there is an inverse bf b. If a,m are relatively prime (gcd(a,m)=1) then a has an inverse b. |
For example 3*2~1 mod 5 therefore 3 and 2 are
inverses of each other.see table of inverses mod 5 and mod
4. 2 does not have an inverse mod for since NOTE: 3*b~1 mod 4 where b=3 Since 3 is relatively prime to 4, 3 has an inverse, in this case 1. While the system does not have an inverse, inverses do exist for those values of a that are relatively prime to m. This is why the restriction a,m is relatively prime or equivalently, gcd(a,m)=1 is added to the restrictions if we need to take the inverse of a. |
|
| Addition by a constant | If a mod m~b mod m then a+c mod m ~ b+c mod m If a mod m =r, then |
This allows us to do math using just the
remainders.
5 ~ 1 mod 4 =1 This means that we can add across a congruency and not change the relationship. If we add an hour to 1am and an hour to 1pm, we get 2am ~ 2pm. We haven't changed the relationship, but each clock is advanced one hour. |
|
| Multiplication by a constant | If a mod m~b mod m then ac mod m ~ bc mod m If a mod m =r, then Multiplication always works; division only works if m is prime. |
Multiplication by a constant. Example 5 ~ 1 mod 4 =1 Note unless m is prime, ac mod m~bc mod m does not equal a mod m ~ b mod m. Example: 14 ~ 8 mod 6 |
|
| Addition | if a~b mod m and c~d mod m, a+c (mod m) ~ b +d (mod m) |
Outline of proof: b=a+sm
d=c+tm 6~1 mod 5, 7 ~ 2 mod 5 |
|
| Multiplication | if a~b mod m and c~d mod m, a*c (mod m) ~ b *d (mod m) |
Outline of proof: b=a+sm
d=c+tm 6~1 mod 5, 7 ~ 2 mod 5 |
|
| Mod Applications | Chinese Remainder Theorem | If m1, m2
... mn are pairwise relatively prime, the
system x~ a1 mod m1 has a unique solution mod m where m= The solution is given by x= Since gcd(mk, Mk)=1 x ~ak Mk yk |
Example: Suppose that we have x ~ 2 mod 3 3,5,7 are pairwise relatively prime. Second, determine yk, the inverse of Mk Third, we get the a's from the original problem 23 ~ 2 mod 3 Therefore 23 is the smallest x that solves all three equations.There are other
solutions but they are congruent to x mod 105 (23+105, for example) |
| Representation of Large Numbers | We can represent a as (a mod m1, a mod m2 ..a mod mn) |
Example let a=12 and let m1 =3 and m2 =4 Then for 0,5, and 12 0=(0,0) 0 mod 3=0, 0 mod 4=0 5+ 7 = (2,1) + (1,3) = (3, 4) = (0,0) 5*7 =(2,1) * (1,3) = (2,3) = 11 mod 12 We are able to work on large integers by working with their remainders. |
|
| Working with Large Numbers | Every nonnegative integer less
than 89,403,930 can be represented by the remainders of the factors 99, 98,97 and 95. |
We can represent any number
under 89,403,930 as a mod 99, a mod 98, a mod 97, a mod 95 and use the procedure above to
add and multiply. We we get our answer, we can use the Chinese Remainder Theorem to solve for x. If we
can represent a large number (such as the one above), but
|
Euclidean Algorithm (example)
r(i) =
r(i+1)*q(i) + r(i+2) |
||||
| a= | b | q | r | |
| r(i) | r(i+1) | q | r(i+2) | |
| 662 | = | 414 | 1 | 248 |
| 414 | = | 248 | 1 | 166 |
| 248 | = | 166 | 1 | 82 |
| 166 | = | 82 | 2 | 2 |
| 82 | = | 41 | 2 | 0 [0 remainder] |
gcd(414,662)=gcd(414,248)=gcd(248,166)=gcd(166,82)=2 |
x=a
y=b
while y <>0
r=x mod y
x=y
y=r
end
x is the gcd(a,b)
Linear Combinations: (start with Euclidean algorithm)
| a= | b | q | r |
| 252 | 198 | 1 | 54 |
| 198 | 54 | 3 | 36 |
| 54 | 36 | 1 | 18 |
| 36 | 18 | 2 | 0 |
Keep substituting until 18 is written in terms of 252 and 198.
18 = 54-1*36
36 = 198-3*54
18 = 54-1*36 = 54-198-3*54 = 4*54-1*198
54=252-1*198
18=4*54-1*198 = 4*252-1*198 - 1*198 = 4*252-5*198
18=4*252-5*198
Better Algorithm |
||||||||
| Algorithm to compute linear combination
factors at the same time you compute gcd. This streamlines the back calculations that you
see above and can be implemented on a computer. This is from p. 235 in Fisher. This keeps track of q and r as we go through the algorithm. |
||||||||
| 1. Let X=(1,0a), Y=(0,1,b) and Z=(0,0,0) | ||||||||
| 2. Divide the 3rd term of X by the third term of Y to get q and r. | ||||||||
| 3. Calculate X-qY | ex. (1,0,252)-1*(0,1,198)=(1,-1,54) | |||||||
| 4. If r=0, stop: T=(s,t,gcd(a,b)) | if r<>0, replace Z by Y, Y by X-qY and X by Z. go to set 2 | |||||||
| X | Y | q | r | X-qY | ||||
| (1,0,252) | (0,1,198) | 1 | 54 | (1,-1,54) | ||||
| (0,1,198) | (1,-1,54) | 3 | 36 | (-3,4,36) | ||||
| (1,-1,54) | (-3,4,36) | 1 | 18 | (4,-5,18) | ||||
| (-3,4,36) | (4,-5,18) | 2 | 0 | |||||
| When r=0,Y=(4,-5,18) | ||||||||
| s = 4 | t = -5 | gcd(252,198) = 18 | 18=4*252-5*198 | |||||
To solve linear congruencies we need to get the inverse of a mod function. In order to get an inverse, we need to have a and m be relatively prime.
sa +tm =1 [gcd(a,m)=1; see Linear combinations]
sa + tm ~ 1 (mod m)
tm ~ 0 (mod m) [the remainder of m | tm is 0]
sa ~ 1 (mod m) [tm=0]
s is the inverse of a [s*a=1]
Find the inverse of 3 mod 7
since gcd(3,7)=1, the inverse exists and is unique
7=2*3+1 or 1= -2 * 3 + 1 *7 see Euclidean
Algorithm
Since 5*3 ~ 1 mod 7, 3 and 5 are inverses of each other.
...-2, 5,... are also inverses
| Using the better version of the Euclidean Algorithm | ||||
| X | Y | q | r | X-qY |
| (1,0,7) | (0,1,3) | 2 | 1 | (1,-2,1) |
| (0,1,3) | (1,-2,1) | 3 | 0 | |
| Since r=0 and Y=(1,-2,1), s=1, t=-2, gcd(7,3)=1 and 1=1*7-2*3 | ||||
| Hence -2 is the inverse of 3; So is the set {-2 +/- 7} since we are dealing with mod 7 | ||||
HINT: a~b (mod m) has inverses ONLY IF m is PRIME. see Existence of inverses. for counter example.
A very large prime number resource page. If you are interested in prime numbers there is a little of everything for you from history to discussions to programs. (Hint: you may want to look at these when you work on project 1.) You can take a whole course on prime numbers so if this interests you, this is the resource for you.
| Sieve of Eratosthenes (program to find primes) | |
| array x(i)=(1,1,1,1,1,1,,,,1) DO k=3 TO sqr(2*n+1) BY
2 DO k=1 TO N
|
set x to be an array of n 1's. {ii}where
i=1 to n. This is a set of flags to initialize all numbers from 1 to n as prime. look at numbers from 3 to the square root of n. x((k-1)/2) are the odd numbers. (Any even number other than 2 is not prime.) Start with the square of smallest prime other than 2 (ie. 3) and search for numbers divisible by 2*k. These numbers are divisible by 2 and/or k and hence are not prime. We mark these number, by position, as 0. We then go to the next odd integer and repeat the process. When done print the array where x(k)=1
|
| search pattern: 4,6,8.. 2n+1 are eliminated initially
we are left with 5,7,11,13,17,19,23..... |
take care of number devisable by 2 ahead of
time. mark off numbers divisible by 3, by 5, by 7, by 9 etc.
|
| Comment: The problem with this version is that numbers divisible by 9 are also divisible by 3. Or, more generally, if k is not itself prime, we want to skip over looking at its factors. We can do this by checking that k is not on our list of primes. In practice, there are many other shortcuts that can be used. However, even with those, the problem takes a long time as n gets larger. | |
Modular Arithmetic.
It can be shown that if a~b (mod m) and m is prime, then inverses exist for all integers 1..m-1. In addition the inverses are unique.
example: for multiplication: as ~ 1 (mod 3) where s is the inverse of a<>0
The inverse of 2 is 2 since 2*2 ~ 4 mod 3 ~ 1 mod 3
The inverse of 1 is 1 since 1*1 ~ 1 mod 3
counter example: as ~ 1 (mod 4) where s is the inverse of a <>0
1*2 =2 (mod 4) and 3*2= 6 mod 4 = 2 mod 4
both "1" can "3" can not be inverses of 2
the reason for this is that 2 | 4 and 2 | 2 so 2 is not a unique divisor (mod 4), see Z/(4) for tables. (2 is not relatively prime to 4.)
NOTE: that since 1 is relatively prime to 4, the inverse of 1 (in this case 1) exists. 1b=1 mod 4, b=1.
| Exercise 7: Find the inverse 19 mod 141. gcd(19,141)=1 therefore an inverse exists. we want to find 19s ~ 1 mod 141 |
||||
| X | Y | q | r | X-qY |
| 1,0,141 | 0,1,19 | 7 | 8 | -1, -7, 8 |
| 0,1,19 | -1,-7,8 | 2 | 3 | -2,15,3 |
| -1,-7,8 | -2,15,3 | 2 | 2 | 3,-37,2 |
| -2,15,3 | 3,-37,2 | 1 | 1 | -5,52,1 |
| 3,-23,2 | -5,52,1 | 2 | 0 | |
| s=52; therefore, 52 is the inverse of 19: 52*19 ~ 1 mod 141 | ||||
| X | Y | q | r | X-qY |
| 0,141 | 1,19 | 7 | 8 | -7, 8 |
| 1,19 | -7,8 | 2 | 3 | 15,3 |
| -7,8 | 15,3 | 2 | 2 | -37,2 |
| 15,3 | -37,2 | 1 | 1 | 52,1 |
| -23,2 | 52,1 | 2 | 0 | |
| Note that to get the inverse, we never used the first number so that we could have just used the second pair. This gives us a faster way to compute the inverse. Note also that the remainder gets smaller as we progress and s gets bigger. | ||||
| #include <stdio.h> void main(int argc, char *argv[]) { long int a= 144445551; long int m= 194444444; int x[3] = {1,0,a}; int y[3] = {0,1,m}; int z[3] = {0,0,0}; int k,rr,i,q,t,s,gcd; int r=100; for (k=1; r>0; k++) { if (y[2]>0) r=x[2] % y[2]; if (y[2]>0) q=x[2]/y[2]; for (i=0; i<3; i++) { z[i]=x[i] - q*(y[i]); } for (i=0; i<3; i++){ x[i]=y[i]; y[i]=z[i]; } printf("\n(%i,%i,%i), (%i,%i,%i), %i,%i, (%i,%i,%i)", x[0],x[1],x[2],y[0],y[1], y[2],q,r,z[0],z[1],z[2]); } s=x[0]; t=x[1]; gcd=x[2]; printf("\ns=%i, t=%i, gcd=%i",s,t,gcd); if (gcd=1) printf("\nThe inverse of %i is %i", a,t); |
You are the visitor to this site.