The Fundamentals: Algorithms The Integers
The Fundamentals: Algorithms The Integers
The Fundamentals:
Algorithms
The Integers
Objectives
Algorithms
The Growth of Functions
Complexity of Algorithms
The Integers and Division
Primes and Greatest Common Divisors
Integers and Algorithms
3.1- Algorithms
Input
Output
Definiteness
Correctness
Effectiveness
Generality
3.2. The Growth of Functions
3.2.1-Big-O Notation
Definition:
Let f and g be functions from R to R. We say that
f(x) is O(g(x))
if there are constants C and k such that
|f(x)| ≤ C|g(x)|
whenever x>k
Q2. Give as good a big-O estimate as possible for each of these functions.
a. (n^2 + 8)(n + 1) b. (n logn + n^2)(n^3 + 2)
3.3- Complexity of Algorithms
Example:
3ł7 because 7/3 is not an integer
3|12 because 12/3=4, remainder=0
Corollary 1:
a|b ^ a|c → a|(mb+nc), m, n are integers
The Division Algorithm
Theorem 2: Division Algorithm: Let a be an integer and d a positive
integer. Then there are unique integers q and r, with 0 ≤ r<d, such
that a=dq+r
d: divisor , r: remainder, q: quotient
Note: r can not be negative
Definition: a=dq+r
a: dividend d: divisor
q: quotient r : remainder,
q= a div d r = a mod d
Example:
101 is divided by 11: 101 = 11.9 + 2 q=9, r=2
-11 is divided by 3: 3(-4)+ 1 q=-4, r=1
Notations:
a ≡ b (mod m), a is congruent to b modulo m
a b (mod m), a is not congruent to b mod m
Examples:
15 is congruent to 6 modulo 3 because 3|15-6
15 is not congruent to 7 modulo 3 because 3 ł15-7
Q3. Suppose that a and b are integers, a ≡ 4 (mod 13), and b ≡ 9 (mod 13). Find the integer c
with 0 ≤ c ≤ 12 such that
a. c ≡ 9a (mod 13) b. c ≡ 11+2b (mod 13). c. c ≡ a + 2b (mod 13).
Modular Arithmetic
Theorem 3
a,b: integers, m: positive integer
a ≡ b (mod m) ↔ a mod m = b mod m
Theorem 4
a, b: integers, m: positive integer
a and b are congruent modulo m if and only if there is
an integer k such that a = b + km
Modular Arithmetic…
Theorem 5
m: positive integer
a ≡ b (mod m) ^ c ≡ d (mod m)
a+c ≡ b+d (mod m) ^ ac ≡ bd (mod m)
Corollary 2:
m : positive integer, a,b : integers
(a+b) mod m = ((a mod m) + (b mod m)) mod m
ab mod m = ((a mod m)(b mod m)) mod m
Q4. Evaluate these quantities.
a. 13 mod 3 b. −97 mod 11 c. 155 mod 19 d. −221 mod 23
Q5. Find a div m and a mod m when
a. a = −111, m = 19. b. a = −999, m = 101.
Examples:
H(064212848) mod 111= 14
H(037149212) mod 111= 65
Q7. Suppose pseudo-random numbers are produced by using: xn+1 = (5xn + 11) mod 13.
If x3= 1, find x4 and x2.
Applications of Congruences
Cryptography: letter 1 → letter 2
Examples: shift cipher with k, f(p) =(p+k) mod 26
f-1(p)=(p-k) mod 26
Sender: (encoding)
Message: “ABC” , k=3
Using f(p) = (p+3) mod 26 // 26 characters
ABC 0 1 2 3 4 5 DEF
Receiver: (decoding)
DEF 3 4 5
Using f-1(p) = (p-3) mod 26
3 4 5 0 1 2 ABC
3.5- Primes and Greatest Common
Divisors
Definition 1:
A positive integer p greater than 1 is called prime if the only
positive factors are 1 and p
A positive integer that is greater than 1 and is not prime is
called composite
Examples:
Primes: 2 3 5 7 11
Composites: 4 6 8 9
Examples:
Primes: 37
Composite: 100 = 2.2.5.5 = 2252
999 = 3.3.3.37 = 3337
Primes…
Converting a number to prime factors
Examples: 7007
Primes…
Notation: gcd(a,b)
Example: gcd(24,36)=?
Greatest Common Divisors and
Least Common Multiples
Definition 3:
The integers a, b are relatively prime if their greatest
common divisor is 1.
Example:
7 10 11 17 23 are pairwise relatively prime
7 10 11 16 24 are not pairwise relatively prime
Greatest Common Divisors and
Least Common Multiples
Definition 5:
The least common multiple of the positive integer a and b
is the smallest integer that is divisible by both a and b
Notation: lcm(a,b)
Example:
lcm(12,36) lcm(7,11)
Greatest Common Divisors and
Least Common Multiples
Theorem 5:
Let a, b be positive integers then
ab= gcd(a,b). lcm(a,b)
Representations of Integers
Algorithms for Integer Operations
Modular Exponentiation
Euclid Algorithm
Representations of Integers
Representations of Integers
Algorithm 2: Adding of Integers
Complexity: O (n)
Algorithm 3: Multiplying Integers
Complexity: O (n2)
Algorithm 4: Computing div and mod
Algorithm 4: Computing div and mod
procedure division ( a: integer; d: positive integer)
q:=0
r:= |a|
while r ≥ d {quotient= number of times of successive subtractions}
begin
r:= r-d
q := q+1
end
If a<0 and r>0 then {updating remainder when a<0}
begin
r:= d-r
q := -(q+1)
end
{ q = a div d is the quotient, r=a mod d is the remainder}
Algorithm 5: Modular Exponentiation
Algorithm 5: Modular Exponentiation