Professional Documents
Culture Documents
Mad101 Chap 3
Mad101 Chap 3
Department of Mathematics
The FPT university
Topics covered:
Topics covered:
3.1 Algorithms
Topics covered:
3.1 Algorithms
3.2 The Growth of Functions
Topics covered:
3.1 Algorithms
3.2 The Growth of Functions
3.3 Complexity of Algorithms
Topics covered:
3.1 Algorithms
3.2 The Growth of Functions
3.3 Complexity of Algorithms
3.4 The Integers and Division
Topics covered:
3.1 Algorithms
3.2 The Growth of Functions
3.3 Complexity of Algorithms
3.4 The Integers and Division
3.5 Primes and Greatest Common Divisors
Topics covered:
3.1 Algorithms
3.2 The Growth of Functions
3.3 Complexity of Algorithms
3.4 The Integers and Division
3.5 Primes and Greatest Common Divisors
3.6 Integers and Algorithms
Input:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Algorithm:
Input: n cents
Input: n cents
Output: The least number of coins using quarters (= 25 cents),
dimes (= 10 cents), nickles (= 5 cents) and pennies ( = 1 cent).
Input: n cents
Output: The least number of coins using quarters (= 25 cents),
dimes (= 10 cents), nickles (= 5 cents) and pennies ( = 1 cent).
Input: n cents
Output: The least number of coins using quarters (= 25 cents),
dimes (= 10 cents), nickles (= 5 cents) and pennies ( = 1 cent).
log n, nk , an , n!,
log n, nk , an , n!,
log n, nk , an , n!,
That means:
log n, nk , an , n!,
That means:
nk
lim =∞
n→∞ log n
log n, nk , an , n!,
That means:
nk an
lim =∞ lim =∞
n→∞ log n n→∞ nk
log n, nk , an , n!,
That means:
nk an n!
lim =∞ lim =∞ lim =∞
n→∞ log n n→∞ nk n→∞ an
log n, nk , an , n!,
That means:
nk an n!
lim =∞ lim =∞ lim =∞
n→∞ log n n→∞ nk n→∞ an
Question.
log n, nk , an , n!,
That means:
nk an n!
lim =∞ lim =∞ lim =∞
n→∞ log n n→∞ nk n→∞ an
log n, nk , an , n!,
That means:
nk an n!
lim =∞ lim =∞ lim =∞
n→∞ log n n→∞ nk n→∞ an
log n, nk , an , n!,
That means:
nk an n!
lim =∞ lim =∞ lim =∞
n→∞ log n n→∞ nk n→∞ an
log n, nk , an , n!,
That means:
nk an n!
lim =∞ lim =∞ lim =∞
n→∞ log n n→∞ nk n→∞ an
The function f (x) is called big-O of g (x), write f (x) is O(g (x)), if there
is a constant C such that
The function f (x) is called big-O of g (x), write f (x) is O(g (x)), if there
is a constant C such that
|f (x)| ≤ C |g (x)|
The function f (x) is called big-O of g (x), write f (x) is O(g (x)), if there
is a constant C such that
|f (x)| ≤ C |g (x)|
for all x large enough, (meaning, it is true for all x > k for some k).
The function f (x) is called big-O of g (x), write f (x) is O(g (x)), if there
is a constant C such that
|f (x)| ≤ C |g (x)|
for all x large enough, (meaning, it is true for all x > k for some k).
Example.
The function f (x) is called big-O of g (x), write f (x) is O(g (x)), if there
is a constant C such that
|f (x)| ≤ C |g (x)|
for all x large enough, (meaning, it is true for all x > k for some k).
Example.
The function f (x) is called big-O of g (x), write f (x) is O(g (x)), if there
is a constant C such that
|f (x)| ≤ C |g (x)|
for all x large enough, (meaning, it is true for all x > k for some k).
Example.
Theorem 2
Theorem 2
Assume that f1 (x) is O(g1 (x)) and f2 (x) is O(g2 (x)). Then:
Theorem 2
Assume that f1 (x) is O(g1 (x)) and f2 (x) is O(g2 (x)). Then:
f1 (x) + f2 (x) is big-O of the maximum of g1 (x) and g2 (x).
Theorem 2
Assume that f1 (x) is O(g1 (x)) and f2 (x) is O(g2 (x)). Then:
f1 (x) + f2 (x) is big-O of the maximum of g1 (x) and g2 (x).
f1 (x)f2 (x) is big-O of g1 (x)g2 (x).
Theorem 2
Assume that f1 (x) is O(g1 (x)) and f2 (x) is O(g2 (x)). Then:
f1 (x) + f2 (x) is big-O of the maximum of g1 (x) and g2 (x).
f1 (x)f2 (x) is big-O of g1 (x)g2 (x).
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words,
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words, f (x) is Θ(g (x)) if there are constants C1 , C2 > 0 such that
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words, f (x) is Θ(g (x)) if there are constants C1 , C2 > 0 such that
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words, f (x) is Θ(g (x)) if there are constants C1 , C2 > 0 such that
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words, f (x) is Θ(g (x)) if there are constants C1 , C2 > 0 such that
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words, f (x) is Θ(g (x)) if there are constants C1 , C2 > 0 such that
The function f (x) is called big-theta of g (x), write: f (x) is Θ(g (x)), if
f (x) is O(g (x)) and g (x) is O(f (x)).
In other words, f (x) is Θ(g (x)) if there are constants C1 , C2 > 0 such that
Note: is f (x) is Θ(g (x)) and g (x) is also Θ(f (x)). Therefore, if f (x) is
Θ(g (x)) we say f (x) is of order g (x).
Space complexity:
Number of loops: n − 1
Number of loops: n − 1
Number of comparisons in
each loop: 2
Number of loops: n − 1
Number of comparisons in
each loop: 2
Number of comparisons to
exit the loop: 1
Number of loops: n − 1
Number of comparisons in
each loop: 2
Number of comparisons to
exit the loop: 1
Total number of comparisons:
2(n − 1) + 1 = 2n − 1 = O(n)
n loops
n loops
2 comparisons in each loop
n loops
2 comparisons in each loop
1 comparison to exit the loop
n loops
2 comparisons in each loop
1 comparison to exit the loop
1 comparison outside the loop
n loops
2 comparisons in each loop
1 comparison to exit the loop
1 comparison outside the loop
Total number of comparisons:
2n + 1 + 1 = 2n + 2 = O(n)
i − 1 loops
i − 1 loops
2 comparisons in each loop
i − 1 loops
2 comparisons in each loop
2 comparisons to exit the loop
i − 1 loops
2 comparisons in each loop
2 comparisons to exit the loop
1 outside the loop
i − 1 loops
2 comparisons in each loop
2 comparisons to exit the loop
1 outside the loop
Total number of comparisons:
2(i − 1) + 2 + 1 = 2i + 1
Assume n = 2k
Assume n = 2k
k loops
Assume n = 2k
k loops
2 comparisons in each
loop
Assume n = 2k
k loops
2 comparisons in each
loop
1 comparison to exit
the loop
Assume n = 2k
k loops
2 comparisons in each
loop
1 comparison to exit
the loop
1 comparison outside
the loop
Assume n = 2k
k loops
2 comparisons in each
loop
1 comparison to exit
the loop
1 comparison outside
the loop
Total number of
comparisons:
2k + 1 + 1 = 2k + 2
= 2dlog ne + 2
= O(log n)
b is divisible by a
b is divisible by a
b is a multiple of a
b is divisible by a
b is a multiple of a
a is a factor of b
b is divisible by a
b is a multiple of a
a is a factor of b
a|b
b is divisible by a
b is a multiple of a
a is a factor of b
a|b
Theorem
b is divisible by a
b is a multiple of a
a is a factor of b
a|b
Theorem
Let a, b, c be integers. Then:
b is divisible by a
b is a multiple of a
a is a factor of b
a|b
Theorem
Let a, b, c be integers. Then:
If a | b and a | c then a | (b + c)
b is divisible by a
b is a multiple of a
a is a factor of b
a|b
Theorem
Let a, b, c be integers. Then:
If a | b and a | c then a | (b + c)
If a | b then a | bc for all c
b is divisible by a
b is a multiple of a
a is a factor of b
a|b
Theorem
Let a, b, c be integers. Then:
If a | b and a | c then a | (b + c)
If a | b then a | bc for all c
If a | b and b | c then a | c
TrungDT (FUHN) MAD101 Chapter 3 31 / 45
The Division Algorithm
q = a div d, r = a mod d
q = a div d, r = a mod d
q = a div d, r = a mod d
q = a div d, r = a mod d
Some properties
Some properties
Some properties
Pseudorandom numbers
Pseudorandom numbers
Pseudorandom numbers
Pseudorandom numbers
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
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Caesar’s cipher
f (p) = p + 3 mod 26
Theorem
Theorem
There are infinitely many primes.
Theorem
There are infinitely many primes.
ab = gcd(a, b) · lcm(a, b)
ab = gcd(a, b) · lcm(a, b)
Relatively prime
Two integers a, b are called relatively prime if gcd(a, b) = 1.
ab = gcd(a, b) · lcm(a, b)
Relatively prime
Two integers a, b are called relatively prime if gcd(a, b) = 1.
A set of integers are called pairwise relatively prime if any two
integers in the set are relatively prime.
ab = gcd(a, b) · lcm(a, b)
Relatively prime
Two integers a, b are called relatively prime if gcd(a, b) = 1.
A set of integers are called pairwise relatively prime if any two
integers in the set are relatively prime.
ab = gcd(a, b) · lcm(a, b)
Relatively prime
Two integers a, b are called relatively prime if gcd(a, b) = 1.
A set of integers are called pairwise relatively prime if any two
integers in the set are relatively prime.
ab = gcd(a, b) · lcm(a, b)
Relatively prime
Two integers a, b are called relatively prime if gcd(a, b) = 1.
A set of integers are called pairwise relatively prime if any two
integers in the set are relatively prime.
Representations of Integers
Representations of Integers
Let b be an integer greater than 1. Let n be a positive integer. Then n
can be expressed uniquely in the form
n = ak b k + ak−1 b k−1 + · · · + a0 ,
Representations of Integers
Let b be an integer greater than 1. Let n be a positive integer. Then n
can be expressed uniquely in the form
n = ak b k + ak−1 b k−1 + · · · + a0 ,
Representations of Integers
Let b be an integer greater than 1. Let n be a positive integer. Then n
can be expressed uniquely in the form
n = ak b k + ak−1 b k−1 + · · · + a0 ,
Binary expansion: 0, 1
Binary expansion: 0, 1
Octal expansion: 0, 1, 2,..., 7
Binary expansion: 0, 1
Octal expansion: 0, 1, 2,..., 7
Hexadecimal expansion: 0, 1, 2,..., 9, A, B, C, D, E, F
Binary expansion: 0, 1
Octal expansion: 0, 1, 2,..., 7
Hexadecimal expansion: 0, 1, 2,..., 9, A, B, C, D, E, F
Example.
Binary expansion: 0, 1
Octal expansion: 0, 1, 2,..., 7
Hexadecimal expansion: 0, 1, 2,..., 9, A, B, C, D, E, F
Example.
Binary expansion: 0, 1
Octal expansion: 0, 1, 2,..., 7
Hexadecimal expansion: 0, 1, 2,..., 9, A, B, C, D, E, F
Example.
Binary expansion: 0, 1
Octal expansion: 0, 1, 2,..., 7
Hexadecimal expansion: 0, 1, 2,..., 9, A, B, C, D, E, F
Example.
Decimal 0 1 2 3 4 5 6 7
Hexadecimal 0 1 2 3 4 5 6 7
Octal 0 1 2 3 4 5 6 7
Binary 0 1 10 11 100 101 110 111
Decimal 8 9 10 11 12 13 14 15
Hexadecimal 8 9 A B C D E F
Octal 10 11 12 13 14 15 16 17
Binary 1000 1001 1010 1011 1100 1101 1110 1111
Addition algorithm.
Addition algorithm.
Theorem
Theorem
Let a > b be positive integers. Put r = a mod b. Then
gcd(a, b) = gcd(b, r )
Theorem
Let a > b be positive integers. Put r = a mod b. Then
gcd(a, b) = gcd(b, r )
Print(x)
Problem:
Print(x)