ELEC1087: Discrete Mathematics

ELEC1087: Discrete Mathematics
Lecture 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 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 modulus
prime, and relatively prime
public-key cryptography
`
`
`
`
PAGE
2
Floor and Ceiling Functions
PAGE
3
Floor and Ceiling
3
2
1
-4
-3
-2
-1
0
1
2
3
4
1
-1
-2
-3
-4
PAGE
4
ceiling
floor
Properties of Floor and Ceiling 1
PAGE
5
Properties of Floor and Ceiling 2
PAGE
6
Floor/Ceiling Applications (1)
PAGE
7
Floor/Ceiling Applications (2)
`
Ans: Rosen, p. 145.
PAGE
8
Floor/Ceiling Applications (3)
PAGE
9
“Real
Real problem #1
#1”:: Josephus problem
`
Remember the solution is:
J (1) = 1
J (2n) = 2 J (n) − 1 for
o n ≥1
J (2n + 1) = 2 J (n) + 1 for n ≥ 1
`
Can also be written as:
J (1) = 1
J (n) = 2 J ( ⎣n / 2⎦) − (−1) n for
f n >1
PAGE
10
“Real
Real problem #2
#2”:: Sorting
`
Suppose we are sorting n>1 records
`
`
`
`
Divide into two approximately equal parts
Sort each part by the same method (recursively)
n = ⎡n / 2⎤ + ⎣n / 2⎦
Merge, with at most n-1 further comparisons
Total number of comparisons is f(n), where
f (1) = 0
f (n) = f ( ⎡n / 2⎤) + f ( ⎣n / 2⎦) + n − 1 for n > 1
`
G h et al.l exercise
Graham
i 33.34
34 gives
i
the
h d
derivation
i i ffor f(
f(n):
)
f (n) = nm − 2 m + 1 where m = ⎡log 2 n ⎤
PAGE
11
Worth thinking…
thinking
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 Division
PAGE
13
Integer Functions Theorems
PAGE
14
The Division Algorithm
PAGE
15
Modular Arithmetic
`
`
In 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
willll hhave to wait llonger at the
h bus
b stop??
PAGE
16
Modular Arithmetic Notation
PAGE
17
Modular Arithmetic Theorem
PAGE
18
Example
Show that if n | m,
m where n > 1,m
1 m > 1 are integers,
integers
and if a ≡ b (mod m), where a and b are integers,
then a ≡ b (mod n)
PAGE
19
Using Congruence Arithmetic
`
`
`
Hash Function
Checksum
Cryptography
yp g p y
PAGE
20
Hashing
`
A 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 function
A simple hash function: h(k) = k mod m
PAGE
21
Hashing Example
`
`
`
`
`
For example, we want to sort students submitted midterm
into 12 boxes according to their Chinese zodiac sign (rat,
(rat ox
ox,
tiger, rabbit, etc)
Solution: First, we label box for zodiac sign rat
rat=0,
0, ox
ox=1,
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 12
F example,
For
l midterm
id
off a student
d
bborn iin year 2000 will
ill bbe
placed at box
b = (2000 - 2008) mod 12 = 4
Therefore, it will be at box for year of dragon
PAGE
22
Checksum
`
Use to verify a number (code) is valid
`
`
`
`
EE.g. HKID card,
d UPC code,
d ISBN code,
d credit
dit card,
d passportt
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,
digits with weights {10
{10, 9,
9 8,
8 … 3,
3 2}
such that
10 a9 + 9 a8 + … + 3 a2 + 2 a1 ≡ -a0 ((mod 11))
In other word, the weighted sum of all digits add up as a
multiple of 11
`
PAGE
e.g. ISBN 007-124474-3
007 124474 3
23
Cryptography
`
Julius Caesar’s Encryption Algorithm
`
`
`
Shift each
h lletter
tt iin a message fforward
d iin th
the alphabet
l h b t bby 3
letters (with wrap-around)
E.gg “I LOVE DISCRETE MATH” becomes “L ORYH
GLVFUHWH PDWK”
Algorithm:
`
`
Represents
R
t eachh lletter
tt as a number
b “A” = 0,
0 “B”=1,
“B”=1 etc
t
To encrypt a letter p, use the following function:
f ( p) = ( p + 3) mod 26
`
To decrypt a letter p, use the inverse function:
f −1 ( p) = ( p − 3) mod 26
PAGE
24
Further thoughts
1. Modular arithmetic is “simple” to understand and compute.
2 Th
2.
There’s
’ an inherent
i h
t asymmetry:
t
from
f
the
th remainder
i d we
cannot deduce the original numbers.
p
in several
3. Modular arithmetic turns out to be important
formulas involving prime numbers.
PAGE
25
Prime Numbers
`
`
`
Prime numbers have been studied extensively since
ancient time
An extremely important class of numbers in modern
math,
h especially
i ll with
i h regard
d to cryptography
h
A positive integer p greater than 1 is called prime if
the only positive factors of p are 1 and p
`
PAGE
Otherwise, it is composite
26
Division Algorithm Example
`
`
`
`
`
Example A: 100 is divided by 7.
Solution: We have 100 = 14(7) + 2.
Therefore
quotient
i
= 100 di
div 7 = 14
remainder = 100 mod 7 = 2
E
Example
l B
B: -13
13 iis di
divided
id d by
b 5.
5
Solution: We have -13 = -3 (5) + 2.
Th f
Therefore
quotient = -13 div 5 = -3
remainder = -13
13 mod 5 = 2
Note: remainder must be positive and smaller than
the divisor.
divisor
PAGE
27
Fundamental Theorem of Arithmetic
`
`
Every positive integer greater than 1 can be written
uniquely
l as a prime or as the
h product
d
off two or more
primes where the prime factors are written in order
of nondecreasing size.
size
Examples:
`
`
`
`
PAGE
10 = 2 × 5
12 = 2 × 2 × 3
242 = 2 × 11 × 11
105 = 3 × 5 × 7
28
Prime factors
`
`
`
There are infinite number of primes.
`
`
Proof: [Rosen, p. 211]
Proof: [Rosen,
[Rosen p.
p 212]
What is the largest known prime then?
PAGE
29
Mersenne Primes
`
Mersenne primes are prime numbers of the form
2p -1 where p is prime
`
`
`
`
`
PAGE
30
Useful to find extremely large primes that are useful for
modern cryptographic applications
Much easier to verify its primality using LucasLehmer test than other primes
L
Largest
t kknown prime
i
number:
b 243112609-1
1
` 12,978,189 digits!
316,470,269,330,255,923,143,453,723,949,337,516,054,106,188,475,264,644,14
0,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,19
5,900,928,079,670,729,447,921,616,491,887,478,265,780,022,181,166,697,152,
511
http://prime.isthe.com/chongo/tech/math/prime/m4311260
9/prime-c.html
Lucas Lehmer test
Lucas-Lehmer
(only for odd p)
`
Some values:
s0 = 4
p=3
s1 = 4 − 2 = 14(= 7 × 2)
2
M2 = 3
M3 = 7
s2 = 14 − 2 = 194
2
p=5
PAGE
s3 = 194 2 − 2 = 37634(= 31× 1214)
31
Twin Prime
`
Examples:
`
`
`
`
`
`
PAGE
3&5
5&7
…
4967 & 4969
…
65,516,468,355×2333,333±1 (100,355 digits)
32
M 5 = 31
Greatest Common Divisor
`
`
`
`
The greatest integer d that divides two non-zero integers
a, b is
i called
ll d the
h greatest common di
divisor
i
off a and
db
Denoted gcd(a,b)
T integers
Two
i
a and
d b are relatively
l ti l prime
i
if gcd(a,b)
d( b) = 1
Example
`
`
PAGE
gcd(12,21)
d(12 21) = 3
gcd(15,28) = 1 [relatively prime]
33
Least Common Multiple
`
`
`
The least common multiple (LCM) of the integers a and b
is the smallest positive integer that is divisible by both a
and 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)
`
To find the gcd of 2 integers
Based on the following observation:
`
Proof:
PAGE
35
`
The Euclidean Algorithm (2)
`
`
`
To find gcd(a,b):
Let x := max(a,b), y := min(a,b)
while y ≠ 0
b i
begin
r := x mod y
x := y
y := r
end
In other words, divide the larger number x with the
smaller one y to get remainder r. Then set y to be
the bigger number, and r be the smaller number, and
repeat
p
the division until the remainder is 0
PAGE
36
Euclidean Algorithm Example
`
`
`
`
`
Find gcd(15,72)
Step 1: 72 = 4 * 15 + 12
Stepp 2: 15 = 1 * 12 + 3
Step 3: 12 = 4 * 3 + 0
Therefore gcd(15,72)
Therefore,
gcd(15 72) = 3
PAGE
37
Bringing them together
Public-key
P
bli k cryptography:
t
h Can
C
we
communicate a secret message in the open?
Yes with the discrete math we know!
Yes—with
PAGE
38
Public key: an example
Public-key:
`
`
`
`
`
`
`
`
Green = 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
Eve
Alice and Bob know 2, but Eve doesn’t!
Use it as the private key
thereafter
Magic?
Alice
PAGE
Bob
39
Public key: an example
Public-key:
`
Requirements:
`
`
`
Calculations:
`
`
`
`
p is a prime
g is a primitive root mod p (we did not discuss what this means, but
you can look up the web)
A = ga mod p
B = gb mod p
s = Ab mod p = gab mod p = gba mod p = Ba mod p
Key insight—the Discrete Logarithm Problem:
`
`
`
PAGE
Given A, g, p: difficult to calculate a.
Need big prime number p (~ several hundred digits), and big a,b
g needs not be big, e.g. 2 or 5
40