1. Modular Arithmetic
Total Page:16
File Type:pdf, Size:1020Kb
Modular arithmetic Divisibility Given p ositive numb ers a b if a we can write b aq r 1 for appropriate integers q r such that r a The numb er r is the remainder We say that a divides b or ajb if r and so b aq ie b factors as a times q Primes A numb er is prime if it cant b e factored as a pro duct of two numb ers greater or equal to If a numb er factors ie it is not prime then we say that it is comp osite Exercise Find all prime numb ers smaller than Here are several imp ortant questions How do we determine whether a numb er is prime How do we factor a numb er into primes How can we nd big prime numb ers How many prime numb ers are there Exercise Factor Exercise Show that every numb er is either prime or divisible by a prime numb er Theorem There are innitely many prime numbers Proof Supp ose that there are only nitely many prime numb ers P P 1 n then N P P P is not divisible by any prime hence it do es 1 2 n factor and hence it is a new prime not ˜ Divisibility tricks Recall that a numb er is even if its last digit is divisible by it is divisible by if the sum of its digits is divisible by it is divisible by if its last digit is divisible by etc Why is this Can we nd other divisibility tricks eg for One p ossible explanation Let abc b e a digit numb er so that abc a b c Then abc a b c and this is an integer only if c is an integer abc a b c and this is an integer only if c is an integer abc a b a b c and this is an integer only if a b c is an integer A b etter explanation is given by congruences 1 In Python notation r ba and q ba 1 2 Congruences We say that a is congruent to b mo dulo N ie a b mod N i N divides a b or equivalently i aN bN So a is congruent mo dulo N to any numb er in the arithmetic progression f a N a N a a N a N g Here are some examples mod mod mod mod Exercise Is divisible by How ab out 100 Exercise What is the last digit of 111 Of What are the last two digits of the numb ers ab ove Solution Every integer mo dulo is congruent to one of f g we call this a system of residues mo dulo We now compute the 100 residue of We have 2 3 4 Then 100 4 25 25 Arithmetic of residues mo dular arithmetic Residues mo d ulo f g Z Exercise Write down the addition and multiplication tables We can compute etc We have Check Now ab makes little sense eg 1 1 what do es mean We should write a b eg From the 1 multiplication table we see that so that and then 1 Exercise Write down the addition and multiplication ta bles for Z Z Find the additive and multiplicative inverses of mo d mo d mo d and mo d Find the order of mo d Find the orders of mo d Do you see a pattern 2 2 Can you solve x mo d and x mo d Show that if a b c d then a b b d and ac bd 3 The Euclidean Algorithm The greatest common divisor By denition the greatest com mon divisor of two numb ers a b is the largest numb er that divides b oth a and b We denote it by GC D a b or just by a b Eg To see the last one write and so that the sets of divisors are f g and f g We can compute the GCD in the following ways By brute force Find all the divisors and compare Find all divisors of a and throw out the ones that do not divide b Compare prime factors and multiply the highest p owers that divide b oth numb ers The Euclidean algorithm The Euclidean algorithm is by far the fastest metho d for big numb ers The Euclidean algorithm Start with a b and let r ab b e the remainder Replace a by b and b by r and rep eat The last nonzero remainder is the GCD a b r q The GCD is Exercise Find the GCD of and of Exercise Factor the ab ove numb ers Python Co de def gcdab rab print abr while r abr brbr print abr return b It works as follows gcd Well ordering principle Each nonempty set of positive integers has a least element 4 Note that this is not true for the rationals Eg has no smallest 0 Q numb er Prop osition Given positive integers a b let S fax by g where x y are any integers Let g be the least element in S then g GC D a b Proof It is enough to show that g divides GC D a b and that GC D a b divides g Clearly GC D a b divides a and b so it divides any element in S and so it divides g To see that g divides GC D a b it is enough to show that g divides a and that g divides b Consider a g q r then r a g q a ax by q q ax bq y Now r g and so r else r S contradicting the fact that g is the smallest element of S Therefore g divides a and by the same argument it divides b and so divides GC D a b it ˜ So we can always write GC D a b ax by This allows us to nd the inverse of any numb er a p mo dulo p when p is prime In fact GC D a p so we have ax py so 1 mo dulo p we have ax ie x a mo dulo p Finding x and y Use back substitution Eg for a b then GC D a b Recall that a b r q So and Then Exercise Solve x y x y Exercise Find the multiplicative inverse of mo d mo d 5 Python co de for the Extended Euclidean Algorithm def egcdab qr divmo dab xyuvq while r qqrr divmo dbr xxyyuqqxvqqy abrqxyuvbrrrqqxxyyxy print abrqxyuv return uvb Exercise What are the last digits of the multiplicative inverse 100 of mo d Exercise I have enco ded my SSN using the algorithm f x x 10 mo d The result is What is my SSN You will need a calculator that can compute with digit numb ers eg python Modular Powers We b egin by writing a table for the p owers mo dulo Mo dulo 2 3 4 5 6 7 a a a a a a a Note that after we get a column with all s and a at the top the pattern must rep eat Therefore as so on as and if this happ ens the table just keeps rep eating itself and so we do not b other to keep writing the rep eated entries Mo dulo 2 3 4 5 6 7 a a a a a a a Here the th column has all s and a at the top Mo dulo 2 3 4 5 6 7 a a a a a a a 6 Do you notice a pattern The rst pattern seems to b e that for all 2 4 6 a we have a mo d a mo d a mo d It is easy to guess what the general pattern might b e Theorem Fermats Little Theorem Let p be any prime and a p Then p1 a mo d p 22 22 Eg mo d Check Similarly 100 100 mo d Note that has more than digits so checking explicitly might not b e so easy Proof We claim that the set fa a p ag is the same as the set f p g the elements are not in the same order Granting the claim then we have p1 p a a p a p a Since p do es not divide p it follows that GC D p p so there is a numb er x p such that x p mo d p But then p1 p1 x p x p a a as required To see the claim we must check that The map x ax mo d p induces a map f p g fa a p ag Ie that for all x p we have that p do es not divide ax This is clear as p is a prime numb er that do es not divide either of a and x If x y p then ax ay mo d p ie this function is to Supp ose in fact that ax ay mo d p As GC D a p there is a numb er b such that ba mo d p Therefore bax bay so that y x ˜ 12345 Exercise Compute mo d 12345 Exercise Compute mo d Exercise Compute the order of mo d and Can you make a guess ab out the general pattern Eulers function How do es Fermats Little Theorem general ize to numb ers m that are not prime The key thing in the pro of is that f p g are coprime with p so that they have multiplicative inverses in It is then natural to dene Zp m fa a m and GC D a m g This is Eulers function We now compute its value for some ms p p for all prime numb ers p Eg 7 f g f g 2 2 p p p for any prime numb er p To see this note that 2 in the list f p g we must erase p p p p so we 2 2 have p p p p entries n n n1 p p p for any prime numb er p Explain this in detail f g f g f g f g p q p q for any prime numb ers p q To see this note that in the list f pq g we must erase p p q p and q q p q so we have pq p q p q entries m n m n whenever GC D m n Exercise Prove the last fact Exercise Compute Theorem Eulers Formula If GC D a m then (m) a mo d m 5243 Exercise Compute mo d 90 Exercise Compute mo d Is prime m So if we pick a random numb er a m and we have a a mo d m m then m is not prime If a a mo d m then m mayb e prime m If a a mo d m for many as then m is probably prime m Exercise There are some comp osite numb ers such that a a mo d m for all a m Can you nd one 1000000000 Powers mo d m How would you compute mo d by hand Or mayb e with a p o cket calculator so 9 1000000000 2200 so mo d 2 4 2 8 2 16 2 32 64 128 256 512 1024 2048 Now so 2200 mo d This was remarkably quick It turns out that we did not need to p erform op erations Just ab out l og so ab out 2 op erations note that in base the numb er is 1000000000 mo d mo d mo d Exercise Compute 8 Exercise Estimate the numb er of doublings needed to compute 100 10 2 mo d 9990 Exercise Compute mo d without factoring Is prime Factor