0% found this document useful (0 votes)
21 views11 pages

Lecture 6

Uploaded by

usmanashraf071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Lecture 6

Uploaded by

usmanashraf071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Modular arithmetic in CS

Modular arithmetic and congruencies are used in CS:


– Pseudorandom number generators
– Hash functions
– Cryptology

CS 441 Discrete mathematics for CS M. Hauskrecht

1
Pseudorandom number generators
• Some problems we want to program need to simulate a
random choice.
• Examples: flip of a coin, roll of a dice

We need a way to generate random outcomes


Basic problem:
– assume outcomes: 0, 1, .. N
– generate the random sequences of outcomes

• Pseudorandom number generators let us generate sequences that


look random
• Next: linear congruential method

CS 441 Discrete mathematics for CS M. Hauskrecht

Pseudorandom number generators


Linear congruential method
• We choose 4 numbers:
• the modulus m,
• multiplier a,
• increment c, and
• seed x0,
such that 2 =< a < m, 0 =< c < m, 0 =< x0 < m.

• We generate a sequence of numbers x1, x2 x3 ... xn ... such that


0 =< xn < m for all n by successively using the congruence:
• xn+1 = (a.xn + c) mod m

CS 441 Discrete mathematics for CS M. Hauskrecht

2
Pseudorandom number generators
Linear congruential method:
• xn+1 = (a.xn + c) mod m
Example:
• Assume : m=9,a=7,c=4, x0 = 3

• x1= 7*3+4 mod 9=25 mod 9 =7


• x2 = 53 mod 9 = 8
• x3 = 60 mod 9 = 6
• x4= 46 mod 9 =1
• x5 = 11 mod 9 =2
• x6 = 18 mod 9 =0
• ....
CS 441 Discrete mathematics for CS M. Hauskrecht

Hash functions
A hash function is an algorithm that maps data of arbitrary length
to data of a fixed length.
The values returned by a hash function are called hash values or
hash codes.
Example:
Hash function
John 00
01
Mary
02
Peter 03
Ann 04
..
Charles 19

CS 441 Discrete mathematics for CS M. Hauskrecht

3
Hash functions
• Problem: Given a large collection of records, how can we
store and find a record quickly?
• Solution: Use a hash function calculate the location of the
record based on the record’s ID.
• Example: A common hash function is
• h(k) = k mod n,
where n is the number of available storage locations.

0 1 2 3 4 5 6 7 8

ID: 21 21 mod 9 ID: 35 35 mod 9


=3 =8

M. Hauskrecht

Hash function
An example of a hash function that maps integers (including very
large ones) to a subset of integers 0, 1, .. m-1 is:
h(k) = k mod m

Example: Assume we have a database of employes, each with a


unique ID – a social security number that consists of 8 digits. We
want to store the records in a smaller table with m entries. Using
h(k) function we can map a social secutity number in the database
of employes to indexes in the table.
Assume: h(k) = k mod 111
Then:
h(064212848) = 064212848 mod 111 = 14
h(037149212) = 037149212 mod 111 = 65
CS 441 Discrete mathematics for CS M. Hauskrecht

4
Hash functions
• Problem: two documents mapped to the same location

0 1 2 3 4 5 6 7 8

ID: 21 21 mod 9 ID: 39 39 mod 9


=3 =3

M. Hauskrecht

Hash functions
• Solution 1: move the next available location
– Method is represented by a sequence of hash functions to
try h (k) = k mod n
0
h1(k) = (k+1) mod n

hm(k) = (k+m) mod n

0 1 2 3 4 5 6 7 8

ID: 21 21 mod 9 ID: 39 39 mod 9


=3 =3

M. Hauskrecht

5
Hash functions
• Solution 2: remember the exact location in a secondary
structure that is searched sequentially

ID: 39
Loc: 4

ID: 21
Loc: 3

0 1 2 3 4 5 6 7 8

ID: 21 21 mod 9 ID: 39 39 mod 9


=3 =3

M. Hauskrecht

Cryptology
Encryption of messages.
• Ceasar cipher:
• Shift letters in the message by 3, last three letters mapped to the
first 3 letters, e.g. A is shifted to D, X is shifted to A
How to represent the idea of a shift by 3?
• There are 26 letters in the alphabet. Assign each of them a
number from 0,1, 2, 3, .. 25 according to the alphabetical order.
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

• The encryption of the letter with an index p is represented as:


• f(p) = (p + 3) mod 26

CS 441 Discrete mathematics for CS M. Hauskrecht

6
Cryptology
Encryption of messages using a shift by 3.
• The encryption of the letter with an index p is represented as:
• f(p) = (p + 3) mod 26
Coding of letters:
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

• Encrypt message:
– I LIKE DISCRETE MATH

CS 441 Discrete mathematics for CS M. Hauskrecht

Cryptology
Encryption of messages using a shift by 3.
• The encryption of the letter with an index p is represented as:
• f(p) = (p + 3) mod 26
Coding of letters:
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

• Encrypt message:
– I LIKE DISCRETE MATH

– L 0LNH GLYFUHVH PDVK.

CS 441 Discrete mathematics for CS M. Hauskrecht

7
Cryptology
How to decode the message ?
• The encryption of the letter with an index p is represented as:
• f(p) = (p + 3) mod 26
Coding of letters:
ABCD E F G H I J K L M N O P Q R S T U Y V X W Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

• What method would you use to decode the message:


• f-1(p) = (p-3) mod 26

CS 441 Discrete mathematics for CS M. Hauskrecht

Representations of Integers
• In the modern world, we use decimal, or base 10, notation to
represent integers. For example when we write 965, we mean
9·102 + 6·101 + 5·100 .
• We can represent numbers using any base b, where b is a
positive integer greater than 1.
• The bases b = 2 (binary), b = 8 (octal) , and b= 16
(hexadecimal) are important for computing and
communications
• The ancient Mayans used base 20 and the ancient Babylonians
used base 60.

M. Hauskrecht

8
Base b Representations
• We can use positive integer b greater than 1 as a base

Theorem 1: Let b be a positive integer greater than 1. Then if n is


a positive integer, it can be expressed uniquely in the form:
n = akbk + ak-1bk-1 + …. + a1b + a0
where k is a nonnegative integer, a0,a1,…. ak are nonnegative
integers less than b, and ak≠ 0. The aj, j = 0,…,k are called the
base-b digits of the representation.

• The representation of n given in Theorem 1 is called the base


b expansion of n and is denoted by (akak-1….a1a0)b.
• We usually omit the subscript 10 for base 10 expansions.

M. Hauskrecht

Binary Expansions
Most computers represent integers and do arithmetic with
binary (base 2) expansions of integers. In these expansions,
the only digits used are 0 and 1.
Example: What is the decimal expansion of the integer that has
(1 0101 1111)2 as its binary expansion?
Solution:
(1 0101 1111)2 1∙28 0∙27 1∙26 0∙25 1∙24 1∙23
1∙22 1∙21 1∙20 351.
Example: What is the decimal expansion of the integer that has
(11011)2 as its binary expansion?
Solution: (11011)2 1 ∙24 1∙23 0∙22 1∙21 1∙20 27

M. Hauskrecht

9
Octal Expansions
The octal expansion (base 8) uses the digits {0,1,2,3,4,5,6,7}.
Example: What is the decimal expansion of the number with
octal expansion (7016)8 ?
Solution: 7∙83 0∙82 1∙81 6∙80 3598
Example: What is the decimal expansion of the number with
octal expansion (111)8 ?
Solution: 1∙82 1∙81 1∙80 64 8 1 73

M. Hauskrecht

Hexadecimal Expansions
• The hexadecimal expansion uses 16 digits:
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}.
– The letters A through F represent the decimal numbers 10
through 15.
Example: What is the decimal expansion of the number with
hexadecimal expansion (2AE0B)16 ?
Solution:
2∙164 10∙163 14∙162 0∙161 11∙160 175627
Example: What is the decimal expansion of the number with
hexadecimal expansion (E5)16 ?
Solution: 14∙161 5∙160 224 5 229

M. Hauskrecht

10
Base Conversion
To construct the base b expansion of an integer n:
– Divide n by b to obtain a quotient and remainder.
n = bq0 + a0 0 a0 b
– The remainder, a0 , is the rightmost digit in the base b
expansion of n. Next, divide q0 by b.
q0 = bq1 + a1 0 a1 b
– The remainder, a1, is the second digit from the right in the
base b expansion of n.
– Continue by successively dividing the quotients by b,
obtaining the additional base b digits as the remainder. The
process terminates when the quotient is 0.

M. Hauskrecht

Base Conversion
Example: Find the octal expansion of (12345)10
Solution: Successively dividing by 8 gives:
– 12345 = 8 · 1543 + 1
– 1543 = 8 · 192 + 7
– 192 = 8 · 24 + 0
– 24 = 8 · 3 + 0
– 3 =8·0+3
The remainders are the digits from right to left yielding
(30071)8.

M. Hauskrecht

11

You might also like