lecture 7 number theory - androbenchcsl.skku.edu/uploads/swe2004s13/lecture7.pdfswe2004: principles...

20
1 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected]) Lecture 7 Number Theory Euiseong Seo ([email protected])

Upload: others

Post on 26-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

1 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Lecture 7 Number Theory

Euiseong Seo ([email protected])

Page 2: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

2 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Number Theory §  God created the integers. All else is the work

of man – Leopold Kronecker §  Study of the property of the integers

•  Specifically, integer divisibility •  Full of beautiful proofs and surprising results

§  b divides a •  b|a •  a = bk for some integer k •  b is a divisor of a •  a is a multiple of b

Page 3: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

3 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Prime Numbers §  Prime Numbers

•  integers p > 1 which are only divisible by 1 and itself •  if p is a prime numner, then p = a*b for integers a <= b

implies that a = 1 and b = p §  Fundamental theorem of arithmetic

•  Every integer can be expressed in only one way as the product of primes

•  Examples –  105 = 3 * 5 * 7 –  32 = 2 * 2 * 2 * 2 * 2

•  Prime factorization §  Any number which is not prime is a composite

Page 4: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

4 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Primality Testing and Factorization

§  There are an infinite number of primes (Euclid’s proof)

§  x / ln x number of primes that are less than or equal to x

§  The smallest prime factor of n is at most √n unless n is a prime

Page 5: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

5 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Constructing All Divisors §  Every divisor is the product of some subset of

these prime factors §  Such subsets can be constructed using

backtracking techniques •  Be careful about duplicate prime factors •  12 has three terms but has only six divisors

Page 6: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

6 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Greatest Common Divisor §  Largest divisor shared by a given pair of

integers §  Used to obtain the reduced form of a fraction §  Euclid’s GCD algorithms

•  If b|a, then gdb(a,b) = b •  If a = bt + r for integers t and r, then gcd(a,b) = gcd(b,r)

§  ax + by = gcd(a,b)

Page 7: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

7 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Greatest Common Divisor Implementation

/* Find the gcd(p,q) and x,y such that p*x + q*y = gcd(p,q) */

long gcd(long p, long q, long *x, long *y){

long x1,y1; /* previous coefficients */long g; /* value of gcd(p,q) */

if (q > p) return(gcd(q,p,y,x));

if (q == 0) {

*x = 1;

*y = 0;return(p);

}

g = gcd(q, p%q, &x1, &y1);

*x = y1;

*y = (x1 - floor(p/q)*y1);

return(g);}

The least common multiple (lcm), the smallest integer whichis divided by both of a given pair of integers. For example,the least common multiple of 24 and 36 is 72.To compute it, observe that lcm(x, y) = xy/gcd(x, y).

Page 8: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

8 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Least Common Multiple §  The smallest integer which is divided by both

of a given pair of integers §  lcm(x,y) = xy / gcd(x,y)

Page 9: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

9 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Modular Arithmetic §  What day of the week will your birthday fall on

next year? §  Modular arithmetic

•  modulus •  residue

§  (x+y) mod n = ((x mod n) + (y mod n)) mod n §  (12 mod 100) – (53 mod 100) = -41 mod 100

= 59 mod 100 §  xy mod n = (x mod n)(y mod n) mod n §  xy mod n = (x mod n)y mod n

Page 10: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

10 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Modular Arithmetic §  Finding the last digit – What is the last digit of

2100?

Page 11: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

11 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Congruences §  An alternate notation for representing modular

arithmetic §  a ≡ b (mod n) if m|(a-b)

•  By definition, if a mod m is b, then a ≡ b (mod n) •  What integers x satisfy the congruence x ≡ 3 (mod 9)? •  What about 2x ≡ 3 (mod 9) and 2x ≡ 3 (mod 4)?

Page 12: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

12 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Operations on Congruences §  Congruences support addition, subtraction,

and multiplication, as well as a limited form of division •  Addition and subtraction

–  a ≡ b (mod n) and c ≡ d (mod n) -> a+c ≡ b+d (mod n)

•  Multiplication –  a ≡ b (mod n) and c ≡ d (mod n) -> ac ≡ bd (mod n)

•  Division –  6 * 2 ≡ 6 * 1 (mod 3) -/-> 2 ≡ 1 (mod 3) –  ad ≡ bd (mod dn) -> a ≡ b (mod n)

Page 13: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

13 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Solving Linear Congruences §  A linear congruence

•  An equation of the form ax ≡ b (mod n) •  Solving means identifying which values of x satisfy it

§  Not all linear congruences have solutions •  ax ≡ 1 (mod n) has a solution if and only if

the modulus and the multiplier are relatively prime §  Using Euclid’s algorithm to find this inverse

•  ax ≡ 1 (mod n) -> ax ≡ ax’ + ny’ (mod n) •  Since ny’ ≡ 0 (mod n), this inverse is the x’ from

Euclid’s algorithm

Page 14: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

14 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Solving Linear Congruences §  In general, there are three cases depending on

the relationship between a, b, and n •  gcd(a,b,n) > 1

– We can divide all three terms by this divisor to get an equivalent congruence

–  This gives us a single solution mod the new base, or equivalently gcd(a,b,n) solutions (mod n)

•  gcd(a,n) does not divide b –  The congruence can have no solution

•  gcd(a,b) = 1 –  There is one solution (mod n) –  x = a-1b works since aa-1b ≡ b (mod n) –  Inverse exists and can be found using Euclid’s algorithm

Page 15: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

15 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

More Advanced Tools §  Chinese remainder theorem

•  Gives us a tool for working with systems of congruence over different moduli

•  Suppose there exists an integer x such that x ≡ a1 (mod m1) and x ≡ a2 (mod m2), then x is uniquely determined (mod m1m2) if m1 and m2 are relatively prime

Page 16: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

16 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Diophantine Equations §  Formulae in which the variables are restricted

to integers §  Fermat’s last theorem

•  an + bn = cn

§  Linear Diophatine equation •  ax – ny = b, where x and y are the integer variables

and a, b, and n are integer constants •  Equivalent to ax ≡ b (mod n)

Page 17: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

17 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Light, More Light

7.6. Problems 157

7.6 Problems

7.6.1 Light, More LightPC/UVa IDs: 110701/10110, Popularity: A, Success rate: average Level: 1

There is man named Mabu who switches on-o! the lights along a corridor at ouruniversity. Every bulb has its own toggle switch that changes the state of the light. Ifthe light is o!, pressing the switch turns it on. Pressing it again will turn it o!. Initiallyeach bulb is o!.

He does a peculiar thing. If there are n bulbs in a corridor, he walks along the corridorback and forth n times. On the ith walk, he toggles only the switches whose positionis divisible by i. He does not press any switch when coming back to his initial position.The ith walk is defined as going down the corridor (doing his peculiar thing) and comingback again. Determine the final state of the last bulb. Is it on or o!?

InputThe input will be an integer indicating the nth bulb in a corridor, which is less than orequal to 232 ! 1. A zero indicates the end of input and should not be processed.

OutputOutput “yes” or “no” to indicate if the light is on, with each case appearing on its ownline.

Sample Input3624181910

Sample Outputnoyesno

7.6. Problems 157

7.6 Problems

7.6.1 Light, More LightPC/UVa IDs: 110701/10110, Popularity: A, Success rate: average Level: 1

There is man named Mabu who switches on-o! the lights along a corridor at ouruniversity. Every bulb has its own toggle switch that changes the state of the light. Ifthe light is o!, pressing the switch turns it on. Pressing it again will turn it o!. Initiallyeach bulb is o!.

He does a peculiar thing. If there are n bulbs in a corridor, he walks along the corridorback and forth n times. On the ith walk, he toggles only the switches whose positionis divisible by i. He does not press any switch when coming back to his initial position.The ith walk is defined as going down the corridor (doing his peculiar thing) and comingback again. Determine the final state of the last bulb. Is it on or o!?

InputThe input will be an integer indicating the nth bulb in a corridor, which is less than orequal to 232 ! 1. A zero indicates the end of input and should not be processed.

OutputOutput “yes” or “no” to indicate if the light is on, with each case appearing on its ownline.

Sample Input3624181910

Sample Outputnoyesno

Page 18: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

18 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Light, More Light

7.6. Problems 157

7.6 Problems

7.6.1 Light, More LightPC/UVa IDs: 110701/10110, Popularity: A, Success rate: average Level: 1

There is man named Mabu who switches on-o! the lights along a corridor at ouruniversity. Every bulb has its own toggle switch that changes the state of the light. Ifthe light is o!, pressing the switch turns it on. Pressing it again will turn it o!. Initiallyeach bulb is o!.

He does a peculiar thing. If there are n bulbs in a corridor, he walks along the corridorback and forth n times. On the ith walk, he toggles only the switches whose positionis divisible by i. He does not press any switch when coming back to his initial position.The ith walk is defined as going down the corridor (doing his peculiar thing) and comingback again. Determine the final state of the last bulb. Is it on or o!?

InputThe input will be an integer indicating the nth bulb in a corridor, which is less than orequal to 232 ! 1. A zero indicates the end of input and should not be processed.

OutputOutput “yes” or “no” to indicate if the light is on, with each case appearing on its ownline.

Sample Input3624181910

Sample Outputnoyesno

Page 19: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

19 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Factovisors 160 7. Number Theory

7.6.4 FactovisorsPC/UVa IDs: 110704/10139, Popularity: A, Success rate: average Level: 2

The factorial function, n! is defined as follows for all non-negative integers n:

0! = 1n! = n ! (n " 1)! (n > 0)

We say that a divides b if there exists an integer k such that

k ! a = b

InputThe input to your program consists of several lines, each containing two non-negativeintegers, n and m, both less than 231.

OutputFor each input line, output a line stating whether or not m divides n!, in the formatshown below.

Sample Input6 96 2720 1000020 1000001000 1009

Sample Output9 divides 6!27 does not divide 6!10000 divides 20!100000 does not divide 20!1009 does not divide 1000!

160 7. Number Theory

7.6.4 FactovisorsPC/UVa IDs: 110704/10139, Popularity: A, Success rate: average Level: 2

The factorial function, n! is defined as follows for all non-negative integers n:

0! = 1n! = n ! (n " 1)! (n > 0)

We say that a divides b if there exists an integer k such that

k ! a = b

InputThe input to your program consists of several lines, each containing two non-negativeintegers, n and m, both less than 231.

OutputFor each input line, output a line stating whether or not m divides n!, in the formatshown below.

Sample Input6 96 2720 1000020 1000001000 1009

Sample Output9 divides 6!27 does not divide 6!10000 divides 20!100000 does not divide 20!1009 does not divide 1000!

Page 20: Lecture 7 Number Theory - AndroBenchcsl.skku.edu/uploads/SWE2004S13/Lecture7.pdfSWE2004: Principles in Programming | Spring 2013 | Euiseong Seo (euiseong@skku.edu) 2 Number Theory

20 SWE2004: Principles in Programming | Spring 2013 | Euiseong Seo ([email protected])

Factovisors

160 7. Number Theory

7.6.4 FactovisorsPC/UVa IDs: 110704/10139, Popularity: A, Success rate: average Level: 2

The factorial function, n! is defined as follows for all non-negative integers n:

0! = 1n! = n ! (n " 1)! (n > 0)

We say that a divides b if there exists an integer k such that

k ! a = b

InputThe input to your program consists of several lines, each containing two non-negativeintegers, n and m, both less than 231.

OutputFor each input line, output a line stating whether or not m divides n!, in the formatshown below.

Sample Input6 96 2720 1000020 1000001000 1009

Sample Output9 divides 6!27 does not divide 6!10000 divides 20!100000 does not divide 20!1009 does not divide 1000!