Number Theory
Number Theory
Basics
Number Theory
Combinatorics
Game Theory
2
Basics
Computer Science ⊂ Mathematics
4
• Usually at least one problem that involves solving mathematically.
• Problems often require mathematical analysis to be solved efficiently.
• Using a bit of math before coding can also shorten and simplify code.
5
Finding patterns and formulas
6
Finding patterns and formulas
6
Finding patterns and formulas
6
Finding patterns and formulas
6
Finding patterns and formulas
6
Finding patterns and formulas
6
Arithmetic progression
7
Arithmetic progression
an = an−1 + c
7
Arithmetic progression
an = a1 + (n − 1)c
n(a1 + an )
Sn =
2
8
Arithmetic progression
an = a1 + (n − 1)c
n(a1 + an )
Sn =
2
8
Geometric progression
9
Geometric progression
• More generally
a, ar , ar 2 , ar 3 , ar 4 , ar 5 , ar 6 , . . .
an = ar n−1
9
Geometric progression
10
Geometric progression
10
Little bit about logarithm
11
Little bit about logarithm
11
Little bit about logarithm
11
Little bit about logarithm
11
Example
• For example, what is the first power of 17 that has k digits in base
b?
12
Example
• For example, what is the first power of 17 that has k digits in base
b?
• Naive solution: Iterate over powers of 17 and count the number of
digits.
12
Example
• For example, what is the first power of 17 that has k digits in base
b?
• Naive solution: Iterate over powers of 17 and count the number of
digits.
• But the powers of 17 grow exponentially!
12
Example
• For example, what is the first power of 17 that has k digits in base
b?
• Naive solution: Iterate over powers of 17 and count the number of
digits.
• But the powers of 17 grow exponentially!
12
Example
13
Example
13
Example
13
Example
• We still have to iterate over the powers of 17, but we can do that in
log
ln(17x−1 · 17) = ln(17x−1 ) + ln(17)
14
Example
• We still have to iterate over the powers of 17, but we can do that in
log
ln(17x−1 · 17) = ln(17x−1 ) + ln(17)
• More generally
logb (xy ) = logb (x) + logb (y )
• For division
x
logb ( ) = logb (x) − logb (y )
y
14
Example
15
Example
15
Example
15
Base conversion
• Speaking of bases.
16
Base conversion
• Speaking of bases.
• What if we actually need to use base conversion?
16
Base conversion
• Speaking of bases.
• What if we actually need to use base conversion?
• Simple algorithm
vector<int> toBase(int base, int val) {
vector<int> res;
while(val) {
res.push_back(val % base);
val /= base;
}
return val;
• Starts from the 0-th digit, and calculates the multiple of each power.
16
Working with doubles
17
Working with doubles
17
Working with doubles
17
Working with doubles
17
Working with doubles
18
Working with doubles
19
Working with doubles
19
Number Theory
Modular arithmetic
21
Modular arithmetic
21
Modular arithmetic
21
Modular arithmetic
21
Modular arithmetic
• Multiplication
• Exponentiation
(a mod n)b = (ab mod n)
• Note: We are only working with integers.
22
Modular arithmetic
23
Modular arithmetic
23
Modular arithmetic
23
Modular arithmetic
23
Modular arithmetic
24
Modular arithmetic
24
Modular arithmetic
24
Modular arithmetic
25
Extended Euclidean algorithm
26
Extended Euclidean algorithm
26
Extended Euclidean algorithm
27
Extended Euclidean algorithm
27
Extended Euclidean algorithm
28
Applications
29
Primality testing
30
Primality testing
30
Primality testing
30
Primality testing
30
Primality testing
31
Prime sieves
32
Prime sieves
32
Prime sieves
32
Prime sieves
32
Prime sieves
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13, 17,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13, 17, 19,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves
2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13, 17, 19, 23
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Sieve of Eratosthenes
34
Integer factorization
34
Integer factorization
34
Integer factorization
√
• Use the sieve of Eratosthenes to generate all the primes up n
• Iterate over all the primes generated and check if they divide n, and
determine the largest power that divides n.
34
map<int, int> factor(int N) {
vector<int> primes;
primes = eratosthenes(static_cast<int>(sqrt(N+1)));
map<int, int> factors;
for(int i = 0; i < primes.size(); ++i){
int prime = primes[i], power = 0;
while(N % prime == 0){
power++;
N /= prime;
}
if(power > 0){
factors[prime] = power;
}
}
if (N > 1) {
factors[N] = 1;
}
return factors;
}
35
Integer factorization
36
Integer factorization
36
Integer factorization
36
Integer factorization
37
Integer factorization
aφ(n) = 1 (mod n)
37
Combinatorics
Combinatorics
39
Combinatorics
39
Basic counting
• Factorial
n! = 1 · 2 · 3 · · · n
• Binomial coefficient
n n!
=
k k!(n − k)!
40
Basic counting
• Factorial
n! = 1 · 2 · 3 · · · n
• Binomial coefficient
n n!
=
k k!(n − k)!
Number of ways to choose k objects from a set of n objects,
ignoring order.
40
Basic counting
Properties
•
n n
=
k n−k
•
n n
= =1
0 n
•
n n−1 n−1
= +
k k −1 k
41
Basic counting
Pascal triangle!
0
0
1 1
0 1
2 2 2
0 1 2
3 3 3 3
0 1 2 3
4 4 4 4 4
0 1 2 3 4
5 5 5 5 5 5
0 1 2 3 4 5
6 6 6 6 6 6 6
0 1 2 3 4 5 6
7 7 7 7 7 7 7 7
0 1 2 3 4 5 6 7
8 8 8 8 8 8 8 8 8
0 1 2 3 4 5 6 7 8
9 9 9 9 9 9 9 9 9 9
0 1 2 3 4 5 6 7 8 9
10 10 10 10 10 10 10 10 10 10 10
0 1 2 3 4 5 6 7 8 9 10
42
Example
43
Example
43
Example
• 2 vertical
43
Example
• 2 vertical
• 2 horizontal
43
Example
• 2 vertical
• 2 horizontal
43
Example
• 2 vertical
• 2 horizontal
• Number of ways we can choose 2 vertical lines
n
2
43
Example
• 2 vertical
• 2 horizontal
• Number of ways we can choose 2 horizontal lines
m
2
43
Example
• 2 vertical
• 2 horizontal
• Total number of ways we can form a rectangle
n m n!m!
=
2 2 (n − 2)!(m − 2)!2!2!
n(n − 1)m(m − 1)
=
4
43
Multinomial
44
Multinomial
44
Multinomial
44
Example
How many different ways can we divide k identical balls into n boxes?
45
Example
How many different ways can we divide k identical balls into n boxes?
x1 + x2 + . . . + xn = k
45
Example
How many different ways can we divide k identical balls into n boxes?
x1 + x2 + . . . + xn = k
45
Example
1...101...10...01...1
46
Example
|1 .{z
. . 1} 0 |1 .{z
. . 1} 0 . . . 0 |1 .{z
. . 1}
x1 x2 xn
46
Example
|1 .{z
. . 1} 0 |1 .{z
. . 1} 0 . . . 0 |1 .{z
. . 1}
x1 x2 xn
46
Example
How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
(0, 0)
47
Example
How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
1
(0, 0)
1 1
47
Example
How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
1
(0, 0)
1 1
47
Example
How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
47
Example
How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
• There is 1 path to (0, 0)
• There is 1 path to (1, 0) and (0, 1)
47
Example
How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
1 6 21 56 126 252 462 • There is 1 path to (0, 0)
1 5 15 35 70 126 210 • There is 1 path to (1, 0) and (0, 1)
47
Catalan
(n, m)
(0, 0)
48
Catalan
(0, 0)
48
Catalan
48
Catalan
49
Catalan
49
Catalan
49
Catalan
49
Catalan
49
Fibonacci
f1 = 1
f2 = 1
fn = fn−1 + fn−2
50
Fibonacci
f1 = 1
f2 = 1
fn = fn−1 + fn−2
50
Fibonacci
f1 = 1
f2 = 1
fn = fn−1 + fn−2
50
Fibonacci as matrix
51
Fibonacci as matrix
Or simply as a matrix
!n !
1 1 fn+1 fn
=
1 0 fn fn−1
51
Fibonacci as matrix
Or simply as a matrix
!n !
1 1 fn+1 fn
=
1 0 fn fn−1
51
Fibonacci as matrix
52
Fibonacci as matrix
52
Game Theory
Game theory
54
Game theory
54
Example
55
Example
55
Example
56
Example
0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P
56
Example
0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N
56
Example
0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N P
56
Example
0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N P N N N
56
Example
0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N P N N N P N N N P ...
56
Game theory
57
Game theory
57
Game theory
57
Game theory
57
The game called Nim
58
The game called Nim
59
The game called Nim
59
The game called Nim
59
The game called Nim
59
The game called Nim
59