ELEC1087: Discrete Mathematics
Lecture 3, 4, 5: Numbers & PrimesLecture 3, 4, 5: Numbers & Primes
Spring 2010 / Edmund Lam(based on notes by Dr Hayden So; illustrations from Rosen, Graham et al., and wikipedia)
Integer functionsInteger functions“Whole numbers constitute the backbone of discrete mathematics, and we often need to convert from fractions or arbitrary real numbers to integers.”
Graham, Knuth, Patashnik
Our roadmap:(notation) floor and ceiling(notation) division and modulusprime, and relatively primepublic-key cryptography
PAGE 2
Floor and Ceiling FunctionsFloor and Ceiling Functions
PAGE 3
Floor and CeilingFloor and Ceiling
3
22
1
01 2 3 4-4 -3 -2 -1
-11
-2
floor
ceiling-3
-4
PAGE 4
Properties of Floor and Ceiling 1Properties of Floor and Ceiling 1
PAGE 5
Properties of Floor and Ceiling 2Properties of Floor and Ceiling 2
PAGE 6
Floor/Ceiling Applications (1)Floor/Ceiling Applications (1)
PAGE 7
Floor/Ceiling Applications (2)Floor/Ceiling Applications (2)
Ans: Rosen, p. 145.
PAGE 8
Floor/Ceiling Applications (3)Floor/Ceiling Applications (3)
PAGE 9
“Real problem #1”: Josephus problemReal problem #1 : Josephus problemRemember the solution is:
1for 1)(2)2(
1)1(
≥−==
nnJnJ
J
1for 1)(2)12(
o)()(
≥+=+ nnJnJ
nnJnJ
Can also be written as:
⎣ ⎦ 1f)1()2/(2)(
1)1( =
JJ
Jn⎣ ⎦ 1for )1()2/(2)( >−−= nnJnJ n
PAGE 10
“Real problem #2”: SortingReal problem #2 : SortingSuppose we are sorting n>1 records
Divide into two approximately equal partsSort each part by the same method (recursively)
Merge, with at most n-1 further comparisons⎡ ⎤ ⎣ ⎦2/2/ nnn +=
Total number of comparisons is f(n), where
0)1( =f
G h l i 3 34 i h d i i f f( )
⎡ ⎤ ⎣ ⎦ 1for 1)2/()2/()( >−++= nnnfnfnf
Graham et al. exercise 3.34 gives the derivation for f(n):
⎡ ⎤nmnmnf m2log where12)( =+−=
PAGE 11
Worth thinkingWorth thinking…
Floor and ceiling are more than just a handy Floor and ceiling are more than just a handy notation; they help simplify the
representation and manipulation of practical problems, i.e. they provide good abstractions.
PAGE 12
Integer DivisionInteger Division
PAGE 13
Integer Functions TheoremsInteger Functions Theorems
PAGE 14
The Division AlgorithmThe Division Algorithm
PAGE 15
Modular ArithmeticModular ArithmeticIn many discrete math problems, we only care about the remainder of an integer division.Example: Assume a bus arrives at a bus stop every 10 minutes starting at 11:00am. It takes Albert 7 minutes to get to the bus stop while it takes Betty 13 minutes. Who
ll h l h b ?will have to wait longer at the bus stop?
PAGE 16
Modular Arithmetic NotationModular Arithmetic Notation
PAGE 17
Modular Arithmetic TheoremModular Arithmetic Theorem
PAGE 18
ExampleExample
Show that if n | m where n >1 m >1 are integersShow that if n | m, where n >1,m >1 are integers,
and if a ≡ b (modm), where a and b are integers,
then a ≡ b (modn)
PAGE 19
Using Congruence ArithmeticUsing Congruence ArithmeticHash FunctionChecksumCryptographyyp g p y
PAGE 20
HashingHashingA many-to-one mapping between a (relatively) larger set to a smaller set
e.g. memory map, database, dictionary
The mapping function is called a hashing functionA simple hash function: h(k) = k mod m
PAGE 21
Hashing ExampleHashing ExampleFor example, we want to sort students submitted midterm into 12 boxes according to their Chinese zodiac sign (rat ox into 12 boxes according to their Chinese zodiac sign (rat, ox, tiger, rabbit, etc)Solution: First, we label box for zodiac sign rat=0, ox=1, Solution: First, we label box for zodiac sign rat 0, ox 1, tiger=2…Then the box number (b) to hold the midterm for a student born in year (y) midterm will be calculated as:
b = (y - 2008) mod 12F l id f d b i 2000 ill b For example, midterm of a student born in year 2000 will be placed at box
b = (2000 - 2008) mod 12 = 4b (2000 2008) mod 12 4Therefore, it will be at box for year of dragon
PAGE 22
ChecksumChecksumUse to verify a number (code) is valid
E HKID d UPC d ISBN d dit d t E.g. HKID card, UPC code, ISBN code, credit card, passport number, etc
ISBN-10 code has 10 digits g{a9 a8 a7 … a1 a0}
Last digit is a check digit, computed against the weighted sum of previous 9 digits with weights {10 9 8 3 2} sum of previous 9 digits, with weights {10, 9, 8, … 3, 2} such that10 a9 + 9 a8 + … + 3 a2 + 2 a1 ≡ -a0 (mod 11)9 8 2 1 0 ( )In other word, the weighted sum of all digits add up as a multiple of 11
ISBN 007 124474 3e.g. ISBN 007-124474-3
PAGE 23
CryptographyCryptographyJulius Caesar’s Encryption Algorithm
Shift h l tt i f d i th l h b t b 3 Shift each letter in a message forward in the alphabet by 3 letters (with wrap-around)E.g “I LOVE DISCRETE MATH” becomes “L ORYH gGLVFUHWH PDWK”
Algorithm:R t h l tt b “A” = 0 “B”=1 t Represents each letter as a number “A” = 0, “B”=1, etc To encrypt a letter p, use the following function:
f (p) (p + 3) mod 26To decrypt a letter p, use the inverse function:
f (p) = (p + 3) mod 26
f −1(p) = (p − 3) mod 26
PAGE 24
Further thoughtsFurther thoughts
1. Modular arithmetic is “simple” to understand and compute.
2 Th ’ i h t t f th i d 2. There’s an inherent asymmetry: from the remainder we cannot deduce the original numbers.
3. Modular arithmetic turns out to be important in several pformulas involving prime numbers.
PAGE 25
Prime NumbersPrime NumbersPrime numbers have been studied extensively since
ancient timeAn extremely important class of numbers in modern
h i ll i h d hmath, especially with regard to cryptography
A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p
Otherwise, it is composite
PAGE 26
Division Algorithm ExampleDivision Algorithm ExampleExample A: 100 is divided by 7.Solution: We have 100 = 14(7) + 2.Therefore
i 100 di 7 14quotient = 100 div 7 = 14remainder = 100 mod 7 = 2
E l B 13 i di id d b 5Example B: -13 is divided by 5.Solution: We have -13 = -3 (5) + 2.Th fThereforequotient = -13 div 5 = -3remainder = 13 mod 5 = 2remainder = -13 mod 5 = 2
Note: remainder must be positive and smaller than the divisorthe divisor.
PAGE 27
Fundamental Theorem of ArithmeticFundamental Theorem of ArithmeticEvery positive integer greater than 1 can be written
l h d f uniquely as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing sizeof nondecreasing size.Examples:
10 = 2 × 510 = 2 × 512 = 2 × 2 × 3242 = 2 × 11 × 11242 = 2 × 11 × 11105 = 3 × 5 × 7
PAGE 28
Prime factorsPrime factors
Proof: [Rosen, p. 211]
There are infinite number of primes.Proof: [Rosen p 212]Proof: [Rosen, p. 212]
What is the largest known prime then?
PAGE 29
Mersenne PrimesMersenne PrimesMersenne primes are prime numbers of the form 2p -1 where p is prime2p -1 where p is prime
Useful to find extremely large primes that are useful for modern cryptographic applications
Much easier to verify its primality using Lucas-Lehmer test than other primesL t k i b 243112609 1Largest known prime number: 243112609-1
12,978,189 digits!316,470,269,330,255,923,143,453,723,949,337,516,054,106,188,475,264,644,14316,470,269,330,255,923,143,453,723,949,337,516,054,106,188,475,264,644,140,304,176,732,811,247,493,069,368,692,043,.. … 791,908,398,130,223,304,824,083,119,093,195,998,014,562,456,347,941,202,195,900,928,079,670,729,447,921,616,491,887,478,265,780,022,181,166,697,152,511511
http://prime.isthe.com/chongo/tech/math/prime/m43112609/prime-c.html
PAGE 30
Lucas Lehmer testLucas-Lehmer test
(only for odd p)
Some values:40 =s 32 =M
)121431(376342194
194214
)27(1424
2
22
21
=−=
×==−=
s
s
31
73 =
M
M
5
3=p
)121431(37634219423 ×==−=s 315 =M5=p
PAGE 31
Twin PrimeTwin PrimeExamples:
3 & 55 & 7…4967 & 4969…65,516,468,355×2333,333±1 (100,355 digits)
PAGE 32
Greatest Common DivisorGreatest Common DivisorThe greatest integer d that divides two non-zero integers
b i ll d h di i f d ba, b is called the greatest common divisor of a and bDenoted gcd(a,b)T i d b l ti l i if d( b) 1Two integers a and b are relatively prime if gcd(a,b) = 1Example
d(12 21) 3gcd(12,21) = 3gcd(15,28) = 1 [relatively prime]
PAGE 33
Least Common MultipleLeast Common MultipleThe least common multiple (LCM) of the integers a and bis the smallest positive integer that is divisible by both aand b.For example, lcm(12,15) = 60
For two positive integers a and b,ab = gcd(a,b) × lcm(a,b)
PAGE 34
The Euclidean Algorithm (1)The Euclidean Algorithm (1)To find the gcd of 2 integersBased on the following observation:Based on the following observation:
Proof:
PAGE 35
The Euclidean Algorithm (2)The Euclidean Algorithm (2)To find gcd(a,b):Let x := max(a,b), y := min(a,b)while y ≠ 0b ibegin
r := x mod yx := yx := yy := r
endendIn other words, divide the larger number x with the smaller one y to get remainder r. Then set y to be smaller one y to get remainder r. Then set y to be the bigger number, and r be the smaller number, and repeat the division until the remainder is 0p
PAGE 36
Euclidean Algorithm ExampleEuclidean Algorithm ExampleFind gcd(15,72)Step 1: 72 = 4 * 15 + 12Step 2: 15 = 1 * 12 + 3pStep 3: 12 = 4 * 3 + 0Therefore gcd(15 72) = 3 Therefore, gcd(15,72) 3
PAGE 37
Bringing them togetherBringing them together
P bli k t h C Public-key cryptography: Can we communicate a secret message in the open?
Yes with the discrete math we know!Yes—with the discrete math we know!
PAGE 38
Public key: an examplePublic-key: an exampleGreen = public Red = secret
Alice and Bob agrees prime p=23 and base g=5
Alice’s secret integer: a=6. Alice sends Bob: A=56 mod 23=8
Bob’s secret integer: b=15. Bob sends Alice: B=515 mod 23=19
Alice computes s=196 mod 23=2
Bob computes s=815 mod 23=2
Alice and Bob know 2, but Eve doesn’t!Eve
Use it as the private keythereafter
Magic?
PAGE 39
Alice Bob
Public key: an examplePublic-key: an exampleRequirements:
p is a primeg is a primitive root mod p (we did not discuss what this means, but you can look up the web)you can look up the web)
Calculations:A = ga mod pA g mod pB = gb mod ps = Ab mod p = gab mod p = gba mod p = Ba mod pp g p g p p
Key insight—the Discrete Logarithm Problem:Given A, g, p: difficult to calculate a. Need big prime number p (~ several hundred digits), and big a,bg needs not be big, e.g. 2 or 5
PAGE 40