Chapter 2 Discrete Structures Notes

 

Links to Key Topic on Page

Relatively Prime Numbers

Lemma 1 lcd

Prime Numbers

Definition of divide gcd

Linear Congruencies

Chinese Remainder Theorem Existence of inverses.

Linear Combinations

Linear Combination Euclidean Algorithm
Modular Arithmetic Working with Large Numbers Representation of Large Numbers
Using the better version of the Euclidean Algorithm Working with Matricesnew_tiny.gif (189 bytes) Finding the Inverse in Exercise 7 new_tiny.gif (189 bytes)
C program to find inverse and gcdnew_tiny.gif (189 bytes) 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 is in set notation and R is the equivelance class of a.
For example,
4 mod 5= {...-1,4,9...}
[4] is called the representative of the equivalance relationship.

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]
a|c = implies c=at
b +c = as + at = a(s + t) 
So from the definition, we have
a | (b+c) 

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
b|c implies c=bt
c=bt=ast=(st)a st=an integer
By the definition, a|c since c=(st)a from
Theorem 1

if 6/3 goes evenly and 12/6 goes evenly
then 12/3 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]
b|a implies b=at
b=at=bst
1=st
this means that s=1, t=1 or s=-1, t=-1
so a=-1*b or a=1*b

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.
contrast this with lcm.

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
252=198*1 +54

gcd(198,54)=gcd(54,36) since
198=54*3+36

gcd(54,36)=gcd(36,18) =18 since
36=18*2+0. [stop when a number goes in evenly.]

Therefore since gcd(36,18)=18,
gcd(36,18)=gcd(54,36)=gcd(198,54)
=gcd(252,198)=18

Hence gcd(252,198)=18.

This is the outline of the Euclidean algorithm.
See Euclidean Algorithms for example

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
d|a and d|b
d|a implies that d|sa
d|b implies that d|tb Theorem 1
d|sa and d|tb implies d|(sa+tb)
Therefore, since d is the greatest common divisor, d=sa+tb. 

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]
csa +ctb = c [multiply by c]
since a|bc a|tbc [Theorem 1]
a | csa    [a | (cs)a ]
Since a |csa and a|tbc, a|(csa+ctb)
a | (c*(sa+tb))
but (sa+tb) is a integer so
a |c

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
    m | (ac-bc) = m | c(a-b)
Since gcd(c,m)=1 and m |c(b-a)
    from Lemma 1 we get
m | (b-a)
    This is the definition of a~b mod m  

Example (5 is prime)
14~ 4 mod 5
divide by 2
7  ~ 2 mod 5

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)
9~  21 mod 4
divide by 3
3  ~ 7 mod 4

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:
   a mod m=r  
   b mod m=r  
Then,
  a=qm+r
  b=pm+r
  a-b=qm-pm=(q-p)m
  q-p is an integer so
  m | (a-b)
  or a~b mod 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
2*b ~ 1 mod 4 does not have a solution. Look at the tables and you will see that there is not an onto / 1-1 relation in the mod 4 tables. There is in the mod 5 table.

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
a+c mod m = c+r mod m

This allows us to do math using just the remainders.


Example:

5 ~ 1 mod 4 =1
add 7
5+7 ~ 1+7 mod 4
12 ~ 8 mod 4 = 1+7 mod 8 = 0 mod 4
1+7=8 or 0 mod 4

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
ac mod m = rc mod m

Multiplication always works; division only works if m is prime.

Multiplication by a constant.

Example

5 ~ 1 mod 4 =1
multiply by 6
5*6 ~ 1*6 mod 4
30 ~ 6 mod 4 =2
1*6=6 mod 4 =2

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
divide by 2 and we get 7 ~ 4 mod 6
which is incorrect.

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
b+d = a + sm + c + tm
b+d = a+c + sm + tm|
b+d = a +c + m(s+t)
b+d = a+c +mq     (q=st)
By definition, b+d(mod m) ~ a+c (mod m)

6~1 mod 5, 7 ~ 2 mod 5
6+7 mod 5 ~ 1+2 mod 5 =3 mod 5
Since 6+7=13 mod 5= 3 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
b*d = (a + sm) * (c + tm)
b*d = ac +smc + atm + stm*m
b*d = ac + m(sc+at+stm)
b*d = a*c +mq     (q=sc+at+stm)
By definition, b*d(mod m) ~ a*c (mod m)

6~1 mod 5, 7 ~ 2 mod 5
6*7 mod 5 ~ 1*2 mod 5 =2 mod 5
Since 6*7=42 mod 5 = 2 mod 5

     
Mod Applications Chinese Remainder Theorem If m1, m2 ... mn are pairwise relatively prime, the system

x~ a1 mod m1
x~a2 mod m2
.
x~an mod mn

has a unique solution mod m where m=
m1 * m2 .. mn

The solution is given by

x=
a1*M1*y1 +
a2*M2*y2  + ... an*Mn*yn


Mk = m/mk
yk is the inverse of Mk

Since gcd(mk, Mk)=1
since the numbers are relatively prime. This means that an inverse of Mk exists.That is Mkyk ~ 1 mod mk where yk is the inverse.

x ~ak Mk yk
~ ak (mod mk)

Example:

Suppose that we have

x ~ 2 mod 3
x ~ 3 mod 5
x ~ 2 mod 7

3,5,7 are pairwise relatively prime.
So m=3*5*7=105.

First, determine Mk:
Let Mk = m/mk then
M1 = 105/3 =35
M2 = 105/5 = 21
M3 = 105/7 =15

Second, determine yk, the inverse of  Mk
2 is an inverse of 35 mod 3, (35*2 ~ 1 mod 3)
1 is an inverse of 21 mod 5 (21 ~ 1 mod 5)
1 is an inverse of 15 mod 7 (15~ 1 mod 7)

Third, we get the a's from the original problem
a1 = 2
a2 = 3
a3 = 2

Lastly, using the formula to the left, we get
2*35*2 + 3*21*1 +2*15*1 mod (105) =
233 ~ 23 (mod105).

(The only hard part is finding the inverses.)

23 ~ 2 mod 3
23 ~ 3 mod 5
23 ~ 2 mod 7

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=(2,1)   5 mod 3=2, 5 mod 4=1
7=(1,3)  7 mod 3=1, 3 mod 4=3
11=(2,3) 11 mod 3=2, 11 mod 4=3
12=(0,0) 12 mod 3=0, 12 mod 4=0

5+ 7 = (2,1) + (1,3) = (3, 4) = (0,0)
since 3 mod 3=0 and 4 mod 4 =4.
But (0,0)=12 which is also 5+7.

5*7 =(2,1) * (1,3) = (2,3) = 11 mod 12
5*7=35 ~ 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
a set of smaller remainders (in the case above, numbers less than 100), we can do integer arithmetic on larger numbers than we can hold in a memory location.

 

 

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
     IF x((k-1)/2)=1 THEN DO
        DO i=k**2 TO 2*n+1 BY 2*k
               X((i-1)/2)=0
               END
        END
    END
END

DO k=1 TO N
      DO IF x(k)=1 THEN output 2*k+1
      END
END

 

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


9,15, 21, 27...81..99..117...2n+1  (i=2*3)
25,35,45,55... 2n+1  (i=2*5)
49,63,77...      2n+1  (i=2*7)
81,99,117...    2n+1   (i=2*9) [not needed]

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.
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.

C program to find inverse and gcd

#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.

Return to Main Page