Top Banner
1 Integers and Integers and Division Division CS/APMA 202 CS/APMA 202 Rosen section 2.4 Rosen section 2.4 Aaron Bloomfield Aaron Bloomfield
38

1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

Dec 23, 2015

Download

Documents

Adela Sparks
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: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

11

Integers and DivisionIntegers and Division

CS/APMA 202CS/APMA 202

Rosen section 2.4Rosen section 2.4

Aaron BloomfieldAaron Bloomfield

Page 2: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

22

Rosen, chapter 2Rosen, chapter 2

We are only doing 2 or 3 of the sections in We are only doing 2 or 3 of the sections in chapter 2chapter 2 2.4: integers and division2.4: integers and division 2.6: applications of number theory2.6: applications of number theory

And only parts of that sectionAnd only parts of that section 2.7: matrices (maybe)2.7: matrices (maybe)

Page 3: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3

Quick surveyQuick survey

Have you seen matrices before?Have you seen matrices before?

a)a) Lots and lots and lotsLots and lots and lots

b)b) A fair amountA fair amount

c)c) Just a littleJust a little

d)d) Is that kinda like the movie?Is that kinda like the movie?

Page 4: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

44

Why prime numbers?Why prime numbers?

Prime numbers are not well understoodPrime numbers are not well understood

Basis for today’s cryptographyBasis for today’s cryptography

Unless otherwise indicated, we are only Unless otherwise indicated, we are only talking about positive integers for this talking about positive integers for this chapterchapter

Page 5: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

55

The divides operatorThe divides operator

New notation: 3 | 12New notation: 3 | 12 To specify when an integer evenly divides To specify when an integer evenly divides

another integeranother integer Read as “3 divides 12”Read as “3 divides 12”

The not-divides operator: 5 | 12The not-divides operator: 5 | 12 To specify when an integer does To specify when an integer does notnot evenly evenly

divide another integerdivide another integer Read as “5 does not divide 12”Read as “5 does not divide 12”

Page 6: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

66

Theorem on the divides operatorTheorem on the divides operator

If a | b and a | c, then a | (b+c)If a | b and a | c, then a | (b+c) Example: if 5 | 25 and 5 | 30, then 5 | (25+30)Example: if 5 | 25 and 5 | 30, then 5 | (25+30)

If a | b, then a | bc for all integers cIf a | b, then a | bc for all integers c Example: if 5 | 25, then 5 | 25*c for all ints cExample: if 5 | 25, then 5 | 25*c for all ints c

If a | b and b | c, then a | cIf a | b and b | c, then a | c Example: if 5 | 25 and 25 | 100, then 5 | 100Example: if 5 | 25 and 25 | 100, then 5 | 100

The book calls this Theorem 1The book calls this Theorem 1

Page 7: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

77

Prime numbersPrime numbers

A positive integer A positive integer pp is prime if the only is prime if the only positive factors of positive factors of pp are 1 and are 1 and pp If there are other factors, it is compositeIf there are other factors, it is composite Note that 1 is not prime!Note that 1 is not prime!

It’s not composite either – it’s in its own classIt’s not composite either – it’s in its own class

An integer An integer nn is composite if and only if is composite if and only if there exists an integer there exists an integer aa such that such that aa | | nn and 1 < and 1 < aa < < nn

Page 8: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

88

Fundamental theorem of arithmeticFundamental theorem of arithmetic

Every positive integer greater than 1 can be Every positive integer greater than 1 can be uniquely written as a prime or as the product of uniquely written as a prime or as the product of two or more primes where the prime factors are two or more primes where the prime factors are written in order of non-decreasing sizewritten in order of non-decreasing size

ExamplesExamples 100 = 2 * 2 * 5 * 5100 = 2 * 2 * 5 * 5 182 = 2 * 7 * 13182 = 2 * 7 * 13 29820 = 2 * 2 * 3 * 5 * 7 * 7129820 = 2 * 2 * 3 * 5 * 7 * 71

The book calls this Theorem 2The book calls this Theorem 2

Page 9: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

99

Composite factorsComposite factors

If If n n is a composite integer, then is a composite integer, then nn has a prime divisor less has a prime divisor less than or equal to the square root of than or equal to the square root of nn

Direct proofDirect proof Since Since nn is composite, it has a factor is composite, it has a factor aa such that 1< such that 1<aa<<nn Thus, Thus, nn = = abab, where , where aa and and bb are positive integers greater than 1 are positive integers greater than 1 Either Either aa≤≤nn or or bb≤≤nn (Otherwise, (Otherwise, abab > > nn**nn > > nn)) Thus, Thus, nn has a divisor not exceeding has a divisor not exceeding nn This divisor is either prime or a compositeThis divisor is either prime or a composite

If the latter, then it has a prime factorIf the latter, then it has a prime factor In either case, In either case, nn has a prime factor less than has a prime factor less than nn

The book calls this Theorem 3The book calls this Theorem 3

Page 10: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1010

Showing a number is primeShowing a number is prime

Show that 113 is prime (Rosen, question Show that 113 is prime (Rosen, question 8c, §2.4)8c, §2.4)

SolutionSolution The only prime factors less than The only prime factors less than 113 = 10.63 113 = 10.63

are 2, 3, 5, and 7are 2, 3, 5, and 7 Neither of these divide 113 evenlyNeither of these divide 113 evenly Thus, by the fundamental theorem of Thus, by the fundamental theorem of

arithmetic, 113 must be primearithmetic, 113 must be prime

Page 11: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1111

Showing a number is compositeShowing a number is composite

Show that 899 is prime (Rosen, question 8c, Show that 899 is prime (Rosen, question 8c, §2.4)§2.4)

SolutionSolution Divide 899 by successively larger primes, starting with Divide 899 by successively larger primes, starting with

22 We find that 29 and 31 divide 899We find that 29 and 31 divide 899

On a unix system, enter “factor 899”On a unix system, enter “factor 899”aaron@orion:~.16> factor 899aaron@orion:~.16> factor 899

899: 29 31899: 29 31

Page 12: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1212

Primes are infinitePrimes are infinite

Theorem (by Euclid): There are infinitely many prime Theorem (by Euclid): There are infinitely many prime numbersnumbers

The book calls this Theorem 4The book calls this Theorem 4

Proof by contradictionProof by contradictionAssume there are a finite number of primesAssume there are a finite number of primes

List them as follows: List them as follows: pp11, p, p22 …, p …, pnn..

Consider the number Consider the number qq = = pp11pp22 … p … pnn + 1 + 1 This number is not divisible by any of the listed primesThis number is not divisible by any of the listed primes

If we divided If we divided ppii into into qq, there would result a remainder of 1, there would result a remainder of 1 We must conclude that We must conclude that qq is a prime number, not among the primes is a prime number, not among the primes

listed abovelisted aboveThis contradicts our assumption that all primes are in the list This contradicts our assumption that all primes are in the list pp11, p, p22 …, p …, pnn..

Page 13: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1414

Mersenne numbersMersenne numbers

Mersenne nubmer: any number of the form 2Mersenne nubmer: any number of the form 2nn-1-1Mersenne prime: any prime of the form 2Mersenne prime: any prime of the form 2pp-1, where -1, where pp is is also a primealso a prime

Example: 2Example: 255-1 = 31 is a Mersenne prime-1 = 31 is a Mersenne prime Example: 2Example: 21111-1 = 2047 is not a prime (23*89)-1 = 2047 is not a prime (23*89)

Largest Mersenne prime: 2Largest Mersenne prime: 224,036,58324,036,583-1, which has -1, which has 7,235,733 digits7,235,733 digitsIf M is a Mersenne prime, then M(M+1)/2 is a perfect If M is a Mersenne prime, then M(M+1)/2 is a perfect numbernumber

A perfect number equals the sum of its divisorsA perfect number equals the sum of its divisors Example: 2Example: 233-1 = 7 is a Mersenne prime, thus 7*8/2 = 28 is a -1 = 7 is a Mersenne prime, thus 7*8/2 = 28 is a

perfect numberperfect number28 = 1+2+4+7+1428 = 1+2+4+7+14

Example: 2Example: 255-1 = 31 is a Merenne prime, thus 31*32/2 = 496 is a -1 = 31 is a Merenne prime, thus 31*32/2 = 496 is a perfect numberperfect number

Page 14: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1515

Merenne primesMerenne primes

Reference for Mersenne primes:Reference for Mersenne primes: http://http://

mathworld.wolfram.com/MersennePrime.htmlmathworld.wolfram.com/MersennePrime.html

Finding Mersenne primesFinding Mersenne primes GIMPS – Great Internet Mersenne Prime SearchGIMPS – Great Internet Mersenne Prime Search http://http://www.mersenne.org/prime.htmwww.mersenne.org/prime.htm

A new one was just discovered (last week): A new one was just discovered (last week): http://mathworld.wolfram.com/news/2005-02-18/http://mathworld.wolfram.com/news/2005-02-18/mersenne/mersenne/ This is only the 42This is only the 42ndnd such prime discovered such prime discovered

Page 15: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1616

The prime number theoremThe prime number theorem

The radio of the number of primes not exceeding The radio of the number of primes not exceeding xx and and xx/ln(/ln(xx) ) approaches 1 as approaches 1 as xx grows without bound grows without bound

Rephrased: the number of prime numbers less than Rephrased: the number of prime numbers less than xx is approximately is approximately xx/ln(/ln(xx))

Rephrased: the chance of an number x being a prime number is Rephrased: the chance of an number x being a prime number is 1 / ln(1 / ln(xx))

Consider 200 digit prime numbersConsider 200 digit prime numbers ln (10ln (10200200) ) 460 460 The chance of a 200 digit number being prime is 1/460The chance of a 200 digit number being prime is 1/460 If we only choose odd numbers, the chance is 2/460 = 1/230If we only choose odd numbers, the chance is 2/460 = 1/230 This result will be used in the next lecture!This result will be used in the next lecture!

The book calls this Theorem 5The book calls this Theorem 5

Page 16: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1717

Showing a number is prime or notShowing a number is prime or not

Consider showing that 2Consider showing that 2650650-1 is prime-1 is prime That number has about 200 digitsThat number has about 200 digits

There are approximately 10There are approximately 10193193 prime numbers less than prime numbers less than 22650650-1-1

By theorem 5 (By theorem 5 (xx/ln(/ln(xx), where x = 2), where x = 2650650-1)-1)

How long would that take to test each of those prime How long would that take to test each of those prime numbers?numbers?

Assume a computer can do 1 billion (10Assume a computer can do 1 billion (1099) per second) per second It would take 10It would take 10193193/10/1099 = 10 = 10184184 seconds seconds That’s 3.2 * 10That’s 3.2 * 10176176 years! years! There are quicker methods to show a number is prime, but There are quicker methods to show a number is prime, but notnot to to

find the factors if the number is found to be compositefind the factors if the number is found to be compositeWe will use this in the next lectureWe will use this in the next lecture

Page 17: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1818

The division “algorithm”The division “algorithm”

Let Let aa be an integer and be an integer and dd be a positive be a positive integer. Then there are unique integers integer. Then there are unique integers qq and and rr, with 0 ≤ , with 0 ≤ rr < < dd, such that , such that aa = = dq+rdq+r

We then define two operators:We then define two operators: qq = = aa divdiv dd rr = = aa modmod dd

The book calls this Theorem 6The book calls this Theorem 6

Page 18: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

1919

Greatest common divisorGreatest common divisor

The greatest common divisor of two The greatest common divisor of two integers integers aa and and b b is the largest integer is the largest integer dd such that such that dd | a and | a and dd | b | b Denoted by gcd(a,b)Denoted by gcd(a,b)

ExamplesExamples gcd (24, 36) = 12gcd (24, 36) = 12 gcd (17, 22) = 1gcd (17, 22) = 1 gcd (100, 17) = 1gcd (100, 17) = 1

Page 19: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2020

Relative primesRelative primes

Two numbers are Two numbers are relatively primerelatively prime if they if they don’t have any common factors (other don’t have any common factors (other than 1)than 1) Rephrased: Rephrased: aa and and bb are relatively prime if are relatively prime if

gcd (a,b) = 1gcd (a,b) = 1

gcd (25, 39) = 1, so 25 and 39 are gcd (25, 39) = 1, so 25 and 39 are relatively primerelatively prime

Page 20: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2121

Pairwise relative primePairwise relative prime

A set of integers A set of integers aa11, a, a22, … a, … ann are pairwise are pairwise relatively prime if, for all pairs of numbers, they relatively prime if, for all pairs of numbers, they are relatively primeare relatively prime Formally: The integers Formally: The integers aa11, a, a22, … a, … ann are pairwise are pairwise

relatively prime if gcd(relatively prime if gcd(aaii, a, ajj) = 1 whenever 1 ≤ ) = 1 whenever 1 ≤ ii < < jj ≤ ≤ nn..

Example: are 10, 17, and 21 pairwise relatively Example: are 10, 17, and 21 pairwise relatively prime?prime? gcd(10,17) = 1, gcd (17, 21) = 1, and gcd (21, 10) = 1gcd(10,17) = 1, gcd (17, 21) = 1, and gcd (21, 10) = 1 Thus, they are pairwise relatively primeThus, they are pairwise relatively prime

Example: are 10, 19, and 24 pairwise relatively Example: are 10, 19, and 24 pairwise relatively prime?prime? Since gcd(10,24) ≠ 1, they are notSince gcd(10,24) ≠ 1, they are not

Page 21: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2222

More on gcd’sMore on gcd’s

Given two numbers Given two numbers aa and and bb, rewrite them , rewrite them as:as: Example: gcd (120, 500)Example: gcd (120, 500)

120 = 2120 = 233*3*5 = 2*3*5 = 233*3*311*5*511

500 = 2500 = 222*5*533 = 2 = 222*3*300*5*533

Then compute the gcd by the following Then compute the gcd by the following formula:formula: Example: gcd(120,500) = 2Example: gcd(120,500) = 2min(3,2)min(3,2)33min(1,0)min(1,0)55min(1,3) min(1,3) = =

22223300551 1 = 20= 20

nn bn

bban

aa pppbpppa ...,... 21212121

),min(),min(2

),min(1 ...),gcd( 2211 nn ba

nbaba pppba

Page 22: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2323

Least common multipleLeast common multiple

The least common multiple of the positive The least common multiple of the positive integers integers aa and and bb is the smallest positive is the smallest positive integer that is divisible by both integer that is divisible by both aa and and bb.. Denoted by lcm (Denoted by lcm (aa, , bb))

Example: lcm(10, 25) = 50Example: lcm(10, 25) = 50What is lcm (95256, 432)?What is lcm (95256, 432)? 95256 = 295256 = 23333557722, 432=2, 432=2443333

lcm (2lcm (23333557722, 2, 2443333) = 2) = 2max(3,4)max(3,4)33max(5,3)max(5,3)77max(2,0)max(2,0) = = 224433557722 = 190512 = 190512

),max(),max(2

),max(1 ...),lcm( 2211 nn ba

nbaba pppba

Page 23: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2424

lcm and gcd theoremlcm and gcd theorem

Let Let aa and and bb be positive integers. Then be positive integers. Then a*ba*b = gcd( = gcd(aa,,bb) * lcm () * lcm (aa, , bb))

Example: gcd (10,25) = 5, lcm (10,25) = 50Example: gcd (10,25) = 5, lcm (10,25) = 50 10*25 = 5*5010*25 = 5*50

Example: gcd (95256, 432) = 216, lcm (95256, Example: gcd (95256, 432) = 216, lcm (95256, 432) = 190512432) = 190512 95256*432 = 216*19051295256*432 = 216*190512

The book calls this Theorem 7The book calls this Theorem 7

Page 24: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2525

Modular arithmeticModular arithmetic

If If aa and and b b are integers and are integers and mm is a positive integer, then is a positive integer, then aa is is congruent to b modulo mcongruent to b modulo m if if mm divides divides a-ba-b

Notation: Notation: aa ≡ ≡ bb (mod (mod mm)) Rephrased: Rephrased: mm | | a-ba-b Rephrased: Rephrased: aa mod mod mm = = bb If they are not congruent: If they are not congruent: aa ≡ ≡ bb (mod (mod mm))

Example: Is 17 congruent to 5 modulo 6?Example: Is 17 congruent to 5 modulo 6? Rephrased: 17 ≡ 5 (mod 6)Rephrased: 17 ≡ 5 (mod 6) As 6 divides 17-5, they are congruentAs 6 divides 17-5, they are congruent

Example: Is 24 congruent to 14 modulo 6?Example: Is 24 congruent to 14 modulo 6? Rephrased: 24 ≡ 14 (mod 6)Rephrased: 24 ≡ 14 (mod 6) As 6 does not divide 24-14 = 10, they are not congruentAs 6 does not divide 24-14 = 10, they are not congruent

Page 25: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2626

More on congruenceMore on congruence

Let Let aa and and bb be integers, and let be integers, and let mm be a positive be a positive integer. Then integer. Then aa ≡ ≡ bb (mod (mod mm) if and only if ) if and only if aa mod mod mm = = bb modmod mm The book calls this Theorem 8The book calls this Theorem 8

Example: Is 17 congruent to 5 modulo 6?Example: Is 17 congruent to 5 modulo 6? Rephrased: does 17 ≡ 5 (mod 6)?Rephrased: does 17 ≡ 5 (mod 6)? 17 mod 6 = 5 mod 617 mod 6 = 5 mod 6

Example: Is 24 congruent to 14 modulo 6?Example: Is 24 congruent to 14 modulo 6? Rephrased: 24 ≡ 14 (mod 6)Rephrased: 24 ≡ 14 (mod 6) 24 mod 6 ≠ 14 mod 624 mod 6 ≠ 14 mod 6

Page 26: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2727

Even more on congruenceEven more on congruence

Let Let mm be a positive integer. The integers be a positive integer. The integers aa and and b b are congruent modulo are congruent modulo mm if and only if if and only if there is an integer there is an integer kk such that such that a = b + kma = b + km The book calls this Theorem 9The book calls this Theorem 9

Example: 17 and 5 are congruent modulo 6Example: 17 and 5 are congruent modulo 6 17 = 5 + 2*617 = 5 + 2*6 5 = 17 -2*65 = 17 -2*6

Page 27: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2828

Even even more on congruenceEven even more on congruence

Let Let mm be a positive integer. If be a positive integer. If aa ≡ ≡ bb (mod (mod mm) and ) and cc ≡ ≡ dd (mod (mod mm), then ), then a+c ≡ a+c ≡ ((b+db+d) (mod ) (mod mm) and ) and ac ≡ bdac ≡ bd (mod (mod mm)) The book calls this Theorem 10The book calls this Theorem 10

ExampleExample We know that 7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5)We know that 7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5) Thus, 7+11 ≡ (2+1) (mod 5), or 18 ≡ 3 (mod 5)Thus, 7+11 ≡ (2+1) (mod 5), or 18 ≡ 3 (mod 5) Thus, 7*11 ≡ 2*1 (mod 5), or 77 ≡ 2 (mod 5)Thus, 7*11 ≡ 2*1 (mod 5), or 77 ≡ 2 (mod 5)

Page 28: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

2929

Uses of congruencesUses of congruences

Hashing functionsHashing functions

aaron@orion:~/ISOs/dvd.39> md5sum debian-31-i386-binary.iso aaron@orion:~/ISOs/dvd.39> md5sum debian-31-i386-binary.iso

96c8bba5a784c2f48137c22e99cd5491 debian-31-i386-binary.iso96c8bba5a784c2f48137c22e99cd5491 debian-31-i386-binary.iso

md5 (file) = <file> mod 2md5 (file) = <file> mod 2128128

Not really – this is a simplificationNot really – this is a simplification

Page 29: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3030

Today’s demotivatorsToday’s demotivators

Page 30: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3131

Pseudorandom numbersPseudorandom numbers

Computers cannot generate truly random Computers cannot generate truly random numbers!numbers!

Algorithm for “random” numbers: choose 4 Algorithm for “random” numbers: choose 4 integersintegers Seed Seed xx00: starting value: starting value Modulus Modulus mm: maximum possible value: maximum possible value Multiplier Multiplier aa: such that 2 ≤ : such that 2 ≤ aa < < mm Increment Increment cc: between 0 and : between 0 and mm

Formula: Formula: xxnn+1+1 = ( = (axaxnn + + cc) mod ) mod mm

Page 31: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3232

Pseudorandom numbersPseudorandom numbers

Formula: Formula: xxnn+1+1 = ( = (axaxnn + + cc) mod ) mod mmLet Let xx00 = 3, = 3, mm = 9, = 9, aa = 7, and = 7, and cc = 4 = 4

xx11 = 7 = 7xx00+4 = 7*3+4 = 25 mod 9 = 7+4 = 7*3+4 = 25 mod 9 = 7xx22 = 7 = 7xx11+4 = 7*7+4 = 53 mod 9 = 8+4 = 7*7+4 = 53 mod 9 = 8xx33 = 7 = 7xx22+4 = 7*8+4 = 60 mod 9 = 6+4 = 7*8+4 = 60 mod 9 = 6xx44 = 7 = 7xx33+4 = 7*6+4 = 46 mod 9 = 1+4 = 7*6+4 = 46 mod 9 = 1xx55 = 7 = 7xx44+4 = 7*1+4 = 46 mod 9 = 2+4 = 7*1+4 = 46 mod 9 = 2xx66 = 7 = 7xx55+4 = 7*2+4 = 46 mod 9 = 0+4 = 7*2+4 = 46 mod 9 = 0xx77 = 7 = 7xx66+4 = 7*0+4 = 46 mod 9 = 4+4 = 7*0+4 = 46 mod 9 = 4xx88 = 7 = 7xx77+4 = 7*4+4 = 46 mod 9 = 5+4 = 7*4+4 = 46 mod 9 = 5

Page 32: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3333

Pseudorandom numbersPseudorandom numbers

Formula: Formula: xxnn+1+1 = ( = (axaxnn + + cc) mod ) mod mm

Let Let xx00 = 3, = 3, mm = 9, = 9, aa = 7, and = 7, and cc = 4 = 4

This sequence generates:This sequence generates:3, 7, 8, 6, 1, 2, 0, 4, 5, 3 , 7, 8, 6, 1, 2, 0, 4, 5, 33, 7, 8, 6, 1, 2, 0, 4, 5, 3 , 7, 8, 6, 1, 2, 0, 4, 5, 3 Note that it repeats!Note that it repeats! But it selects all the possible numbers before doing soBut it selects all the possible numbers before doing so

The common algorithms today use The common algorithms today use mm = 2 = 23232-1-1 You have to choose 4 billion numbers before it You have to choose 4 billion numbers before it

repeatsrepeats

Page 33: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3434

The Caesar cipherThe Caesar cipher

Julius Caesar used this to encrypt messagesJulius Caesar used this to encrypt messages

A function A function ff to encrypt a letter is defined as: to encrypt a letter is defined as: ff((pp) = () = (pp+3) mod 26+3) mod 26 Where Where pp is a letter (0 is A, 1 is B, 25 is Z, etc.) is a letter (0 is A, 1 is B, 25 is Z, etc.)

Decryption: Decryption: ff-1-1((pp) = () = (pp-3) mod 26-3) mod 26

This is called a substitution cipherThis is called a substitution cipher You are substituting one letter with anotherYou are substituting one letter with another

Page 34: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3535

The Caesar cipherThe Caesar cipher

Encrypt “go cavaliers”Encrypt “go cavaliers” Translate to numbers: g = 6, o = 14, etc.Translate to numbers: g = 6, o = 14, etc.

Full sequence: 6, 14, 2, 0, 21, 0, 11, 8, 4, 17, 18Full sequence: 6, 14, 2, 0, 21, 0, 11, 8, 4, 17, 18 Apply the cipher to each number: Apply the cipher to each number: ff(6) = 9, (6) = 9, ff(14) = 17, etc.(14) = 17, etc.

Full sequence: 9, 17, 5, 3, 24, 3, 14, 11, 7, 20, 21Full sequence: 9, 17, 5, 3, 24, 3, 14, 11, 7, 20, 21 Convert the numbers back to letters 9 = j, 17 = r, etc.Convert the numbers back to letters 9 = j, 17 = r, etc.

Full sequence: jr wfdydolhuvFull sequence: jr wfdydolhuv

Decrypt “jr wfdydolhuv”Decrypt “jr wfdydolhuv” Translate to numbers: j = 9, r = 17, etc. Translate to numbers: j = 9, r = 17, etc.

Full sequence: 9, 17, 5, 3, 24, 3, 14, 11, 7, 20, 21Full sequence: 9, 17, 5, 3, 24, 3, 14, 11, 7, 20, 21 Apply the cipher to each number: Apply the cipher to each number: ff-1-1(9) = 6, (9) = 6, ff-1-1(17) = 14, etc.(17) = 14, etc.

Full sequence: 6, 14, 2, 0, 21, 0, 11, 8, 4, 17, 18Full sequence: 6, 14, 2, 0, 21, 0, 11, 8, 4, 17, 18 Convert the numbers back to letters 6 = g, 14 = 0, etc. Convert the numbers back to letters 6 = g, 14 = 0, etc.

Full sequence: go cavaliersFull sequence: go cavaliers

Page 35: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

3636

Rot13 encodingRot13 encoding

A Caesar cipher, but translates letters by 13 A Caesar cipher, but translates letters by 13 instead of 3instead of 3 Then, apply the same function to decrypt it, as Then, apply the same function to decrypt it, as

13+13=2613+13=26

Rot13 stands for “rotate by 13”Rot13 stands for “rotate by 13”

Example:Example:aaron@orion:~.4> echo Hello World | rot13aaron@orion:~.4> echo Hello World | rot13Uryyb JbeyqUryyb Jbeyqaaron@orion:~.5> echo Uryyb Jbeyq | rot13aaron@orion:~.5> echo Uryyb Jbeyq | rot13Hello WorldHello Worldaaron@orion:~.6>aaron@orion:~.6>

Page 36: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

37

Quick surveyQuick survey

I felt I understood the material in this I felt I understood the material in this slide set…slide set…

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 37: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

38

Quick surveyQuick survey

The pace of the lecture for this The pace of the lecture for this slide set was…slide set was…

a)a) FastFast

b)b) About rightAbout right

c)c) A little slowA little slow

d)d) Too slowToo slow

Page 38: 1 Integers and Division CS/APMA 202 Rosen section 2.4 Aaron Bloomfield.

39

Quick surveyQuick survey

How interesting was the material in How interesting was the material in this slide set? Be honest!this slide set? Be honest!

a)a) Wow! That was SOOOOOO cool!Wow! That was SOOOOOO cool!

b)b) Somewhat interestingSomewhat interesting

c)c) Rather bortingRather borting

d)d) ZzzzzzzzzzzZzzzzzzzzzz