Presented by Andrew F Conn Adapted from Adam J Lee Lecture 11 Hashes PRNGs Primes GCDs and LCMs October 5 th 2016 Today s Topics Modulo Wrap Up Hash Functions Pseudorandom Number Generators ID: 723922
Download Presentation The PPT/PDF document "Discrete Structures for Computer Science" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Discrete Structures for Computer Science
Presented by: Andrew F. Conn
Adapted from: Adam J. Lee
Lecture #11: Hashes, PRNGs, Primes, GCDs, and LCMs
October 5
th
, 2016Slide2
Today’s Topics
Modulo Wrap Up
Hash Functions
Pseudorandom Number Generators
Primes & Greatest Common Divisors
Prime representations
Important theorems about primality
Greatest Common Divisors
Least Common Multiples
Euclid
’
s algorithmSlide3
Hash Functions
Sometimes it is useful to map a very LARGE number into a very small space.
Computer Memory
It is common for computers to have much less memory than the address space
Data Structures
We frequently need to store things that can be indexed based on a hash value
Checksums\Error Checking
This is a little different than a Error Correcting Code as it only tells us if something has been tampered with or notSlide4
30 people / 10 Parking Spaces?
Let’s apply this logic to a real problem. Suppose you have a company with 30 people and you have 10 parking spaces.
Let’s number the employees 1-30
And the spots 0-9
Now we are in luck because this company works 24-hous a day in 8 hour shifts.
So assign parking space 1 to employees 1, 11, and 21. Parking space 2 goes to employees 2, 12, and 22. Continue on until parking space 0 gets assigned to 10, 20, and 30.Slide5
Modulo again?
Yes!!! When you assigned the parking spaces in the previous problem you were working modulo 10.
Recall that it is “clock” arithmetic, so you just assign the parking spaces in a round
robbin
, clock like pattern.
Also, in general when working in the same modulus as your base, you can drop the higher order values
Examples:
,
Binary modulus is equivalent to binary AND when working in powers of 2!
Slide6
Ok, What’s this non-sense about Pseudorandom numbers?
What sources of random information can you think of in a computer?
What needs can you think of for random numbers in software?
Games
Decision Making
Artificial Intelligence
Cryptography
Probability\Statistical modeling
Others?Slide7
Why Pseudorandom?
Since computers rarely have sources of truly random data, we “generate” random data.
We call the devices that generate random data
Pseudorandom Number Generators
Consider the following function:
With:
Then we have:
Random Pattern
RepeatingSlide8
Prime Numbers!
Definition:
A
prime number
is a positive integer
that is divisible by only 1 and itself. If a number is not prime, it is called a
composite number
.
Intuitively:
A prime number is a number we cannot break down.Mathematically: is prime
Examples:
Are the following numbers prime or composite?
23
42
17
3
9
Prime
Composite,
Prime
Prime
Composite,
Slide9
Any positive integer can be represented as a unique product of prime numbers!
Theorem
(The Fundamental Theorem of Arithmetic)
:
Every positive integer greater than 1 can be written uniquely as a prime or the product of two or more primes where the prime factors are written in order of
nondecreasing
size.
Examples:
Note:
Proving the fundamental theorem of arithmetic requires some mathematical tools that we have not yet learned.
Slide10
This leads to a related theorem…
Theorem:
If
is a positive composite integer, then
has a prime divisor less than
.
Proof:
If
is composite, then it has a positive integer factor
with by definition. This means that
, where
is an integer greater than
.
Assume
and
. Then
, which is a contradiction. So either
or
.
Thus, if
is a positive composite number,
has a divisor less than
.
By the fundamental theorem of arithmetic, this divisor is either prime, or is a product of primes. In either case,
has a prime divisor less than
. ❏
Slide11
Applying contraposition leads to a naive primality test
Corollary:
If
is a positive integer that does not have a prime divisor less than
, then
is prime.
Example:
Is
prime?
The primes less than are
Since
is not divisible by 2,3,5, or 7, it must be prime
Example:
Is prime?The primes less than
are
, so
is not prime
Slide12
This approach can be generalized
The
Sieve of Eratosthenes
is a brute-force algorithm for finding all prime numbers less than some value
Step 1:
List the numbers less than
Step 2:
Start at 2 and remove all of its multiples
Step 3:
Pick the next available number and remove its multiples
Step 4:
Stop once the base has reached
. All remaining numbers are prime
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
245
25
26
27
2829303132333435363738394041424344454647484950515253545556575859606162636465666768697071Slide13
How many primes are there?
Theorem:
There are infinitely many prime numbers.
Proof:
By contradiction
Assume that there are only a finite number of primes
.
Let
By the fundamental theorem of arithmetic,
can be written as the product of two or more primes.
Note that no
divides
, for if
, then
also divides
and
.
Therefore, there must be some prime number not in our list. This prime number is either
(if
is prime) or a prime factor of
(if
is composite).
This is a contradiction since we assumed that all primes were listed. Therefore, there are infinitely many primes. ❏
This is a non-constructive existence proof!Slide14
Group work!
Problem 1:
What is the prime factorization of
?
Problem 2:
Is
prime? Is
prime?
Problem 3:
Is the set of all prime numbers countable or uncountable? If it is countable, show a bijection between the prime numbers and the natural numbers. Slide15
Greatest common divisors
Definition:
Let
and
be integers, not both zero. The largest integer
such that
and
is called the
greatest common divisor
of and , denoted by
.
Note:
We can (naively) find GCDs by comparing the common divisors of two numbers.
Example:
What is the GCD of 24 and 36?Factors of 24: 1, 2, 3, 4, 6, 12
Factors of 36: 1, 2, 3, 4, 6, 9, 12, 18∴ gcd(24, 36) = 12
Slide16
Sometimes, the GCD of two numbers is 1
Example:
What is
gcd
(17, 22)?
Factors of 17: 1, 17
Factors of 22: 1, 2, 11, 22
∴
gcd
(17, 22) = 1Definition: If , we say that and are relatively prime, or coprime. We say that
are
pairwise relatively prime
if
.
Example:
Are 10, 17, and 21 pairwise coprime?
Factors of 10: 1, 2, 5, 10
Factors of 17: 1, 17
Factors of 21: 1, 3, 7, 21
Yes!Slide17
We can leverage the fundamental theorem of arithmetic to develop a better algorithm
Let:
and
Then:
Example:
Compute
gcd
(120, 500)
120 = 2
3
× 3 × 5
500 = 2
2
× 5
3
So
gcd
(120, 500) = 2
2
× 3
0
× 5 = 20
Greatest multiple of
in both
and
Greatest multiple of
in both
and
Slide18
Better still is Euclid’s algorithm
Observation:
If
, then
So, let
and
. Then:
Proved in section 4.3 of the book
Slide19
Examples of Euclid’s algorithm
Example:
Compute gcd(414, 662)
662 = 414 × 1 + 248
414 = 248 × 1 + 166
248 = 166 × 1 + 82
166 = 82 × 2 + 2
82 = 2 × 41
Example:
Compute gcd(9888, 6060)9888 = 6060 × 1 + 38286060 = 3828 × 1 + 22323828 = 2232 × 1 + 15962232 = 1596 × 1 + 6361596 = 636 × 2 + 324636 = 324 × 1 + 312324 = 312 × 1 + 12312 = 12 × 26
gcd(414, 662) = 2
gcd(9888, 6060) = 12Slide20
Least common multiples
Definition:
The
least common multiple
of the integers
and
is the smallest positive integer that is divisible by both
and
. The least common multiple of
and is denoted .
Example:
What is lcm(3,12)?
Multiples of 3: 3, 6, 9, 12, 15, …
Multiples of 12: 12, 24, 36, …
So lcm(3,12) = 12Note:
is guaranteed to exist, since a common multiple exists (i.e., ).
Slide21
We can leverage the fundamental theorem of arithmetic to develop a better algorithm
Let:
and
Then:
Example:
Compute lcm(120, 500)
120 = 2
3
× 3 × 5
500 = 22 × 53So lcm(120, 500) = 23 × 3 × 53 = 3000 << 120 × 500 = 60,000
Greatest multiple of
p
1
in either a or b
Greatest multiple of
p
2
in either a or bSlide22
LCMs are closely tied to GCDs
Note:
ab
= lcm(
a
,
b
) × gcd(
a, b)Example: a = 120 = 23 × 3 × 5, b = 500 = 22 × 53120 = 23 × 3 × 5500 = 22 × 53lcm(120, 500) = 23 × 3 × 53 = 3000gcd(120, 500) = 22 × 30 × 5 = 20lcm(120, 500) × gcd(120, 500) = 23 × 3 × 53 × 22 × 30 × 5 = 25 × 3 × 5
4
= 60,000 = 120 × 500
✔Slide23
Group work!
Problem 4:
Use Euclid
’
s algorithm to compute
gcd(92928, 123552).
Problem 5:
Compute gcd(24, 36) and lcm(24, 36). Verify that gcd(24, 36) × lcm(24, 36) = 24 × 36.Slide24
Final Thoughts
Prime numbers play an important role in number theory
There are an infinite number of prime numbers
Any number can be represented as a product of prime numbers; this has implications when computing GCDs and LCMs
Next time: Proof by Induction