Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og...

167
Mathematics Tómas Ken Magnússon Bjarki Ágúst Guðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Transcript of Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og...

Page 1: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Mathematics

Tómas Ken MagnússonBjarki Ágúst Guðmundsson

Árangursrík forritun og lausn verkefna

School of Computer Science

Reykjavík University

Page 2: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Today we’re going to cover

Basics

Number Theory

Combinatorics

Game Theory

2

Page 3: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Basics

Page 4: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Computer Science ⊂ Mathematics

4

Page 5: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

• 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

Page 6: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Finding patterns and formulas

• Some problems have solutions that form a pattern.

• By finding the pattern, we solve the problem.

• Could be classified as mathematical ad-hoc problem.

• Requires mathematical intuition.

• Useful tricks:• Solve some small instances by hand.• See if the solutions form a pattern.

• Does the pattern involve some overlapping subproblem?We might need to use DP.

• Knowing reoccurring identities and sequences can be helpful.

6

Page 7: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Finding patterns and formulas

• Some problems have solutions that form a pattern.

• By finding the pattern, we solve the problem.

• Could be classified as mathematical ad-hoc problem.

• Requires mathematical intuition.

• Useful tricks:• Solve some small instances by hand.• See if the solutions form a pattern.

• Does the pattern involve some overlapping subproblem?We might need to use DP.

• Knowing reoccurring identities and sequences can be helpful.

6

Page 8: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Finding patterns and formulas

• Some problems have solutions that form a pattern.

• By finding the pattern, we solve the problem.

• Could be classified as mathematical ad-hoc problem.

• Requires mathematical intuition.

• Useful tricks:• Solve some small instances by hand.• See if the solutions form a pattern.

• Does the pattern involve some overlapping subproblem?We might need to use DP.

• Knowing reoccurring identities and sequences can be helpful.

6

Page 9: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Finding patterns and formulas

• Some problems have solutions that form a pattern.

• By finding the pattern, we solve the problem.

• Could be classified as mathematical ad-hoc problem.

• Requires mathematical intuition.

• Useful tricks:• Solve some small instances by hand.• See if the solutions form a pattern.

• Does the pattern involve some overlapping subproblem?

We might need to use DP.

• Knowing reoccurring identities and sequences can be helpful.

6

Page 10: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Finding patterns and formulas

• Some problems have solutions that form a pattern.

• By finding the pattern, we solve the problem.

• Could be classified as mathematical ad-hoc problem.

• Requires mathematical intuition.

• Useful tricks:• Solve some small instances by hand.• See if the solutions form a pattern.

• Does the pattern involve some overlapping subproblem?We might need to use DP.

• Knowing reoccurring identities and sequences can be helpful.

6

Page 11: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Finding patterns and formulas

• Some problems have solutions that form a pattern.

• By finding the pattern, we solve the problem.

• Could be classified as mathematical ad-hoc problem.

• Requires mathematical intuition.

• Useful tricks:• Solve some small instances by hand.• See if the solutions form a pattern.

• Does the pattern involve some overlapping subproblem?We might need to use DP.

• Knowing reoccurring identities and sequences can be helpful.

6

Page 12: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Arithmetic progression

• Often we see a pattern like

2, 5, 8, 11, 14, 17, 20, . . .

• This is called a arithmetic progression.

an = an−1 + c

7

Page 13: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Arithmetic progression

• Often we see a pattern like

2, 5, 8, 11, 14, 17, 20, . . .

• This is called a arithmetic progression.

an = an−1 + c

7

Page 14: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Arithmetic progression

• Depending on the situation we may want to get the n-th element

an = a1 + (n − 1)c

• Or the sum over a finite portion of the progression

Sn =n(a1 + an)

2

• Remember this one?

1+ 2+ 3+ 4+ 5+ . . .+ n =n(n + 1)

2

8

Page 15: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Arithmetic progression

• Depending on the situation we may want to get the n-th element

an = a1 + (n − 1)c

• Or the sum over a finite portion of the progression

Sn =n(a1 + an)

2

• Remember this one?

1+ 2+ 3+ 4+ 5+ . . .+ n =n(n + 1)

2

8

Page 16: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Geometric progression

• Other types of pattern we often see are geometric progressions

1, 2, 4, 8, 16, 32, 64, 128, . . .

• More generally

a, ar , ar2, ar3, ar4, ar5, ar6, . . .

an = arn−1

9

Page 17: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Geometric progression

• Other types of pattern we often see are geometric progressions

1, 2, 4, 8, 16, 32, 64, 128, . . .

• More generally

a, ar , ar2, ar3, ar4, ar5, ar6, . . .

an = arn−1

9

Page 18: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Geometric progression

• Sum over a finite portion

n∑i=0

ar i =a(1− rn)

(1− r)

• Or from the m-th element to the n-thn∑

i=m

ar i =a(rm − rn+1)

(1− r)

10

Page 19: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Geometric progression

• Sum over a finite portion

n∑i=0

ar i =a(1− rn)

(1− r)

• Or from the m-th element to the n-thn∑

i=m

ar i =a(rm − rn+1)

(1− r)

10

Page 20: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficientalternative.

• In both C++(<cmath>) and Java(java.lang.Math) we have thenatural logarithm

double log(double x);

and logarithm in base 10

double log10(double x);

• And also the exponential

double exp(double x);

11

Page 21: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficientalternative.

• In both C++(<cmath>) and Java(java.lang.Math) we have thenatural logarithm

double log(double x);

and logarithm in base 10

double log10(double x);

• And also the exponential

double exp(double x);

11

Page 22: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficientalternative.

• In both C++(<cmath>) and Java(java.lang.Math) we have thenatural logarithm

double log(double x);

and logarithm in base 10

double log10(double x);

• And also the exponential

double exp(double x);

11

Page 23: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficientalternative.

• In both C++(<cmath>) and Java(java.lang.Math) we have thenatural logarithm

double log(double x);

and logarithm in base 10

double log10(double x);

• And also the exponential

double exp(double x);

11

Page 24: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• For example, what is the first power of 17 that has k digits in baseb?

• Naive solution: Iterate over powers of 17 and count the number ofdigits.

• But the powers of 17 grow exponentially!

1716 > 264

• What if k = 500 (∼ 1.7 · 10615), or something larger?

• Impossible to work with the numbers in a normal fashion.

• Why not log?

12

Page 25: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• For example, what is the first power of 17 that has k digits in baseb?

• Naive solution: Iterate over powers of 17 and count the number ofdigits.

• But the powers of 17 grow exponentially!

1716 > 264

• What if k = 500 (∼ 1.7 · 10615), or something larger?

• Impossible to work with the numbers in a normal fashion.

• Why not log?

12

Page 26: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• For example, what is the first power of 17 that has k digits in baseb?

• Naive solution: Iterate over powers of 17 and count the number ofdigits.

• But the powers of 17 grow exponentially!

1716 > 264

• What if k = 500 (∼ 1.7 · 10615), or something larger?

• Impossible to work with the numbers in a normal fashion.

• Why not log?

12

Page 27: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• For example, what is the first power of 17 that has k digits in baseb?

• Naive solution: Iterate over powers of 17 and count the number ofdigits.

• But the powers of 17 grow exponentially!

1716 > 264

• What if k = 500 (∼ 1.7 · 10615), or something larger?

• Impossible to work with the numbers in a normal fashion.

• Why not log?

12

Page 28: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• Remember, we can calculate the length of a number n in base b

with blogb(n)c+ 1.

• But how do we do this with only ln or log10?

• Change base!

logb(a) =logd(a)logd(b)

=ln(a)ln(b)

• Now we can at least count the length without converting bases

13

Page 29: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• Remember, we can calculate the length of a number n in base b

with blogb(n)c+ 1.

• But how do we do this with only ln or log10?

• Change base!

logb(a) =logd(a)logd(b)

=ln(a)ln(b)

• Now we can at least count the length without converting bases

13

Page 30: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• Remember, we can calculate the length of a number n in base b

with blogb(n)c+ 1.

• But how do we do this with only ln or log10?

• Change base!

logb(a) =logd(a)logd(b)

=ln(a)ln(b)

• Now we can at least count the length without converting bases

13

Page 31: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• We still have to iterate over the powers of 17, but we can do that inlog

ln(17x−1 · 17) = ln(17x−1) + ln(17)

• More generallylogb(xy) = logb(x) + logb(y)

• For divisionlogb(

x

y) = logb(x)− logb(y)

14

Page 32: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• We still have to iterate over the powers of 17, but we can do that inlog

ln(17x−1 · 17) = ln(17x−1) + ln(17)

• More generallylogb(xy) = logb(x) + logb(y)

• For divisionlogb(

x

y) = logb(x)− logb(y)

14

Page 33: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• We can simplify this even more.

• The solution to our problem is in mathematical terms, finding the x

forlogb(17

x) = k − 1

• One more handy identity

logb(ac) = c · logb(a)

• Using this identity and the ones we’ve covered, we get

x =

⌈(k − 1) · ln(10)

ln(17)

15

Page 34: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• We can simplify this even more.

• The solution to our problem is in mathematical terms, finding the x

forlogb(17

x) = k − 1

• One more handy identity

logb(ac) = c · logb(a)

• Using this identity and the ones we’ve covered, we get

x =

⌈(k − 1) · ln(10)

ln(17)

15

Page 35: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• We can simplify this even more.

• The solution to our problem is in mathematical terms, finding the x

forlogb(17

x) = k − 1

• One more handy identity

logb(ac) = c · logb(a)

• Using this identity and the ones we’ve covered, we get

x =

⌈(k − 1) · ln(10)

ln(17)

⌉15

Page 36: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

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

Page 37: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

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

Page 38: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

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

Page 39: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• Comparing doubles, sounds like a bad idea.

• What else can we do if we are working with real numbers?

• We compare them to a certain degree of precision.

• Two numbers are deemed equal of their difference is less than somesmall epsilon.

const double EPS = 1e-9;

if (abs(a - b) < EPS) {...}

17

Page 40: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• Comparing doubles, sounds like a bad idea.

• What else can we do if we are working with real numbers?

• We compare them to a certain degree of precision.

• Two numbers are deemed equal of their difference is less than somesmall epsilon.

const double EPS = 1e-9;

if (abs(a - b) < EPS) {...}

17

Page 41: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• Comparing doubles, sounds like a bad idea.

• What else can we do if we are working with real numbers?

• We compare them to a certain degree of precision.

• Two numbers are deemed equal of their difference is less than somesmall epsilon.

const double EPS = 1e-9;

if (abs(a - b) < EPS) {...}

17

Page 42: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• Comparing doubles, sounds like a bad idea.

• What else can we do if we are working with real numbers?

• We compare them to a certain degree of precision.

• Two numbers are deemed equal of their difference is less than somesmall epsilon.

const double EPS = 1e-9;

if (abs(a - b) < EPS) {...}

17

Page 43: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• Less than operator:

if (a < b - EPS) {...}

• Less than or equal:

if (a < b + EPS) {...}

• The rest of the operators follow.

18

Page 44: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• This allows us to use comparison based algorithms.

• For example std::set<double>.

struct cmp {bool operator(){double a, double b}{

return a < b - EPS;}

};

set<double, cmp> doubleSet();

• Other STL containers can be used in similar fashion.

19

Page 45: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Working with doubles

• This allows us to use comparison based algorithms.

• For example std::set<double>.

struct cmp {bool operator(){double a, double b}{

return a < b - EPS;}

};

set<double, cmp> doubleSet();

• Other STL containers can be used in similar fashion.

19

Page 46: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Number Theory

Page 47: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integersmodulo n.

• The integers, modulo some n form a structure called a ring.

• Special rules apply, also loads of interesting properties.

21

Page 48: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integersmodulo n.

• The integers, modulo some n form a structure called a ring.

• Special rules apply, also loads of interesting properties.

21

Page 49: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integersmodulo n.

• The integers, modulo some n form a structure called a ring.

• Special rules apply, also loads of interesting properties.

21

Page 50: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integersmodulo n.

• The integers, modulo some n form a structure called a ring.

• Special rules apply, also loads of interesting properties.

21

Page 51: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

Some of the allowed operations:

• Addition and subtraction modulo n

(a mod n) + (b mod n) = (a+ b mod n)

(a mod n)− (b mod n) = (a− b mod n)

• Multiplication

(a mod n)(b mod n) = (ab mod n)

• Exponentiation(a mod n)b = (ab mod n)

• Note: We are only working with integers.

22

Page 52: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about division?

NO!

• We could end up with a fraction!

• Division with k equals multiplication with the multiplicative inverseof k.

• The multiplicative inverse of an integer a, is the element a−1 suchthat

a · a−1 = 1 (mod n)

23

Page 53: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about division? NO!

• We could end up with a fraction!

• Division with k equals multiplication with the multiplicative inverseof k.

• The multiplicative inverse of an integer a, is the element a−1 suchthat

a · a−1 = 1 (mod n)

23

Page 54: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about division? NO!

• We could end up with a fraction!

• Division with k equals multiplication with the multiplicative inverseof k .

• The multiplicative inverse of an integer a, is the element a−1 suchthat

a · a−1 = 1 (mod n)

23

Page 55: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about division? NO!

• We could end up with a fraction!

• Division with k equals multiplication with the multiplicative inverseof k .

• The multiplicative inverse of an integer a, is the element a−1 suchthat

a · a−1 = 1 (mod n)

23

Page 56: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about logarithm?

YES!• But difficult.• Basis for some cryptography such as elliptic curve, Diffie-Hellmann.

• Google “Discrete Logarithm” if you want to know more.

24

Page 57: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about logarithm? YES!• But difficult.

• Basis for some cryptography such as elliptic curve, Diffie-Hellmann.

• Google “Discrete Logarithm” if you want to know more.

24

Page 58: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• What about logarithm? YES!• But difficult.• Basis for some cryptography such as elliptic curve, Diffie-Hellmann.

• Google “Discrete Logarithm” if you want to know more.

24

Page 59: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Modular arithmetic

• Prime number is a positive integer greater than 1 that has nopositive divisor other than 1 and itself.

• Greatest Common Divisor of two integers a and b is the largestnumber that divides both a and b.

• Least Common Multiple of two integers a and b is the smallestinteger that both a and b divide.

• Prime factor of an positive integer is a prime number that divides it.

• Prime factorization is the decomposition of an integer into its primefactors. By the fundamental theorem of arithmetics, every integergreater than 1 has a unique prime factorization.

25

Page 60: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Extended Euclidean algorithm

• The Euclidean algorithm is a recursive algorithm that computes theGCD of two numbers.

int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);

}

• Runs in O(log2 N).

• Notice that this can also compute LCM

lcm(a, b) =a · b

gcd(a, b)

• See Wikipedia to see how it works and for proofs.

26

Page 61: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Extended Euclidean algorithm

• The Euclidean algorithm is a recursive algorithm that computes theGCD of two numbers.

int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);

}

• Runs in O(log2 N).

• Notice that this can also compute LCM

lcm(a, b) =a · b

gcd(a, b)

• See Wikipedia to see how it works and for proofs.

26

Page 62: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Extended Euclidean algorithm

• Reversing the steps of the Euclidean algorithm we get the Bézout’sidentity

gcd(a, b) = ax + by

which simply states that there always exist x and y such that theequation above holds.

• The extended Euclidean algorithm computes the GCD and thecoefficients x and y .

• Each iteration it add up how much of b we subtracted from a andvice versa.

27

Page 63: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Extended Euclidean algorithm

• Reversing the steps of the Euclidean algorithm we get the Bézout’sidentity

gcd(a, b) = ax + by

which simply states that there always exist x and y such that theequation above holds.

• The extended Euclidean algorithm computes the GCD and thecoefficients x and y .

• Each iteration it add up how much of b we subtracted from a andvice versa.

27

Page 64: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Extended Euclidean algorithm

int egcd(int a, int b, int& x, int& y) {if (b == 0) {

x = 1;y = 0;return a;

} else {int d = egcd(b, a % b, x, y);x -= a / b * y;swap(x, y);return d;

}}

28

Page 65: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Applications

• Essential step in the RSA algorithm.

• Essential step in many factorization algorithms.

• Can be generalized to other algebraic structures.

• Fundamental tool for proofs in number theory.

• Many other algorithms for GCD

29

Page 66: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Primality testing

• How do we determine if a number n is a prime?

• Naive method: Iterate over all 1 < i < n and check it i | n.• O(N)

• Better: If n is not a prime, it has a divisor ≤√n.

• Iterate up to√n instead.

• O(√N)

• Even better: If n is not a prime, it has a prime divisor ≤√n

• Iterate over the prime numbers up to√n.

• There are ∼ N/ ln(N) primes less N, therefore O(√N/ logN).

30

Page 67: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Primality testing

• How do we determine if a number n is a prime?

• Naive method: Iterate over all 1 < i < n and check it i | n.• O(N)

• Better: If n is not a prime, it has a divisor ≤√n.

• Iterate up to√n instead.

• O(√N)

• Even better: If n is not a prime, it has a prime divisor ≤√n

• Iterate over the prime numbers up to√n.

• There are ∼ N/ ln(N) primes less N, therefore O(√N/ logN).

30

Page 68: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Primality testing

• How do we determine if a number n is a prime?

• Naive method: Iterate over all 1 < i < n and check it i | n.• O(N)

• Better: If n is not a prime, it has a divisor ≤√n.

• Iterate up to√n instead.

• O(√N)

• Even better: If n is not a prime, it has a prime divisor ≤√n

• Iterate over the prime numbers up to√n.

• There are ∼ N/ ln(N) primes less N, therefore O(√N/ logN).

30

Page 69: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Primality testing

• How do we determine if a number n is a prime?

• Naive method: Iterate over all 1 < i < n and check it i | n.• O(N)

• Better: If n is not a prime, it has a divisor ≤√n.

• Iterate up to√n instead.

• O(√N)

• Even better: If n is not a prime, it has a prime divisor ≤√n

• Iterate over the prime numbers up to√n.

• There are ∼ N/ ln(N) primes less N, therefore O(√N/ logN).

30

Page 70: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Primality testing

• Trial division is a deterministic primality test.

• Many algorithms that are probabilistic or randomized.

• Fermat test; uses Fermat’s little theorem.

• Probabilistic algorithms that can only prove that a number iscomposite such as Miller-Rabin.

• AKS primality test, the one that proved that primality testing is in P.

31

Page 71: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

Primes:2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 72: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.

• For all numbers from 2 to√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

Primes:2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 73: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

Primes:2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 74: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

Primes:2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 75: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

Primes:2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 76: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:

2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 77: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2,

3, 5, 7, 11, 13, 17, 19, 23

32

Page 78: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2,

3, 5, 7, 11, 13, 17, 19, 23

32

Page 79: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3,

5, 7, 11, 13, 17, 19, 23

32

Page 80: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3,

5, 7, 11, 13, 17, 19, 23

32

Page 81: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5,

7, 11, 13, 17, 19, 23

32

Page 82: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5,

7, 11, 13, 17, 19, 23

32

Page 83: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5,

7, 11, 13, 17, 19, 23

32

Page 84: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5, 7,

11, 13, 17, 19, 23

32

Page 85: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5, 7, 11,

13, 17, 19, 23

32

Page 86: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5, 7, 11, 13,

17, 19, 23

32

Page 87: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5, 7, 11, 13, 17,

19, 23

32

Page 88: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5, 7, 11, 13, 17, 19,

23

32

Page 89: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Prime sieves

• If we want to generate primes, using a primality test is veryinefficient.

• Instead, our preferred method of prime generation is the sieve ofEratosthenes.• For all numbers from 2 to

√n:

• If the number is not marked, iterate over every multiple of thenumber up to n and mark them.

• The unmarked numbers are those that are not a multiple of anysmaller number.

• O(√N log logN)

2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 25

Primes:2, 3, 5, 7, 11, 13, 17, 19, 23

32

Page 90: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Sieve of Eratosthenes

vector<int> eratosthenes(int n){bool *isMarked = new bool[n+1];memset(isMarked, 0, n+1);vector<int> primes;int i = 2;for(; i*i <= n; ++i)

if (!isMarked[i]) {primes.push_back(i);for(int j = i; j <= n; j += i)

isMarked[j] = true;}

for (; i <= n; i++)if (!isMarked[i])

primes.push_back(i);return primes;

}33

Page 91: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = pe11 pe22 pe33 · · · pekk

We can therefore store integers as lists of their prime powers.To factor an integer n:

• Use the sieve of Eratosthenes to generate all the primes up√n

• Iterate over all the primes generated and check if they divide n, anddetermine the largest power that divides n.

34

Page 92: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = pe11 pe22 pe33 · · · pekk

We can therefore store integers as lists of their prime powers.To factor an integer n:

• Use the sieve of Eratosthenes to generate all the primes up√n

• Iterate over all the primes generated and check if they divide n, anddetermine the largest power that divides n.

34

Page 93: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = pe11 pe22 pe33 · · · pekk

We can therefore store integers as lists of their prime powers.

To factor an integer n:

• Use the sieve of Eratosthenes to generate all the primes up√n

• Iterate over all the primes generated and check if they divide n, anddetermine the largest power that divides n.

34

Page 94: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = pe11 pe22 pe33 · · · pekk

We can therefore store integers as lists of their prime powers.To factor an integer n:

• Use the sieve of Eratosthenes to generate all the primes up√n

• Iterate over all the primes generated and check if they divide n, anddetermine the largest power that divides n.

34

Page 95: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

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

Page 96: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The prime factors can be quite useful.

• The number of divisors

σ0(n) =k∏

i=1

(e1 + 1)

• The sum of all divisors in x-th power

σm(n) =k∏

i=1

(p(ei+1)x − 1)(pi − 1)

36

Page 97: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The prime factors can be quite useful.

• The number of divisors

σ0(n) =k∏

i=1

(e1 + 1)

• The sum of all divisors in x-th power

σm(n) =k∏

i=1

(p(ei+1)x − 1)(pi − 1)

36

Page 98: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

The prime factors can be quite useful.

• The number of divisors

σ0(n) =k∏

i=1

(e1 + 1)

• The sum of all divisors in x-th power

σm(n) =k∏

i=1

(p(ei+1)x − 1)(pi − 1)

36

Page 99: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

• The Euler’s totient function

φ(n) = n ·k∏

i=1

(1− p)

• Euler’s theorem, if a and n are coprime

aφ(n) = 1 (mod n)

Fermat’s theorem is a special case when n is a prime.

37

Page 100: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Integer factorization

• The Euler’s totient function

φ(n) = n ·k∏

i=1

(1− p)

• Euler’s theorem, if a and n are coprime

aφ(n) = 1 (mod n)

Fermat’s theorem is a special case when n is a prime.

37

Page 101: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Combinatorics

Page 102: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Combinatorics

Combinatorics is study of countable discrete structures.

Generic enumeration problem: We are given an infinite sequence of setsA1,A2, . . .An, . . . which contain objects satisfying a set of properties.Determine

an := |An|

for general n.

39

Page 103: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Combinatorics

Combinatorics is study of countable discrete structures.

Generic enumeration problem: We are given an infinite sequence of setsA1,A2, . . .An, . . . which contain objects satisfying a set of properties.Determine

an := |An|

for general n.

39

Page 104: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Basic counting

• Factorialn! = 1 · 2 · 3 · · · n

• Binomial coefficient (n

k

)=

n!

k!(n − k)!

Number of ways to choose k objects from a set of n objects,ignoring order.

40

Page 105: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Basic counting

• Factorialn! = 1 · 2 · 3 · · · n

• Binomial coefficient (n

k

)=

n!

k!(n − k)!

Number of ways to choose k objects from a set of n objects,ignoring order.

40

Page 106: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Basic counting

Properties

• (n

k

)=

(n

n − k

)• (

n

0

)=

(n

n

)= 1

• (n

k

)=

(n − 1k − 1

)+

(n − 1k

)

41

Page 107: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Basic counting

Pascal triangle!

(00

)(10

) (11

)(20

) (21

) (22

)(30

) (31

) (32

) (33

)(40

) (41

) (42

) (43

) (44

)(50

) (51

) (52

) (53

) (54

) (55

)(60

) (61

) (62

) (63

) (64

) (65

) (66

)(70

) (71

) (72

) (73

) (74

) (75

) (76

) (77

)(80

) (81

) (82

) (83

) (84

) (85

) (86

) (87

) (88

)(90

) (91

) (92

) (93

) (94

) (95

) (96

) (97

) (98

) (99

)(100

) (101

) (102

) (103

) (104

) (105

) (106

) (107

) (108

) (109

) (1010

)

42

Page 108: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

43

Page 109: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

43

Page 110: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical

• 2 horizontal

43

Page 111: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

43

Page 112: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

43

Page 113: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

• Number of ways we can choose 2 vertical lines(n

2

)

43

Page 114: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

• Number of ways we can choose 2 horizontal lines(m

2

)

43

Page 115: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical• 2 horizontal

• Total number of ways we can form a rectangle(n

2

)(m

2

)=

n!m!

(n − 2)!(m − 2)!2!2!

=n(n − 1)m(m − 1)

4

43

Page 116: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Multinomial

What if we have many objects with the same value?

• Number of permutations on n objects, where ni is the number ofobjects with the i-th value.(Multinomial)(

n

n1, n2, . . . , nk

)=

n!

n1!n2! · · · nk !

• Number of way to choose k objects from a set of n objects with,where each value can be chosen more than once.(

n + k − 1k

)=

(n + k − 1)!k!(n − 1)!

44

Page 117: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Multinomial

What if we have many objects with the same value?

• Number of permutations on n objects, where ni is the number ofobjects with the i-th value.(Multinomial)(

n

n1, n2, . . . , nk

)=

n!

n1!n2! · · · nk !

• Number of way to choose k objects from a set of n objects with,where each value can be chosen more than once.(

n + k − 1k

)=

(n + k − 1)!k!(n − 1)!

44

Page 118: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Multinomial

What if we have many objects with the same value?

• Number of permutations on n objects, where ni is the number ofobjects with the i-th value.(Multinomial)(

n

n1, n2, . . . , nk

)=

n!

n1!n2! · · · nk !

• Number of way to choose k objects from a set of n objects with,where each value can be chosen more than once.(

n + k − 1k

)=

(n + k − 1)!k!(n − 1)!

44

Page 119: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different ways can we divide k identical balls into n boxes?

• Same as number of nonnegative solutions to

x1 + x2 + . . .+ xn = k

• Let’s imagine we have a bit string consisting only of 1 of lengthn + k − 1

1 1 1 1 1 1 1 . . . 1︸ ︷︷ ︸n+k−1

45

Page 120: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different ways can we divide k identical balls into n boxes?

• Same as number of nonnegative solutions to

x1 + x2 + . . .+ xn = k

• Let’s imagine we have a bit string consisting only of 1 of lengthn + k − 1

1 1 1 1 1 1 1 . . . 1︸ ︷︷ ︸n+k−1

45

Page 121: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different ways can we divide k identical balls into n boxes?

• Same as number of nonnegative solutions to

x1 + x2 + . . .+ xn = k

• Let’s imagine we have a bit string consisting only of 1 of lengthn + k − 1

1 1 1 1 1 1 1 . . . 1︸ ︷︷ ︸n+k−1

45

Page 122: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• Choose n − 1 bits to be swapped for 0

1 . . . 1 0 1 . . . 1 0 . . . 0 1 . . . 1

• Then total number of 1 will be k , each 1 representing an eachelement, and separated into n groups

• Number of ways to choose the bits to swap(n + k − 1n − 1

)=

(n + k − 1

k

)

46

Page 123: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• Choose n − 1 bits to be swapped for 0

1 . . . 1︸ ︷︷ ︸x1

0 1 . . . 1︸ ︷︷ ︸x2

0 . . . 0 1 . . . 1︸ ︷︷ ︸xn

• Then total number of 1 will be k , each 1 representing an eachelement, and separated into n groups

• Number of ways to choose the bits to swap(n + k − 1n − 1

)=

(n + k − 1

k

)

46

Page 124: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

• Choose n − 1 bits to be swapped for 0

1 . . . 1︸ ︷︷ ︸x1

0 1 . . . 1︸ ︷︷ ︸x2

0 . . . 0 1 . . . 1︸ ︷︷ ︸xn

• Then total number of 1 will be k , each 1 representing an eachelement, and separated into n groups

• Number of ways to choose the bits to swap(n + k − 1n − 1

)=

(n + k − 1

k

)

46

Page 125: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different lattice paths are there from (0, 0) to (n,m)?

(0, 0)

(n,m)

1 1 1 1 1 1 1

1 2 3 4 5 6 7

1 3 6 10 15 21 28

1 4 10 20 35 56 84

1 5 15 35 70 126 210

1 6 21 56 126 252 462

• There is 1 path to (0, 0)

• There is 1 path to (1, 0) and (0, 1)

• Paths to (1, 1) is the sum of numberof paths to (0, 1) and (1, 0).

47

Page 126: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different lattice paths are there from (0, 0) to (n,m)?

(0, 0)

(n,m)

1 1

1

1 1 1 1 1 1 1

1 2 3 4 5 6 7

1 3 6 10 15 21 28

1 4 10 20 35 56 84

1 5 15 35 70 126 210

1 6 21 56 126 252 462

• There is 1 path to (0, 0)

• There is 1 path to (1, 0) and (0, 1)

• Paths to (1, 1) is the sum of numberof paths to (0, 1) and (1, 0).

47

Page 127: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different lattice paths are there from (0, 0) to (n,m)?

(0, 0)

(n,m)

1 1

1

1 1 1 1 1 1 1

1 2 3 4 5 6 7

1 3 6 10 15 21 28

1 4 10 20 35 56 84

1 5 15 35 70 126 210

1 6 21 56 126 252 462

• There is 1 path to (0, 0)

• There is 1 path to (1, 0) and (0, 1)

• Paths to (1, 1) is the sum of numberof paths to (0, 1) and (1, 0).

47

Page 128: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different lattice paths are there from (0, 0) to (n,m)?

(0, 0)

(n,m)

1 1

1 2

1 1 1 1 1 1 1

1 2 3 4 5 6 7

1 3 6 10 15 21 28

1 4 10 20 35 56 84

1 5 15 35 70 126 210

1 6 21 56 126 252 462

• There is 1 path to (0, 0)

• There is 1 path to (1, 0) and (0, 1)

• Paths to (1, 1) is the sum of numberof paths to (0, 1) and (1, 0).

47

Page 129: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different lattice paths are there from (0, 0) to (n,m)?

(0, 0)

(n,m)

1 1

1 2

1

1

1

3

3

1

1 1 1 1 1 1 1

1 2 3 4 5 6 7

1 3 6 10 15 21 28

1 4 10 20 35 56 84

1 5 15 35 70 126 210

1 6 21 56 126 252 462

• There is 1 path to (0, 0)

• There is 1 path to (1, 0) and (0, 1)

• Paths to (1, 1) is the sum of numberof paths to (0, 1) and (1, 0).

• Number of paths to (i , j) is the sumof the number of paths to (i − 1, j)and (i , j − 1).

47

Page 130: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

How many different lattice paths are there from (0, 0) to (n,m)?

(0, 0)

(n,m)

1 1 1 1 1 1 1

1 2 3 4 5 6 7

1 3 6 10 15 21 28

1 4 10 20 35 56 84

1 5 15 35 70 126 210

1 6 21 56 126 252 462 • There is 1 path to (0, 0)

• There is 1 path to (1, 0) and (0, 1)

• Paths to (1, 1) is the sum of numberof paths to (0, 1) and (1, 0).

• Number of paths to (i , j) is(i + j

i

)

47

Page 131: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

What if we are not allowed to cross the main diagonal?

(0, 0)

(n,m)

• The number of paths from (0, 0) to(n,m)

Cn =1

n + 1

(2nn

)

• Cn are known as Catalan numbers.

• Many problems involve solutions givenby the Catalan numbers.

48

Page 132: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

What if we are not allowed to cross the main diagonal?

(0, 0)

(n,m)

• The number of paths from (0, 0) to(n,m)

Cn =1

n + 1

(2nn

)

• Cn are known as Catalan numbers.

• Many problems involve solutions givenby the Catalan numbers.

48

Page 133: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

What if we are not allowed to cross the main diagonal?

(0, 0)

(n,m)

• The number of paths from (0, 0) to(n,m)

Cn =1

n + 1

(2nn

)

• Cn are known as Catalan numbers.

• Many problems involve solutions givenby the Catalan numbers.

48

Page 134: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

• Number of different ways n + 1 factors can be completelyparenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.

• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.

• And aloot more.

49

Page 135: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

• Number of different ways n + 1 factors can be completelyparenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.

• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.

• And aloot more.

49

Page 136: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

• Number of different ways n + 1 factors can be completelyparenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.

• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.

• And aloot more.

49

Page 137: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

• Number of different ways n + 1 factors can be completelyparenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.

• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.

• And aloot more.

49

Page 138: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Catalan

• Number of different ways n + 1 factors can be completelyparenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.

• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.

• And aloot more.

49

Page 139: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci

The Fibonacci sequence is defined recursively as

f1 = 1

f2 = 1

fn = fn−1 + fn−2

Already covered how to calculate fn in O(N) time with dynamicprogramming.But we can do even better.

50

Page 140: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci

The Fibonacci sequence is defined recursively as

f1 = 1

f2 = 1

fn = fn−1 + fn−2

Already covered how to calculate fn in O(N) time with dynamicprogramming.

But we can do even better.

50

Page 141: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci

The Fibonacci sequence is defined recursively as

f1 = 1

f2 = 1

fn = fn−1 + fn−2

Already covered how to calculate fn in O(N) time with dynamicprogramming.But we can do even better.

50

Page 142: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci as matrix

The Fibonacci sequence can be represented by a vectors(fn+2

fn+1

)=

(1 11 0

)(fn+1

fn

)

Or simply as a matrix (1 11 0

)n

=

(fn+1 fnfn fn−1

)

Using fast exponentiaton, we can calculate fn in O(logN) time.

51

Page 143: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci as matrix

The Fibonacci sequence can be represented by a vectors(fn+2

fn+1

)=

(1 11 0

)(fn+1

fn

)

Or simply as a matrix (1 11 0

)n

=

(fn+1 fnfn fn−1

)

Using fast exponentiaton, we can calculate fn in O(logN) time.

51

Page 144: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci as matrix

The Fibonacci sequence can be represented by a vectors(fn+2

fn+1

)=

(1 11 0

)(fn+1

fn

)

Or simply as a matrix (1 11 0

)n

=

(fn+1 fnfn fn−1

)

Using fast exponentiaton, we can calculate fn in O(logN) time.

51

Page 145: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci as matrix

Any linear recurrence

an = c1an−1 + c2an−2 . . . ckan−k

can be expressed in the same wayan+1

an...

an−k

=

c1 c2 . . . ck1 0 . . . 0...

...0 0 . . . 0

anan−1...

an−k−1

With a recurrence relation defined as a linear function of the k precedingterms the running time will be O(k3 logN).

52

Page 146: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Fibonacci as matrix

Any linear recurrence

an = c1an−1 + c2an−2 . . . ckan−k

can be expressed in the same wayan+1

an...

an−k

=

c1 c2 . . . ck1 0 . . . 0...

...0 0 . . . 0

anan−1...

an−k−1

With a recurrence relation defined as a linear function of the k precedingterms the running time will be O(k3 logN).

52

Page 147: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game Theory

Page 148: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game theory

Game theory is the study of strategic decision making, but in competitiveprogramming we are mostly interested in combinatorial games.

Example:

• There is a pile of k matches.

• Player can remove 1, 2 or 3 from the pile and alternate on moves.

• The player who removes the last match wins.

• There are two players, and the first player starts.

• Assuming that both players play perfectly, who wins?

54

Page 149: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game theory

Game theory is the study of strategic decision making, but in competitiveprogramming we are mostly interested in combinatorial games.Example:

• There is a pile of k matches.

• Player can remove 1, 2 or 3 from the pile and alternate on moves.

• The player who removes the last match wins.

• There are two players, and the first player starts.

• Assuming that both players play perfectly, who wins?

54

Page 150: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

We can analyse these types of games with backward induction.

We call a state N-position if it is a winning state for the next player tomove, and P-position if it is a winning position for the previous player.

• All terminal positions are P-positions.

• If every reachable state from the current one is a N-position thenthe current state is a P-position.

• If at least one P-position can be reached from the current one, thenthe current state is a N-position.

• A position is a P-position if all reachable states form the current oneare N position.

55

Page 151: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

We can analyse these types of games with backward induction.We call a state N-position if it is a winning state for the next player tomove, and P-position if it is a winning position for the previous player.

• All terminal positions are P-positions.

• If every reachable state from the current one is a N-position thenthe current state is a P-position.

• If at least one P-position can be reached from the current one, thenthe current state is a N-position.

• A position is a P-position if all reachable states form the current oneare N position.

55

Page 152: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

Let’s analyse our previous game.

• The terminal position is a P-position.

• The positions reachable from the terminal positions are N-positions.

• Position 4 can only reach N-positions, therefore a P position.

• The next 3 positions can reach the P-position 4, therefore they areN-positions.

• And so on.

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

Page 153: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

Let’s analyse our previous game.

• The terminal position is a P-position.

• The positions reachable from the terminal positions are N-positions.

• Position 4 can only reach N-positions, therefore a P position.

• The next 3 positions can reach the P-position 4, therefore they areN-positions.

• And so on.

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

Page 154: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

Let’s analyse our previous game.

• The terminal position is a P-position.

• The positions reachable from the terminal positions are N-positions.

• Position 4 can only reach N-positions, therefore a P position.

• The next 3 positions can reach the P-position 4, therefore they areN-positions.

• And so on.

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

Page 155: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

Let’s analyse our previous game.

• The terminal position is a P-position.

• The positions reachable from the terminal positions are N-positions.

• Position 4 can only reach N-positions, therefore a P position.

• The next 3 positions can reach the P-position 4, therefore they areN-positions.

• And so on.

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

Page 156: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

Let’s analyse our previous game.

• The terminal position is a P-position.

• The positions reachable from the terminal positions are N-positions.

• Position 4 can only reach N-positions, therefore a P position.

• The next 3 positions can reach the P-position 4, therefore they areN-positions.

• And so on.

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

Page 157: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Example

Let’s analyse our previous game.

• The terminal position is a P-position.

• The positions reachable from the terminal positions are N-positions.

• Position 4 can only reach N-positions, therefore a P position.

• The next 3 positions can reach the P-position 4, therefore they areN-positions.

• And so on.

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

Page 158: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game theory

We can see a clear pattern of the N and P positions in the previousgame. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.

• Not only one dimensional games.

• What if there are n piles instead of 1?

• What if we can remove 1, 3 or 4?

57

Page 159: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game theory

We can see a clear pattern of the N and P positions in the previousgame. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.

• Not only one dimensional games.

• What if there are n piles instead of 1?

• What if we can remove 1, 3 or 4?

57

Page 160: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game theory

We can see a clear pattern of the N and P positions in the previousgame. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.

• Not only one dimensional games.

• What if there are n piles instead of 1?

• What if we can remove 1, 3 or 4?

57

Page 161: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Game theory

We can see a clear pattern of the N and P positions in the previousgame. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.

• Not only one dimensional games.

• What if there are n piles instead of 1?

• What if we can remove 1, 3 or 4?

57

Page 162: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

The game called Nim

• There are n piles, each containing xi chips.

• Player can remove from exactly one pile, and can remove anynumber of chips.

• The player who removes the last match wins.

• There are two players, and the first player starts and they alternateon moves.

• Assuming that both players play perfectly, who wins?

58

Page 163: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.

• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1, x2, . . . , xn) in Nim is aP-position if and only if the xor over the number of chips is 0.This theorem extends to other sums of combinatorial games!

59

Page 164: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.

• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1, x2, . . . , xn) in Nim is aP-position if and only if the xor over the number of chips is 0.This theorem extends to other sums of combinatorial games!

59

Page 165: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.

• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1, x2, . . . , xn) in Nim is aP-position if and only if the xor over the number of chips is 0.This theorem extends to other sums of combinatorial games!

59

Page 166: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.

• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1, x2, . . . , xn) in Nim is aP-position if and only if the xor over the number of chips is 0.

This theorem extends to other sums of combinatorial games!

59

Page 167: Mathematics · Mathematics TómasKenMagnússon BjarkiÁgústGuðmundsson Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.

• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1, x2, . . . , xn) in Nim is aP-position if and only if the xor over the number of chips is 0.This theorem extends to other sums of combinatorial games!

59