7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory...

24
CompSci 102 © Michael Frank 7.1 Today’s topics Integers & Number Theory Integers & Number Theory – Integers Division, GCD Euclidean Alg – Mod! Reading: Sections 2.4,3,2 Reading: Sections 2.4,3,2 Upcoming Upcoming Sequences, Summations, & Induction Sequences, Summations, & Induction

Transcript of 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory...

Page 1: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.1

Today’s topics

• Integers & Number TheoryIntegers & Number Theory– Integers– Division, GCD– Euclidean Alg– Mod!

• Reading: Sections 2.4,3,2Reading: Sections 2.4,3,2• UpcomingUpcoming

– Sequences, Summations, & InductionSequences, Summations, & Induction

Page 2: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.2

§2.4: The Integers and Division

• Of course, you already know what the integers are, Of course, you already know what the integers are, and what division is…and what division is…

• But:But: There are some specific notations, There are some specific notations, terminology, and theorems associated with these terminology, and theorems associated with these concepts which you may not know.concepts which you may not know.

• These form the basics of These form the basics of number theorynumber theory..– Vital in many important algorithms today (hash Vital in many important algorithms today (hash

functions, cryptography, digital signatures).functions, cryptography, digital signatures).

• What are the basic axioms of the natural numbers What are the basic axioms of the natural numbers and elementary school operations?and elementary school operations?

Page 3: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.3

Divides, Factor, Multiple

• Let Let aa,,bbZZ with with aa00..• Def.:Def.: aa||bb ““aa dividesdivides bb”” :: ( (ccZZ:: b=acb=ac))

“There is an integer “There is an integer cc such that such that cc times times a a equals equals b.b.””– Example: 3Example: 312 12 TrueTrue, but 3, but 37 7 FalseFalse..

• Iff Iff aa divides divides bb, then we say , then we say aa is a is a factorfactor or a or a divisordivisor of of bb, and , and bb is a is a multiplemultiple of of aa..

• Ex.: “Ex.: “bb is even” : is even” :≡ 2|≡ 2|bb. Is 0 even? Is −4?. Is 0 even? Is −4?

Page 4: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.4

Facts re: the Divides Relation

• Theorem:Theorem: aa,,bb,,c c ZZ::1. 1. aa|0|0

2. 2. ((aa||bb aa||cc) ) aa | ( | (bb + + cc))

3. 3. aa||bb aa||bcbc

4. 4. ((aa||bb bb||cc) ) aa||cc

• ProofProof of (2): of (2): aa||bb means there is an means there is an ss such that such that bb==asas, and , and aa||cc means that there is a means that there is a tt such that such that cc==atat, so , so bb++cc = = asas++atat = = aa((ss++tt),), so so aa|(|(bb++cc)) also. also.■■

Page 5: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.5

Prime Numbers

• An integer An integer pp>1>1 is is primeprime iff it is not the product of iff it is not the product of two integers greater than 1:two integers greater than 1:

pp>1 >1 aa,,bbN:N: aa>1, >1, bb>1, >1, abab==pp..• The only positive factors of a prime The only positive factors of a prime pp are 1 and are 1 and pp

itself. itself. Some primes: 2,3,5,7,11,13...Some primes: 2,3,5,7,11,13...• Non-prime integers greater than 1 are called Non-prime integers greater than 1 are called

compositecomposite, because they can be , because they can be composedcomposed by by multiplying two integers greater than 1.multiplying two integers greater than 1.

Page 6: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.6

Review of §2.4 So Far

• aa||bb “ “aa dividesdivides bb” ” ccZZ:: b=acb=ac

• ““pp is is primeprime” ” pp>1 >1 aaN:N: (1 < (1 < a a < < p p a|pa|p))

• Terms Terms factorfactor, , divisordivisor, , multiplemultiple,, composite composite..

Page 7: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.7

Fundamental Theorem of Arithmetic

• Every positive integer has a Every positive integer has a uniqueunique representation as the product of a non-representation as the product of a non-decreasing series of zero or more primes.decreasing series of zero or more primes.– Some examples:Some examples:

• 1 = (product of empty series) = 11 = (product of empty series) = 1• 2 = 2 (product of series with one element 2)2 = 2 (product of series with one element 2)• 4 = 2·2 (product of series 2,2)4 = 2·2 (product of series 2,2)• 2000 = 2·2·2·2·5·5·5; 2001 = 3·23·29;2000 = 2·2·2·2·5·5·5; 2001 = 3·23·29;

2002 = 2·7·11·13; 2003 = 2003 (no clear pattern!)2002 = 2·7·11·13; 2003 = 2003 (no clear pattern!)Later, we will see how to rigorously prove the

Fundamental Theorem of Arithmetic, starting from scratch!

Page 8: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.8

An Application of Primes!• When you visit a secure web site (When you visit a secure web site (https:https:… …

address, indicated by padlock icon in IE, key icon address, indicated by padlock icon in IE, key icon in Netscape), the browser and web site may be in Netscape), the browser and web site may be using a technology called using a technology called RSA encryptionRSA encryption..

• This This public-key cryptography public-key cryptography scheme involves scheme involves exchanging exchanging public keyspublic keys containing the product containing the product pqpq of two random large primes of two random large primes pp and and qq (a (a private keyprivate key) ) which must be kept secret by a given party.which must be kept secret by a given party.

• So, the security of your day-to-day web So, the security of your day-to-day web transactions depends critically on the fact that all transactions depends critically on the fact that all known factoring algorithms are intractable!known factoring algorithms are intractable!– Note:Note: There There isis a tractable a tractable quantumquantum algorithm for factoring; so if algorithm for factoring; so if

we can ever build big quantum computers, then RSA is not secure.we can ever build big quantum computers, then RSA is not secure.

Page 9: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.9

The Division “Algorithm”

• It’s really just a It’s really just a theoremtheorem, not an algorithm… , not an algorithm… – Only called an “algorithm” for historical reasons.Only called an “algorithm” for historical reasons.

• Theorem: Theorem: For any integer For any integer dividenddividend aa and and divisordivisor dd≠0≠0, there is a unique integer , there is a unique integer quotientquotient qq and and remainder remainder rrNN such that such that a a = = dq dq + + rr and and 0 0 r r < |< |d|d|. Formally, the theorem is: . Formally, the theorem is: aa,,ddZZ, , dd≠≠0:0: !!qq,,rrZZ: 0: 0rr<|<|d|d|, , a=dqa=dq++rr..

• We can find We can find qq and and rr by: by: qq==aadd, , rr==aaqdqd..

Page 10: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.10

Greatest Common Divisor

• The The greatest common divisor greatest common divisor gcd(gcd(aa,,bb) of integers ) of integers aa,,bb (not (not both 0) is the largest (most positive) integer both 0) is the largest (most positive) integer dd that is a that is a divisor both of divisor both of aa and of and of bb..

dd = gcd( = gcd(aa,,bb) = ) = max(max(dd: : dd||a a dd||bb) ) dd||a a dd||bb eeZZ,, ((ee||aa ee||bb) → ) → d ≥ ed ≥ e

• Example:Example: gcd(24,36)=? gcd(24,36)=?Positive common divisors: 1,2,3,4,6,12.Positive common divisors: 1,2,3,4,6,12.The largest one of these is 12.The largest one of these is 12.

Page 11: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.11

GCD shortcut

• If the prime factorizations are written asIf the prime factorizations are written as and , and ,then the GCD is given by:then the GCD is given by:

• Example of using the shortcut:Example of using the shortcut:– aa=84=2·2·3·7 = 2=84=2·2·3·7 = 222·3·311·7·711

– bb=96=2·2·2·2·2·3 = 2=96=2·2·2·2·2·3 = 255·3·311·7·700

– gcd(84,96) = 2gcd(84,96) = 222·3·311·7·70 0 = 2·2·3 = 12.= 2·2·3 = 12.

nan

aa pppa K2121= nb

nbb pppb K2121=

.),gcd( ),min(),min(2

),min(1

2211 nn ban

baba pppba K=

Page 12: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.12

Relative Primality

• Integers Integers aa and and bb are called are called relatively primerelatively prime or or coprimecoprime iff their gcd = 1. iff their gcd = 1.– Example:Example: Neither 21 nor 10 is prime, but they Neither 21 nor 10 is prime, but they

are are coprimecoprime. . 21=3·721=3·7 and and 10=2·510=2·5, so they have , so they have no common factors > 1, so their gcd = 1.no common factors > 1, so their gcd = 1.

• A A setset of integers { of integers {aa11,,aa22,…} is ,…} is (pairwise) (pairwise)

relatively primerelatively prime if all pairs ( if all pairs (aaii, , aajj), for ), for iijj, ,

are relatively prime.are relatively prime.

Page 13: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.13

Least Common Multiple

• lcm(lcm(aa,,bb)) of positive integers of positive integers aa, , bb, is the smallest positive , is the smallest positive integer that is a multiple both of integer that is a multiple both of aa and of and of bb. . E.g.E.g. lcm(6,10)=30lcm(6,10)=30

mm = lcm( = lcm(aa,,bb) = min() = min(mm: : aa||mm bb||mm)) aa||mm bb||mm nnZZ: (: (aa||nn bb||nn) → () → (m ≤ nm ≤ n))

• If the prime factorizations are written asIf the prime factorizations are written as and , and ,

then the LCM is given bythen the LCM is given bynan

aa pppa K2121= nb

nbb pppb K2121=

.),(lcm ),max(),max(2

),max(1

2211 nn ban

baba pppba K=

Page 14: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.14

The mod operator

• An integer “division remainder” operator.An integer “division remainder” operator.• Let Let aa,,ddZZ with with d>d>11. Then . Then aa modmod dd denotes denotes

the remainder the remainder rr from the division from the division “algorithm” with dividend “algorithm” with dividend aa and divisor and divisor dd; ; i.e.i.e. the remainder when the remainder when aa is divided by is divided by dd..– Using Using e.g.e.g. long division. long division.

• We can compute We can compute ((aa modmod dd)) by: by: a a dd··aa//dd..• In C/C++/Java languages, “In C/C++/Java languages, “%%” = mod.” = mod.

Page 15: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.15

Modular Congruence

• Let Let aa,,bbZZ, , mmZZ++..Where Where ZZ++={={nnZ Z | | nn>0}=>0}=NN−{0}−{0} (the + integers). (the + integers).

• Then Then aa is congruent tois congruent to b modulo mb modulo m, written , written ““aabb (mod (mod mm))”, iff ”, iff m m | | aabb ..– Note: this is a different use of “Note: this is a different use of “” than the ” than the

meaning “is defined as” I’ve used before.meaning “is defined as” I’ve used before.

• It’s also equivalent to: It’s also equivalent to: ((aabb) mod ) mod m m = 0= 0..

Page 16: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.16

Spiral Visualization of mod

≡ 3(mod 5)

≡ 2(mod 5)

≡ 1(mod 5)

≡ 0(mod 5)

≡ 4(mod 5) 0

123

4

5

6

78

9

10

11

1213

14

15

16

1718

19

20

21

22

Example shown:modulo-5arithmetic

Page 17: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.17

Useful Congruence Theorems

• Theorem:Theorem: Let Let aa,,bbZZ, , mmZZ++. Then:. Then:aabb (mod (mod mm) ) kkZZ aa==bb++kmkm..

• Theorem:Theorem: Let Let aa,,bb,,cc,,ddZZ, , mmZZ++. Then if . Then if aabb (mod (mod mm)) and and ccdd (mod (mod mm)), then:, then:▪ ▪ a+ca+c b+db+d (mod (mod mm)), and, and

▪▪ ac ac bdbd (mod (mod mm))

Page 18: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.18

Some Misc. Results

• Theorem 0 (Euclid):Theorem 0 (Euclid): aa,,bb > 0: > 0: gcd(gcd(aa,,bb) = gcd() = gcd(bb, , a a mod mod bb))

• Theorem 1:Theorem 1: aa,,bb>0: >0: ss,,tt: gcd(: gcd(aa,,bb) = ) = sasa + + tbtb

• Lemma 1:Lemma 1: aa,,bb,,cc>0: >0: gcd(gcd(aa,,bb)=1 )=1 aa | | bcbc → → aa||cc

• Lemma 2:Lemma 2:– If If pp is prime and is prime and pp||aa11aa22……aann (integers (integers aaii) then ) then ii: : pp||aaii..

• Theorem 2:Theorem 2:– If If acac ≡ ≡ bcbc (mod (mod mm)) and and gcd(gcd(cc,,mm)=1)=1, then , then aa ≡ ≡ bb (mod (mod mm))..

Page 19: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.19

Proof Euclid’s Algorithm Works

Theorem 0:Theorem 0: gcd(gcd(aa,,bb) = gcd() = gcd(bb,,cc)) if if cc = = aa mod mod bb..

Proof:Proof: First, First, cc = = aa mod mod bb implies implies tt: : aa = = btbt + + cc. Let. Letgg = gcd( = gcd(aa,,bb)), and , and gg′ = gcd(′ = gcd(bb,,cc)). Since . Since gg||aa and and gg||bb (thus (thus gg||btbt) we know ) we know gg|(|(aa−−btbt)), , i.e.i.e. gg||cc. Since. Sincegg||bb gg||cc, it follows that , it follows that gg ≤ gcd( ≤ gcd(bb,,cc) = ) = gg′′. Now, since . Now, since gg′|′|bb (thus (thus g′|btg′|bt) and ) and gg′|′|cc, we know , we know gg′|(′|(btbt++cc)), , i.e.i.e., , gg′|′|aa. Since . Since g′g′||aa gg′|′|bb, it follows that, it follows thatgg′ ≤ gcd(′ ≤ gcd(a,ba,b) = ) = gg. Since we have shown that both . Since we have shown that both gg≤≤gg′′ and and gg′≤′≤gg, it must be the case that , it must be the case that gg==gg′′. ■. ■

Page 20: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.20

Proof of Theorem 1

Theorem 1:Theorem 1: aa≥≥bb≥≥0 0 stst: gcd(: gcd(aa,,bb) = ) = sasa + + tbtb

Proof:Proof: (By induction over the value of the larger argument (By induction over the value of the larger argument aa.).) From From theorem 0, we know that theorem 0, we know that gcd(gcd(aa,,bb) = gcd() = gcd(bb,,cc)) if ifcc = = aa mod mod bb, in which case , in which case aa = = kbkb + + cc for some integer for some integer kk, so , so cc = = aa − − kbkb. Now, since . Now, since bb<<aa and and cc<<bb, by inductive hypothesis, we can , by inductive hypothesis, we can assume that assume that uvuv: gcd(: gcd(bb,,cc) = ) = ubub + + vcvc. Substituting for . Substituting for cc, this is , this is ub+vub+v((aa−−kbkb)), which we can regroup to get, which we can regroup to get vava + ( + (uu−−vkvk))bb. So now let . So now let s = vs = v, and let , and let tt = = uu−−vkvk, and we’re finished. The base case is solved , and we’re finished. The base case is solved by by ss=1, =1, tt=0=0, which works for , which works for gcd(gcd(aa,0),0), or if , or if a=ba=b originally. originally. ■■

Page 21: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.21

Proof of Lemma 1

Lemma 1:Lemma 1: gcd(gcd(aa,,bb)=1 )=1 aa||bcbc → → aa||ccProof:Proof: Applying theorem 1, Applying theorem 1, stst: : sasa++tbtb=1=1. .

Multiplying through by Multiplying through by cc, we have that, we have thatsac sac + + tbctbc = = cc. Since . Since aa||bcbc is given, we know is given, we know that that aa||tbctbc, and obviously , and obviously aa||sacsac. Thus (using . Thus (using the theorem on p.154), it follows that the theorem on p.154), it follows that aa||((sacsac++tbctbc)); in other words, that ; in other words, that aa||cc. ■. ■

Page 22: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.22

Proof of Lemma 2

Lemma 2:Lemma 2: Prime Prime pp||aa11……aann → → ii: : pp||aaii..

Proof:Proof: If If nn=1=1, this is immediate since , this is immediate since pp||aa00 → → pp||aa00. Suppose . Suppose

the lemma is true for all the lemma is true for all nn<<kk and suppose and suppose pp||aa11……aakk. . If If pp||mm

where where mm==aa11……aakk-1-1 then we have it inductively. Otherwise, then we have it inductively. Otherwise,

we have we have pp||mamakk but but ¬(¬(pp||mm)). Since . Since mm is not a multiple of is not a multiple of pp, ,

and and pp has no factors, has no factors, mm has no common factors with has no common factors with pp, thus , thus gcd(gcd(mm,,pp)=1)=1. So by applying lemma 1, . So by applying lemma 1, pp||aakk. ■. ■

Page 23: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.23

Uniqueness of Prime Factorizations

““The prime factorization of any number The prime factorization of any number nn is unique.” is unique.”Theorem:Theorem: If If pp11……ppss = = qq11……qqtt are equal products of two are equal products of two

nondecreasing sequences of primes, then nondecreasing sequences of primes, then ss==tt and and ppii = = qqii for all for all i.i.

Proof:Proof: Assume (without loss of generality) that all primes Assume (without loss of generality) that all primes in common have already been divided out, so that in common have already been divided out, so that ijij: : ppii ≠ ≠ qqjj. . But since But since pp11……ppss = q = q11……qqtt, we have that , we have that pp11|q|q11……qqtt, since , since pp11·(·(pp22……ppss) = ) = qq11……qqtt.. Then applying lemma 2, Then applying lemma 2, jj: : pp11||qqjj. . Since Since qqjj is prime, it has no divisors other than itself and 1, is prime, it has no divisors other than itself and 1, so it must be that so it must be that ppii==qqjj.. This contradicts the assumption This contradicts the assumption ijij: : ppii ≠ q≠ qjj. . The only resolution is that after the common The only resolution is that after the common primes are divided out, both lists of primes were empty, so primes are divided out, both lists of primes were empty, so we couldn’t pick out we couldn’t pick out pp11. In other words, the two lists must . In other words, the two lists must have been identical to begin with! ■have been identical to begin with! ■

The “hard” part of proving the Fundamental Theorem of Arithmetic.

Page 24: 7.1 CompSci 102© Michael Frank Today’s topics Integers & Number TheoryIntegers & Number Theory – –Integers – –Division, GCD – –Euclidean Alg – –Mod! Reading:

CompSci 102 © Michael Frank 7.24

Proof of Theorem 2

Theorem 2:Theorem 2: If If acac ≡ ≡ bcbc (mod (mod mm)) and and gcd(gcd(cc,,mm)=1)=1, then , then aa ≡ ≡ bb (mod (mod mm))..

Proof:Proof: Since Since acac ≡ ≡ bcbc (mod (mod mm)), this means , this means mm | | acac−−bcbc. Factoring the right side, we get . Factoring the right side, we get m m | | cc((aa − − bb)). Since . Since gcd(gcd(cc,,mm)=1)=1, lemma 1 , lemma 1 implies that implies that m m | | aa−−bb, in other words, that , in other words, that a a ≡ ≡ bb (mod (mod mm)). ■. ■