Mathematics for Computer Science 201-N11 • CEGEP, Montreal
Corrected exercises: modular arithmetic (201-N11)
This is the corrected exercise set on modular arithmetic for Mathematics for Computer Science, 201-N11, the mathematics course of CEGEP computer science programs: Euclidean division, congruences, the GCD, the modular inverse, Fermat's theorem and applications to cryptography. It is the chapter that links elementary arithmetic to the tools actually used in computing.
Exercises 0.1 and 0.2 open the set with two lists of direct calculations, as in textbook drill lists: nine remainders to compute modulo n, from a negative integer such as −38mod7 to a sum and a product reduced term by term, up to the last digit of 783 by the cycle of powers and 2405mod13 by Fermat's little theorem, plus a GCD by the Euclidean algorithm; then eight modular inverses and congruences to solve, from the inverse of 5 modulo 11 to Bézout coefficients by the extended Euclidean algorithm and the congruence 8x≡12(mod28) with its four solutions. Every line has its value to find, checked as you type it.
The thread of the set fits in one sentence: we no longer care about the quotient but about the REMAINDER, and computing with remainders is legitimate because congruence is compatible with addition and multiplication. Everything else, from check digits to RSA, follows from that compatibility and from its single exception, division.
Part A builds the tools, from Euclidean division to linear congruences. Part B puts them together: Bézout, Fermat, fast exponentiation, then two complete applications, the check digit of an ISBN code and an RSA encryption carried out from start to finish. Every solution is on the page, with the Python code of the algorithms.
Self-checking setType your answers and check them question by question: the page tells you right or wrong before you open the solution. With an account, every answer right on the first try earns points, and your right answers stay filled in when you come back.
•EUCLIDEAN DIVISION: for n>0, there is a unique pair (q,r) with a=nq+r and 0≤r<n. The remainder is ALWAYS nonnegative, even when a<0: −17mod5=3. Watch out, the operator of the C and Java languages does not follow this convention.
•CONGRUENCES: a≡b(modn) means that n divides a−b, or that a and b leave the same remainder. The relation is compatible with ADDITION and MULTIPLICATION, so you reduce BEFORE computing. It is NOT compatible with division.
•CANCELLATION: ac≡bc(modn) implies a≡b(modn) only if gcd(c,n)=1. Otherwise, the modulus must be divided by gcd(c,n).
•EUCLID: gcd(a,n)=gcd(n,amodn), which gives the algorithm. The number of steps is at most about 5 times the number of digits, a worst case reached on two consecutive Fibonacci numbers.
•BÉZOUT: there exist u and v such that au+nv=gcd(a,n), computed by the EXTENDED Euclidean algorithm. If the GCD equals 1, then u is the inverse of a modulo n.
•INVERSE: a is invertible modulo n if and only if gcd(a,n)=1. All the nonzero elements are invertible if and only if n is PRIME. Otherwise ZERO DIVISORS appear.
•CONGRUENCE ax≡b(modn): it has solutions if and only if gcd(a,n) divides b, and then it has exactly gcd(a,n) of them modulo n.
•FERMAT: if p is prime and does not divide a, then ap−1≡1(modp). So you reduce the BASE modulo p and the EXPONENT modulo p−1. FAST EXPONENTIATION computes aemodn in about 2log2e multiplications.
Calculations: remainders to compute
Exercise 0.1: Reduce modulo n: ten direct calculations
For each line, give the remainder r asked for, with 0≤r<n, or the GCD on the last line. A calculator is no help here: every number reduces by hand.
A single method: reduce BEFORE computing. Replace each number by its remainder, term by term in a sum, factor by factor in a product; for a power, look for the first exponent that brings you back to 1, through the cycle of powers or through Fermat's little theorem when the modulus is prime, then reduce the exponent. A negative remainder does not exist: add n until you land in [0,n).
a) 347mod9.
b) −38mod7.
c) (482+937+615)mod8, reducing each term.
d) (123×456)mod11, reducing each factor.
e) The remainder of n3+2n modulo 4, given that n≡3(mod4).
f) 2,9992mod11.
g) 441mod9, using the cycle of powers of 4.
h) The last digit of 783, that is, 783mod10.
i) 2405mod13, using Fermat's little theorem.
j) The GCD of 1,518 and 391, by the Euclidean algorithm.
Show the solution
Answers
a)347=9×38+5; 5
b)−38=7×(−6)+4; 4
c)2+1+7≡2; 2
d)2×5=10; 10
e)27+6=33≡1; 1
f)72=49≡5; 5
g)43≡1, 441≡42; 7
h)cycle of length 4, 783≡73; 3
i)212≡1, 2405≡29; 5
j)last nonzero remainder; 23
The move that decides everything: reduce before multiplying or raising to a power. Congruence is compatible with sums, products and therefore powers: you may replace every number by its remainder at any step, and you NEVER have to write out 2,9992 or 2405.
a)
347=9×38+5, so 347mod9=5. Check: 9×38=342 and 342+5=347.
b)
You need a quotient that leaves a remainder in [0,7): −38=7×(−6)+4, so −38mod7=4. Pitfall: −38=7×(−5)−3 gives −3, which is not a remainder; add 7 and you get 4 back.
c)
482=8×60+2, 937=8×117+1 and 615=8×76+7. The sum is therefore congruent to 2+1+7=10≡2(mod8). Check: 482+937+615=2,034=8×254+2.
d)
123=11×11+2 and 456=11×41+5, so 123×456≡2×5=10(mod11). The remainder is 10, without ever computing 56,088.
e)
Replace n by its remainder 3: 33+2×3=27+6=33=4×8+1, so the remainder is 1, whatever the integer n≡3(mod4). Check with n=7: 343+14=357=4×89+1.
f)
2,999=11×272+7, so 2,9992≡72=49=44+5≡5(mod11). Pitfall: squaring first, which forces you to divide 8,994,001 by 11.
g)
The cycle: 41≡4, 42=16≡7, 43≡4×7=28≡1(mod9). Its length is 3, and 41=3×13+2, so 441=(43)13×42≡1×7=7(mod9). Pitfall: reducing the exponent modulo 9, the modulus, instead of the length of the cycle.
h)
The cycle modulo 10: 7, 49≡9, 9×7=63≡3, 3×7=21≡1, of length 4. Since 83=4×20+3, 783≡73≡3(mod10): the last digit is 3.
i)
13 is prime and does not divide 2, so 212≡1(mod13). Now 405=12×33+9, so 2405≡29=512=13×39+5≡5(mod13). Pitfall: reducing the exponent modulo 13 instead of 13−1=12, which gives 405≡2 and a wrong 4.
j)
1,518=3×391+345; 391=1×345+46; 345=7×46+23; 46=2×23+0. The last nonzero remainder is 23, so the GCD is 23. Check: 1,518=23×66 and 391=23×17.
Check: verify a remainder by recomputing nq+r, and make sure it falls in [0,n); verify a cycle of powers by making sure it really comes back to 1 before using it to reduce the exponent.
Tick the exercises you have done or want to review: a free account, no password, keeps your ticks and your right answers from one visit to the next, tells you which chapter to tackle next and lets you ask for the exercise you are missing. Create your space, an email is enough.
Calculations: inverses and congruences to solve
Exercise 0.2: Invert and solve modulo n: eight direct calculations
For each line, give the inverse, the solution or the number asked for. An inverse and a solution are written like a remainder, between 0 and n−1.
A single method: first compute gcd(a,n), otherwise you cannot know whether the inverse exists; then find the inverse by the extended Euclidean algorithm, or by trial when n is small; finally, to solve ax≡b(modn), multiply both sides by the inverse of a. If d=gcd(a,n)>1, the congruence has a solution only if d divides b: you then divide everything by d, modulus included, and you get d solutions modulo n.
a) The inverse of 5 modulo 11.
b) The inverse of 4 modulo 15.
c) Does the inverse of 9 modulo 24 exist?
d) The inverse of 23 modulo 60, by the extended Euclidean algorithm.
e) The Bézout coefficients u and v such that 75u+32v=1, with 0<u<32.
f) The solution of 5x≡3(mod17).
g) The solution of 7x≡5(mod18).
h) The congruence 8x≡12(mod28): the number of solutions modulo 28, then the smallest positive solution.
Show the solution
Answers
a)5×9≡1(mod11); 9
b)4×4≡1(mod15); 4
c)gcd(9,24)=3; no
d)1=5×60−13×23; 47
e)1=3×75−7×32; u=3, v=−7
f)x≡7×3; x=4
g)x≡13×5; x=11
h)2x≡3(mod7); 4 solutions, the smallest is 5
The move that decides everything: the GCD BEFORE any calculation. gcd(a,n)=1 tells you that the inverse exists and that the congruence has a single solution modulo n; otherwise, the inverse does not exist, and the congruence has several solutions or none.
a)
gcd(5,11)=1. By trial: 5×9=45=44+1≡1(mod11), so the inverse is 9. Pitfall: answering 51 or 0.2; a modular inverse is an INTEGER between 0 and 10.
b)
gcd(4,15)=1, and 4×4=16=15+1≡1(mod15): 4 is its own inverse modulo 15.
c)
No: gcd(9,24)=3=1. All the multiples of 9 are multiples of 3, and so are their remainders modulo 24, so none of them equals 1. Pitfall: believing that every nonzero number smaller than the modulus is invertible; that is true only when the modulus is prime.
d)
Euclid: 60=2×23+14; 23=1×14+9; 14=1×9+5; 9=1×5+4; 5=1×4+1. Back substitution: 1=5−4=5−(9−5)=2×5−9=2(14−9)−9=2×14−3×9=2×14−3(23−14)=5×14−3×23=5(60−2×23)−3×23=5×60−13×23. So −13×23≡1(mod60), and the inverse is −13+60=47. Check: 23×47=1,081=18×60+1. Pitfall: stopping at −13, which is not written as a remainder.
e)
Euclid: 75=2×32+11; 32=2×11+10; 11=1×10+1. Back substitution: 1=11−10=11−(32−2×11)=3×11−32=3(75−2×32)−32=3×75−7×32. So u=3 and v=−7, and u does lie between 0 and 32. Check: 225−224=1. Pitfall: expanding every product at each step, which makes you lose track of 75 and 32; substitute one division at a time, from the bottom up.
f)
17 is prime, so the inverse of 5 exists: 5×7=35=34+1, it is 7. Multiply both sides by 7: x≡7×3=21≡4(mod17). Check: 5×4=20=17+3.
g)
gcd(7,18)=1, and 7×13=91=5×18+1, so the inverse of 7 modulo 18 is 13. Then x≡13×5=65=3×18+11≡11(mod18). Check: 7×11=77=4×18+5. Pitfall: trying to divide 5 by 7; modulo 18, you never divide, you multiply by the inverse.
h)
d=gcd(8,28)=4, and 4 divides 12: there are 4 solutions modulo 28. Divide everything by 4, modulus included: 2x≡3(mod7). The inverse of 2 modulo 7 is 4, so x≡12≡5(mod7). Modulo 28, the solutions are 5, 12, 19 and 26, and the smallest is 5. Check: 8×5=40=28+12. Pitfall: dividing 8 and 12 by 4 while keeping the modulus 28, which gives 2x≡3(mod28), with no solution since 2x−3 is odd.
Check: an inverse u of a is checked by computing aumodn, which must equal 1; a solution is checked by substituting it into the congruence; Bézout coefficients, by recomputing au+bv. Each check costs one multiplication and one division.
Part A: the basics (/50)
Exercise 1: Euclidean division and the remainder
All of modular arithmetic rests on an operation you think you have known since elementary school. The thread of the set: we no longer care about the QUOTIENT but about the REMAINDER, and a remainder is always nonnegative, even when the dividend is not.
a) State the Euclidean division theorem for a divided by n, specifying the bounds on the remainder.
b) Compute the quotient and the remainder of 2026 divided by 12, then of 2026 divided by 7.
c) Compute −17mod5 and −1mod12. Watch the sign of the remainder.
d) In Python, the operator % always returns a remainder with the sign of the DIVISOR; in C and Java, it returns a remainder with the sign of the DIVIDEND. What is −17%5 in each language?
e) If today is a Tuesday, what day will it be in 100 days? And what day was it 100 days ago?
Show the solution
Answers
a)a=nq+r with 0≤r<n, the pair (q,r) being unique
b)2026=12(168)+10 and 2026=7(289)+3
c)−17mod5=3 and −1mod12=11
d)3 in Python, −2 in C and Java
e)Thursday in 100 days, Sunday 100 days ago
a)
For every integer a and every integer n>0, there is a UNIQUE pair of integers (q,r) such that a=nq+r with 0≤r<n. Two things matter in this statement: existence and uniqueness on the one hand, and above all the BOUNDS on the remainder, between 0 included and n excluded. Those bounds are what rule out a negative remainder, and they are what students forget when a<0.
b)
2026=12×168+10, so q=168 and r=10: this is what the clock in the figure shows, where you go around 168 times before stopping on 10. For 7: 2026=7×289+3, so q=289 and r=3. Always check by recomputing nq+r, which takes one second and catches every multiplication-table error.
c)
For −17 divided by 5: the quotient must be chosen so that the remainder falls in [0,5). With q=−4: −17=5×(−4)+3, so r=3. The knee-jerk division −17=5×(−3)−2 would give r=−2, which is not a valid remainder. For −1 divided by 12: −1=12×(−1)+11, so r=11: it is the last notch on the clock before going back through zero, which is exactly the picture to remember.
d)
In PYTHON, −17%5 equals 3, the mathematical convention, since the divisor 5 is positive. In C and in JAVA, the operator returns −2, with the sign of the dividend. Both languages compute correctly; they simply do not answer the same question. The practical consequence for a programmer: circular indexing code written in C must correct the negative case, for example with ((a%n)+n)%n, whereas the same code in Python works as is.
e)
Days of the week are counted modulo 7. 100=7×14+2, so in 100 days you move forward 2 notches: Tuesday plus 2 gives THURSDAY. 100 days ago, you move back 2 notches: Tuesday minus 2 gives SUNDAY. You can also write −100mod7=5 and move forward 5, which also gives Sunday, since moving forward 5 or back 2 comes to the same thing on a cycle of 7.
Exercise 2: Congruences and their rules of computation
Writing a≡b(modn) is not decorative notation: it is an EQUIVALENCE relation compatible with addition and multiplication, which lets you compute with remainders as you do with numbers.
a) Give the definition of a≡b(modn) in terms of divisibility, then in terms of remainders.
b) Prove that congruence modulo n is reflexive, symmetric and transitive.
c) Prove that if a≡b and c≡d modulo n, then a+c≡b+d and ac≡bd.
d) Compute the remainder of 174 modulo 5 without ever raising 17 to the power 4.
e) Does compatibility hold for DIVISION? Test it on 6≡12(mod6) divided by 2.
Show the solution
Answers
a)n divides a−b, which amounts to saying that a and b leave the same remainder modulo n
b)Reflexive, symmetric and transitive: it is an equivalence relation, with n classes
c)(a+c)−(b+d)=(k+ℓ)n and ac−bd=(ck+bℓ)n, by adding and subtracting bc
d)17≡2, so 174≡24=16≡1(mod5)
e)NO: 6≡12(mod6), but 3≡6(mod6). You cancel k only if k is relatively prime to n
a)
DIVISIBILITY: a≡b(modn) means that n divides a−b. REMAINDERS: it means that a and b leave the SAME remainder when divided by n. The two formulations are equivalent, and you must be able to switch from one to the other: the first is used in proofs, the second in calculations.
b)
REFLEXIVE: n divides a−a=0, so a≡a. SYMMETRIC: if n divides a−b, it also divides b−a=−(a−b), so b≡a. TRANSITIVE: if n divides a−b and b−c, it divides their sum (a−b)+(b−c)=a−c, so a≡c. These three properties make congruence an equivalence relation, whose classes are exactly the n sets of numbers that leave the same remainder.
c)
By hypothesis a−b=kn and c−d=ℓn for two integers k and ℓ. SUM: (a+c)−(b+d)=(a−b)+(c−d)=(k+ℓ)n, a multiple of n ✓. PRODUCT: ac−bd=ac−bc+bc−bd=c(a−b)+b(c−d)=(ck+bℓ)n, a multiple of n ✓. The trick for the product is to add and subtract bc, which brings out both hypotheses; it is worth remembering, it will come up again elsewhere.
d)
17≡2(mod5), since 17=5×3+2. Compatibility with products lets you raise both sides to the power 4: 174≡24=16≡1(mod5). The remainder is therefore 1, while 174=83,521, a number you never had to write down. That is the whole point of modular computation: reduce BEFORE multiplying, never after.
e)
NO, division is not compatible in general. We do have 6≡12(mod6), since 12−6=6. Dividing both sides by 2: 3≡6(mod6) would be false, because 6−3=3 is not a multiple of 6. The correct rule is that you may cancel a factor k provided you ALSO divide the modulus by gcd(k,n), or if k is relatively prime to n. This restriction is what makes exercise 5 on inverses necessary.
Exercise 3: Computing in the integers modulo n
The set of remainders modulo n is a closed world where you add and multiply without ever leaving it. Its multiplication table reveals a deep difference depending on whether n is prime or not.
a) Write out the addition table modulo 5. Does each row contain each element exactly once?
b) The figure gives the multiplication table modulo 7, without zero. Check three entries at random and say what you observe on each row.
c) Write out the multiplication table modulo 6, without zero. What is different?
d) Find two nonzero elements whose product is zero modulo 6. What are such elements called, and why are there none modulo 7?
e) State the general rule that distinguishes the two cases.
Show the solution
Answers
a)Yes: adding a fixed number is a permutation, for every n
b)3×5≡1, 4×6≡3, 2×4≡1: each row contains 1, so every nonzero element is invertible
c)The row of 2 is 2,4,0,2,4 and the row of 3 is 3,0,3,0,3: zeros, and no 1
d)2×3=6≡0(mod6): ZERO DIVISORS, impossible modulo 7, which is prime
e)a is invertible modulo n if and only if gcd(a,n)=1; all of them are if and only if n is prime
a)
The addition table modulo 5 is obtained by adding and then taking the remainder. Its first row, the row of 0, is 0,1,2,3,4; the row of 1 is 1,2,3,4,0; the row of 2 is 2,3,4,0,1, and so on. YES, each row contains each element exactly once: adding a fixed number is a permutation, because you can always go back by adding its negative. This is true for every n, prime or not.
b)
Checks: 3×5=15=14+1≡1; 4×6=24=21+3≡3; 2×4=8≡1. You observe that EACH ROW contains each nonzero element exactly once, and in particular that it contains 1. In other words, every nonzero element has an INVERSE modulo 7, which is far from obvious and will be the subject of exercise 5.
c)
Modulo 6, the row of 2 gives 2,4,0,2,4 for the multipliers 1,2,3,4,5: it contains a ZERO even though neither factor is zero, and it does NOT contain 1. The row of 3 gives 3,0,3,0,3, even worse. Only the rows of 1 and 5 contain 1. The table is therefore very different from the one modulo 7.
d)
2×3=6≡0(mod6), with 2=0 and 3=0. Such elements are called ZERO DIVISORS. They appear because 6 factors as 2×3: the product collects all the factors of 6 and becomes a multiple of 6. Modulo 7 this is impossible, because 7 is PRIME: if 7 divides a product ab, it divides a or b, a property that characterizes precisely the prime numbers.
e)
An element a is invertible modulo n if and only if gcd(a,n)=1. Consequently, ALL the nonzero elements are invertible if and only if n is PRIME, in which case the integers modulo n form a field. If n is composite, the elements that share a factor with n are not invertible and are zero divisors. This distinction is what makes prime moduli so valuable in cryptography and in error-correcting codes.
Exercise 4: The GCD and the Euclidean algorithm
The oldest algorithm still in use dates from 300 BCE, and it is still the one processors execute. Its justification fits in one line of congruence.
def pgcd(a, n):
while n != 0:
a, n = n, a % n
return a
def pgcd_rec(a, n):
if n == 0:
return a
return pgcd_rec(n, a % n)
a) Prove that gcd(a,n)=gcd(n,amodn), which justifies the algorithm.
b) Run the Euclidean algorithm on 1071 and 462, as in the figure, and give the GCD.
c) Run it on 240 and 46.
d) Write the algorithm in Python, as an iterative version and then as a recursive version.
e) At most how many steps does the algorithm take? Which pair of numbers is the worst case?
Show the solution
Answers
a)d divides a and n if and only if it divides n and r: same common divisors, same GCD
b)147, then 21, then 0: gcd(1071,462)=21
c)10,6,4,2,0: gcd(240,46)=2, in five steps instead of three
d)Iterative: (a,n)→(n,amodn). Recursive: same equality, base case n=0
e)At most about 5 times the number of digits of the smaller one; worst case, two consecutive Fibonacci numbers
a)
Let r=amodn, so a=nq+r. If d divides a and n, then it divides a−nq=r, so it divides n and r. Conversely, if d divides n and r, it divides nq+r=a, so it divides a and n. The two pairs therefore have EXACTLY the same common divisors, and in particular the same greatest one. The algorithm then consists in replacing the pair by a smaller pair until the remainder becomes zero.
b)
1071=2×462+147; 462=3×147+21; 147=7×21+0. The last nonzero remainder is 21, so gcd(1071,462)=21. The figure gives the geometric reading: you tile the rectangle with smaller and smaller squares, and the side of the last square is the GCD. Three divisions were enough, where searching for the divisors of 1071 would have taken many more.
c)
240=5×46+10; 46=4×10+6; 10=1×6+4; 6=1×4+2; 4=2×2+0. The GCD is 2. Note that these two numbers, smaller than those in question b, took MORE steps: the number of steps does not depend on the size of the numbers but on the sequence of quotients, all equal to 1 in the worst case.
d)
Both versions fit in four lines and can be read straight off question a. The ITERATIVE version replaces the pair (a,n) by (n,amodn) on every pass and stops when the second term becomes zero; the first one then holds the GCD. The RECURSIVE version translates the same equality word for word, with the base case n=0 returning a. The recursive version is more readable and its number of calls is the number of divisions, so it runs no risk of stack overflow for integers of reasonable size; the iterative version avoids that risk altogether and remains the form used in libraries.
e)
Lamé's theorem states that the number of steps is at most about 5 times the number of DIGITS of the smaller of the two numbers, which is remarkably few: fewer than 50 steps for ten-digit numbers. The WORST CASE is that of two consecutive FIBONACCI numbers, such as 89 and 55, for which every quotient equals 1, so the algorithm cannot speed anything up. It is a rare example where the worst case of an algorithm is known explicitly.
Exercise 5: The modular inverse and linear congruences
You do not divide modulo n: you multiply by an inverse, when there is one. Deciding whether it exists, then computing it, is the central skill of the chapter.
a) Give the condition for the inverse of a modulo n to exist, and find the inverse of 3 modulo 7 by exhaustive search.
b) Find the inverse of 4 modulo 9, then show that 4 has no inverse modulo 8.
c) Solve 3x≡2(mod7).
d) Solve 4x≡6(mod10). How many solutions are there modulo 10?
e) Solve 6x≡4(mod9), or show that there is no solution.
Show the solution
Answers
a)The inverse exists if and only if gcd(a,n)=1; the inverse of 3 modulo 7 is 5
b)4−1=7 modulo 9; modulo 8, gcd(4,8)=4, so no inverse
c)x≡3(mod7), a unique solution
d)x=4 and x=9: two solutions modulo 10, as many as the GCD
e)No solution: gcd(6,9)=3 does not divide 4
a)
The inverse of a modulo n exists if and only if gcd(a,n)=1, that is, if a and n are RELATIVELY PRIME. For 3 modulo 7: gcd(3,7)=1, the inverse exists. Try: 3×1=3, 3×2=6, 3×3=9≡2, 3×4=12≡5, 3×5=15≡1 ✓. The inverse of 3 modulo 7 is therefore 5.
b)
gcd(4,9)=1, the inverse exists: 4×7=28=27+1≡1(mod9), so the inverse is 7. Modulo 8, on the other hand, gcd(4,8)=4=1: there is no inverse. You can check it on the table: the multiples of 4 modulo 8 take only the values 0 and 4, never 1. The same number 4 is therefore invertible or not depending on the modulus, which shows that invertibility is a property of the PAIR and not of the number.
c)
Multiply both sides by the inverse of 3, that is, by 5: 5×3x≡5×2, that is, 15x≡10, that is, x≡3(mod7) since 15≡1 and 10≡3. Check: 3×3=9≡2(mod7) ✓. There is therefore exactly ONE solution modulo 7, as always when the coefficient is invertible.
d)
gcd(4,10)=2, which is not 1: you cannot invert. But 2 DIVIDES the right-hand side 6, so the congruence has solutions. Divide everything by 2, MODULUS INCLUDED: 2x≡3(mod5). This time gcd(2,5)=1 and the inverse of 2 modulo 5 is 3, hence x≡3×3=9≡4(mod5). Modulo 10, this gives TWO solutions, x=4 and x=9. Check: 4×4=16≡6 ✓ and 4×9=36≡6 ✓. The number of solutions equals the GCD, here 2.
e)
gcd(6,9)=3, and 3 DOES NOT DIVIDE 4. The congruence therefore has NO solution. You can see it directly: 6x is always a multiple of 3, so its remainder modulo 9 belongs to {0,3,6}, and 4 is not there. General rule: ax≡b(modn) has solutions if and only if gcd(a,n) divides b, and then it has exactly gcd(a,n) of them modulo n.
Part B: problems and reasoning (/50)
Exercise 6: Bézout's identity and the extended Euclidean algorithm
The Euclidean algorithm gives the GCD; if you keep track of the calculations, it gives much more: two coefficients that express this GCD as a combination of the two starting numbers. That is what makes computing an inverse fast.
def euclide_etendu(a, b):
r, u, v = a, 1, 0
rp, up, vp = b, 0, 1
while rp != 0:
q = r // rp
r, u, v, rp, up, vp = rp, up, vp, r - q*rp, u - q*up, v - q*vp
return r, u, v
def inverse(a, n):
d, u, _ = euclide_etendu(a, n)
if d != 1:
return None
return u % n
a) State Bézout's theorem and say what it becomes when the two numbers are relatively prime.
b) Find two integers u and v such that 1071u+462v=21, by working back up the divisions of exercise 4.
c) Compute the inverse of 17 modulo 43 by the extended Euclidean algorithm.
d) Compare the cost of this method with that of the exhaustive search of exercise 5.
e) Write the extended Euclidean algorithm in Python.
Show the solution
Answers
a)au+bv=gcd(a,b), which becomes au+bv=1 for two relatively prime numbers
b)u=−3 and v=7: −3(1071)+7(462)=21
c)1=2(43)−5(17), so the inverse of 17 modulo 43 is −5≡38
d)Exhaustive: of the order of n, exponential. Extended Euclid: linear in the number of digits
e)The algorithm returns the triple GCD, u, v, updating the coefficients on every pass
a)
For all integers a and b not both zero, there exist integers u and v such that au+bv=gcd(a,b). When a and b are RELATIVELY PRIME, this becomes au+bv=1, and conversely: such an expression proves that the GCD is 1, since every common divisor divides the left-hand side and therefore divides 1. It is the most convenient criterion for proving that two numbers are relatively prime.
b)
Work back up the divisions. From 462=3×147+21 you get 21=462−3×147. From 1071=2×462+147 you get 147=1071−2×462. Substituting: 21=462−3(1071−2×462)=462−3×1071+6×462=7×462−3×1071. So u=−3 and v=7. Check: −3×1071+7×462=−3213+3234=21 ✓. The discipline is to substitute ONE division at a time, from the bottom up, without ever expanding the products before the end.
c)
Euclid first: 43=2×17+9; 17=1×9+8; 9=1×8+1; 8=8×1+0. The GCD is 1, the inverse exists. Back substitution: 1=9−8; then 8=17−9 gives 1=9−(17−9)=2×9−17; then 9=43−2×17 gives 1=2(43−2×17)−17=2×43−5×17. So −5×17≡1(mod43), and the inverse of 17 is −5, that is, 38 modulo 43. Check: 17×38=646=43×15+1 ✓.
d)
Exhaustive search tries the values 1,2,…,n−1 until it hits 1: its cost is of the order of n multiplications, so EXPONENTIAL in the number of digits of n. The extended Euclidean algorithm, on the other hand, performs at most about 5 times the number of digits in divisions, so a cost LINEAR in that number. On a 600-digit modulus like those of RSA encryption, the first method is impossible within the age of the Universe and the second takes a fraction of a millisecond. That gap is what makes modern cryptography practical.
e)
The code below returns the triple made of the GCD and the two Bézout coefficients, updating the coefficients on every pass through the loop.
Exercise 7: Fermat's little theorem and fast exponentiation
Computing 3100 and then taking the remainder is impossible by hand and costly for a machine. Two ideas bring the calculation down to a few lines: a theorem that shortens the exponent, and an algorithm that breaks it down in binary.
def puissance_mod(a, e, n):
resultat = 1
a = a % n
while e > 0:
if e % 2 == 1:
resultat = (resultat * a) % n
a = (a * a) % n
e = e // 2
return resultat
a) State Fermat's little theorem and check it for a=3 and p=7 by writing out the cycle of powers, as in the figure.
b) Compute the remainder of 3100 modulo 7.
c) Compute the remainder of 21000 modulo 11.
d) Compute 711 modulo 13 by fast exponentiation, writing the exponent in binary, then check the result with Fermat's theorem.
e) How many multiplications does fast exponentiation take for a 600-digit exponent, compared with the naive method?
Show the solution
Answers
a)ap−1≡1(modp); the cycle of 3 modulo 7 is 3,2,6,4,5,1, of length 6
b)100≡4(mod6), so 3100≡34=81≡4(mod7)
c)210≡1, so 21000≡1(mod11)
d)711≡2(mod13), confirmed by Fermat: 711≡7−1=2
e)About 3986 multiplications instead of 10600
a)
If p is PRIME and p does not divide a, then ap−1≡1(modp). Check with a=3 and p=7: 31=3, 32=9≡2, 33≡6, 34≡18≡4, 35≡12≡5, 36≡15≡1 ✓. The figure shows this cycle of length 6: the six nonzero values all appear, exactly once, before the cycle closes back on 1.
b)
The theorem lets you reduce the EXPONENT modulo p−1=6. Since 100=6×16+4, you get 3100=(36)16×34≡116×34=81≡4(mod7). The remainder is 4. Remember the rule and its limit: reduce the BASE modulo p, and the EXPONENT modulo p−1, never the other way around.
c)
p=11 is prime and does not divide 2, so 210≡1(mod11). Since 1000=10×100, you get 21000=(210)100≡1100=1(mod11). The remainder is 1. A number with more than three hundred digits has been reduced in one line.
d)
11 is written 1011 in binary, so 711=78×72×71. Compute the successive squares modulo 13: 71≡7; 72=49≡10; 74≡102=100≡9; 78≡92=81≡3. Product: 3×10×7=210≡210−208=2(mod13). CHECK with Fermat: 712≡1, so 711≡7−1, the inverse of 7 modulo 13. Now 7×2=14≡1, so this inverse is 2 ✓. Two independent paths, same answer.
e)
The naive method takes N−1 multiplications for aN, that is, about 10600 operations for a 600-digit exponent: that is unthinkable, the Universe has existed for only about 1017 seconds. Fast exponentiation takes at most 2log2N, that is, about 2×600×3.3219≈3986 multiplications, a matter of a few milliseconds. This difference, and it alone, is what lets a browser open an encrypted connection in the blink of an eye.
Exercise 8: Five statements to correct
Each of the following five statements is FALSE. Say why and give the correct statement.
1) “−17mod5 equals −2, since −17=5×(−3)−2.”
2) “If ac≡bc(modn) then a≡b(modn), just as with integers.”
3) “Every nonzero element has an inverse modulo n.”
4) “To compute 3100 modulo 7, you reduce the exponent modulo 7.”
5) “The Euclidean algorithm becomes slow when the numbers are large, so it should be avoided in cryptography.”
Show the solution
Answers
1)FALSE: −2 is not a valid remainder. −17=5(−4)+3, so the remainder is 3
2)FALSE: you cancel only an invertible c. 2×3≡4×3(mod6) and yet 2≡4
3)FALSE: only 1 and 5 are invertible modulo 6. You need gcd(a,n)=1
4)FALSE: the exponent is reduced modulo p−1=6, not modulo 7. The answer is 4, not 2
5)FALSE: fewer than 3000 divisions for two 600-digit numbers. Extended Euclid is a pillar of RSA
1)
FALSE. The equality written is correct, but −2 is NOT a valid remainder: Euclidean division requires 0≤r<n, so here 0≤r<5. You must choose q=−4: −17=5×(−4)+3, so −17mod5=3. That is also what Python returns. The confusion comes from languages such as C or Java, whose operator follows another convention and does return −2: the language is not wrong, it simply does not compute the Euclidean remainder.
2)
FALSE. You may cancel c only if c is INVERTIBLE modulo n, that is, relatively prime to n. Counterexample: 2×3≡4×3(mod6), because 6≡12≡0, and yet 2≡4(mod6). The correct rule is that ac≡bc(modn) implies a≡b(modgcd(c,n)n), which gives back the original modulus only if the GCD is 1.
3)
FALSE. The inverse of a modulo n exists if and only if gcd(a,n)=1. Modulo 6, the elements 2, 3 and 4 have no inverse, since they share a factor with 6; only 1 and 5 have one. The statement is true only when n is PRIME, in which case every nonzero element is relatively prime to n by definition.
4)
FALSE, and the error is only one notch off, but it changes everything. Fermat's little theorem gives 36≡1(mod7), so it is modulo p−1=6 that the exponent is reduced, not modulo 7. We have 100≡4(mod6), so 3100≡34=81≡4. Reducing modulo 7 would give 100≡2, so 32=9≡2, a wrong result. Rule: the base is reduced modulo n, the exponent modulo p−1.
5)
FALSE, it is exactly the opposite. Lamé's theorem guarantees that the number of divisions is at most about 5 times the number of DIGITS of the smaller number: for two 600-digit numbers, that makes fewer than 3000 divisions, a fraction of a millisecond. This efficiency is what makes it possible to compute the decryption exponent of an RSA key, and the extended Euclidean algorithm is therefore, on the contrary, a pillar of modern cryptography.
Exercise 9: Problem: check digits, from the ISBN to the card number
Every code you type, a book number, a credit card number or a social insurance number, carries one extra digit whose only job is to catch your typing mistakes. That digit is a remainder.
a) A thirteen-digit ISBN code satisfies the condition that the sum of its thirteen digits weighted by 1,3,1,3,… is a multiple of 10. The figure gives the first twelve digits and a weighted sum of 97. Compute the check digit c.
b) Check that the complete code does satisfy the condition.
c) A user mistypes one digit of the code. Prove that the error is always detected.
d) The user swaps two adjacent digits. Show that the error is detected EXCEPT in one specific case, and give an example.
e) The ten-digit ISBN code used the weights 10,9,…,1 and the modulus 11, with an X for the value 10. Why does 11 detect errors better than 10?
Show the solution
Answers
a)The check digit is 3, since 97+3=100
b)100≡0(mod10): the code is valid
c)The sum changes by w(d′−d), which cannot be a multiple of 10: 3 is relatively prime to 10
d)The sum changes by 2(a−b): the error escapes when ∣a−b∣=5
e)11 is PRIME: no zero divisors, so every transposition is detected, at the cost of the character X
a)
In the ISBN-13 convention, the check digit is the thirteenth digit and it carries the weight 1, since the weights alternate starting with 1. So 97+c must be a multiple of 10, with 0≤c≤9. The next multiple of 10 above 97 is 100, so c=3. General formula: c=(10−(Smod10))mod10, and the last modulo is there only for the case where S is already a multiple of 10, where the check digit is 0 and not 10.
b)
Total sum: 97+3=100, which is indeed a multiple of 10 since 100≡0(mod10) ✓. The complete code is therefore valid, and it is this check, not the computation of the check digit, that a bookstore checkout performs at every scan.
c)
Suppose a digit of weight w changes from d to d′. The sum changes by w(d′−d), with w equal to 1 or 3 and d′−d between −9 and 9, nonzero. For the error to go unnoticed, you would need w(d′−d)≡0(mod10). With w=1: you would need 10 to divide d′−d, impossible for a nonzero difference smaller than 10. With w=3: since gcd(3,10)=1, you can cancel the 3 and you are back to the same impossibility. Every error in ONE digit is therefore detected, and that is precisely because 3 is relatively prime to 10.
d)
If two adjacent digits a and b, carrying the weights 1 and 3, are swapped, the sum goes from a+3b to b+3a, a change of 2(a−b). The error escapes the check if 2(a−b)≡0(mod10), that is, if a−b≡0(mod5), so if ∣a−b∣=5. EXAMPLE: the code 9782723456807 is valid, with sum 103 plus a check digit 7, that is, 110. Swapping the 2 and the 7 in positions 4 and 5 gives 9782→9787…, whose weighted sum becomes 113 plus 7, that is, 120, again a multiple of 10: the typing mistake is NOT detected. This is the known weakness of ISBN-13, and it is a serious flaw, since swapping two adjacent digits is the most common data-entry error.
e)
Because 11 is PRIME while 10 is not. Modulo a prime number, every nonzero coefficient is invertible and there are no zero divisors: swapping two digits a and b of weights wi and wj changes the sum by (wi−wj)(a−b), and since wi−wj is nonzero and lies between −9 and 9, 11 does not divide it, so this change can vanish modulo 11 only if 11 divides a−b, that is, only if a=b: there was no error. ISBN-10 therefore detected ALL transpositions of two digits, including non-adjacent ones. The price to pay is that a check digit can equal 10, hence the character X, which was judged awkward for barcodes and explains why the system was abandoned in favour of the weaker but more convenient ISBN-13.
Exercise 10: Problem: RSA encryption with small numbers
Everything so far comes together here. RSA encryption rests on the modular inverse, Fermat's little theorem and fast exponentiation, and it fits on one page as long as you take tiny prime numbers.
a) Choose p=11 and q=13. Compute the modulus n=pq and Euler's totient function φ(n)=(p−1)(q−1).
b) Choose the public exponent e=7. Check that it is suitable, then compute the private exponent d, the inverse of e modulo φ(n).
c) Encrypt the message M=9 by C=Memodn.
d) Decrypt C by M=Cdmodn and check that you get 9 back.
e) Why is an attacker who knows n and e unable to recover d in practice, when the method is obvious in principle?
Show the solution
Answers
a)n=143 and φ(n)=120
b)gcd(7,120)=1 and d=103, since 7×103=721=6(120)+1
c)C=48
d)M=48103mod143=9, by Euler's theorem
e)You would have to factor n; with 600 digits, no known algorithm can, and this difficulty is observed, not proved
a)
n=11×13=143. φ(n)=(11−1)(13−1)=10×12=120. The modulus n is PUBLIC, it is part of the public key; φ(n) is SECRET, because knowing it amounts to knowing the private key, as question e will show.
b)
You need gcd(e,φ(n))=1: here gcd(7,120)=1, because 7 is prime and does not divide 120. The exponent is suitable. Look for d such that 7d≡1(mod120). By extended Euclid or by guided trial: 7×103=721=720+1=6×120+1, so d=103. Check: 721mod120=1 ✓.
c)
C=97mod143. Proceed by fast exponentiation, reducing at every step: 92=81; 94=812=6561, and 6561=143×45+126 so 94≡126; finally 97=94×92×9≡126×81×9. Compute 126×81=10,206≡53 since 10,206=143×71+53, then 53×9=477≡48 since 477=143×3+48. So C=48. The encrypted message is 48.
d)
M=48103mod143. The number 48103 has 174 digits and of course you never write it out: fast exponentiation reduces modulo 143 after every squaring, and you get M=9 ✓, the original message. The theorem that guarantees this round trip is Euler's theorem, a generalization of Fermat's little theorem: since ed≡1(modφ(n)), you get Med≡M(modn).
e)
In principle the method is immediate: you only have to factor n to recover p and q, deduce φ(n), then d by extended Euclid. Here, with n=143, a student finds 11×13 in a few seconds. But in real use n has about 600 digits, and no known algorithm can factor such a number in a reasonable time: the best ones would take billions of years. All the security therefore rests on an ASYMMETRY of cost: multiplying two large primes is instantaneous, recovering the factors is not. Note that none of this is proved: nobody knows how to prove that factoring is hard; we only observe that nobody has found a way to make it easy.
Something missing from this set?A type of exercise your teacher gives, a problem you are stuck on, a mistake you spotted: tell me, I read every request.
Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space.
Rather not create an account? Write to me directly.
Looking for a 201-N11 Mathematics for Computer Science tutor in Montreal?
Get in touch for a first session. We work on modular arithmetic at the level of real CEGEP tests, from congruences all the way to the extended Euclidean algorithm and RSA.