Mathematics for Computer Science 201-N11 • CEGEP, Montreal

Revision sheet: modular arithmetic (201-N11)

Modular arithmetic is not hard to understand: it is clock arithmetic. It is hard to HANDLE, because three operations out of four behave as usual and the fourth one, division, does not exist at all.

This sheet is written for CEGEP students taking 201-N11 in a computer science program (Computer Science Technology, Techniques de l'informatique), and for anyone who comes to cryptography through mathematics. The corrected exercise set on the same chapter then puts every reflex to the test.

Mark this sheet as read or add it to your favourites: a free account, no password, keeps your read sheets and favourites from one visit to the next, tells you which chapter to tackle next and lets you ask for the method or exercise you are missing. Create your space, an email is enough.

The thread of the chapter

Working modulo nn means replacing an integer by its remainder and computing with remainders. Addition and multiplication go through without trouble; DIVISION never does, and that is where almost every lost mark comes from.

This chapter is part of 201-N11, Mathematics for Computer Science

The essentials

Euclidean division: the remainder is ALWAYS nonnegative

  • • For n>0n>0, there is a unique pair (q,r)(q, r) such that a=nq+ra=nq+r with 0≤r<n0\leq r<n.
  • • This also holds for a<0a<0: −17=5×(−4)+3-17=5\times(-4)+3, so −17 mod 5=3-17\bmod 5=3, not −2-2.
  • • The %\% operator of the C, C++ and Java languages does NOT follow this convention and returns a remainder with the sign of the dividend. Python does follow it.
  • • The quotient is the FLOOR of an\frac{a}{n}: ⌊−175⌋=−4\left\lfloor\frac{-17}{5}\right\rfloor=-4, not −3-3.
−25−20−15−10−505r = 3a = −17−20 = 5 × (−4)
Dividing −17-17 by 55 gives q=−4q=-4 and r=3r=3, never r=−2r=-2: the remainder is counted from the multiple located to the LEFT, so it is always nonnegative.

On a test, a negative remainder is a wrong answer even when the calculator displays it. Adding the modulus until you land back in [0,n−1][0, n-1] takes five seconds.

Congruences: what goes through, and what does not

  • • a≡b(modn)a\equiv b\pmod n means that nn divides a−ba-b, in other words that aa and bb leave the same remainder.
  • • COMPATIBLE with addition, subtraction, multiplication and therefore powers: you reduce BEFORE computing, never after.
  • • NOT compatible with division: ab(modn)\frac{a}{b}\pmod n makes no sense in general.
  • • CANCELLATION: from ac≡bc(modn)ac\equiv bc\pmod n you can conclude a≡b(modn)a\equiv b\pmod n only if gcd⁡(c,n)=1\gcd(c,n)=1. Otherwise you must divide the modulus by gcd⁡(c,n)\gcd(c,n).
01234−3, 2, 7, 12: same remainder 2
Modulo 55, the wheel has only five positions: −3-3, 22, 77 and 1212 land on the same spot, they are congruent to one another, with the same remainder 22.

Reducing before computing is also what makes the calculations feasible: 750 mod 137^{50}\bmod 13 can be done in your head by reducing at every step, and overflows the calculator if you compute 7507^{50} first.

Euclid, Bézout, inverse: one machine with three uses

  • • EUCLID: gcd⁡(a,n)=gcd⁡(n,a mod n)\gcd(a,n)=\gcd(n,a\bmod n), repeated until the remainder is zero. The number of steps is at most about five times the number of digits.
  • • BÉZOUT: there exist uu and vv such that au+nv=gcd⁡(a,n)au+nv=\gcd(a,n), obtained by working back up the EXTENDED Euclidean algorithm.
  • • INVERSE: aa is invertible modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n)=1, and the inverse is then Bézout's uu, reduced into [0,n−1][0, n-1].
  • • All the nonzero elements are invertible IF AND ONLY IF nn is prime. Otherwise ZERO DIVISORS appear, such as 2×3≡0(mod6)2\times 3\equiv 0\pmod 6.
18181266
The Euclidean algorithm tiles the 48×1848\times 18 rectangle with squares: 48=2×18+1248=2\times 18+12, then 18=1×12+618=1\times 12+6, then 12=2×612=2\times 6. The side of the last square is the GCD, here 66.

Fermat, fast exponentiation and linear congruences

  • • FERMAT: if pp is PRIME and does not divide aa, then ap−1≡1(modp)a^{p-1}\equiv 1\pmod p.
  • • In practice: reduce the BASE modulo pp and the EXPONENT modulo p−1p-1. Two different moduli, the trap of the chapter.
  • • FAST EXPONENTIATION: ae mod na^{e}\bmod n in about 2log⁡2e2\log_{2}e multiplications, by squaring and reducing at every step.
  • • CONGRUENCE ax≡b(modn)ax\equiv b\pmod n: it has solutions if and only if d=gcd⁡(a,n)d=\gcd(a,n) divides bb, and then it has exactly dd of them modulo nn.
7 ≡ 77² ≡ 10squared7⁴ ≡ 9squared7⁸ ≡ 3squaredmodulo 13, reduced at every step
By successive squaring, 787^{8} modulo 1313 takes three multiplications, reduced at every step: you never handle a number larger than 12212^{2}.

For a modulus that is not prime, Euler's theorem replaces Fermat's, with φ(n)\varphi(n) in place of p−1p-1. Applying Fermat as is to a composite modulus is wrong.

The mistakes that cost marks

These are the errors I correct most often in session. Each one costs marks on a paper, even when the reasoning behind it is right.

1. Giving a negative remainder

1 mark each time, and a wrong encryption in an RSA problem

What not to write

“−17 mod 5=−2-17\bmod 5=-2, that's what my calculator shows.”

What to write

“−17=5×(−4)+3-17=5\times(-4)+3, so −17 mod 5=3-17\bmod 5=3: the remainder of a Euclidean division always belongs to [0,n−1][0, n-1].”

Why: The %\% operator of the C family of languages returns a remainder with the sign of the dividend, which is not the mathematical definition. In Python and in mathematics, the remainder is nonnegative.

2. Cancelling a factor that is not relatively prime to the modulus

the whole question

What not to write

“4×2≡4×5(mod6)4\times 2\equiv 4\times 5\pmod 6, so 2≡5(mod6)2\equiv 5\pmod 6.”

What to write

“gcd⁡(4,6)=2≠1\gcd(4, 6)=2\neq 1: no cancelling. Divide the MODULUS by 22 and you get 2≡5(mod3)2\equiv 5\pmod 3, which is true.”

Why: Modulo 66, 88 and 2020 do leave the same remainder 22, whereas 22 and 55 are not congruent: cancelling loses information as soon as the factor shares a divisor with the modulus.

3. Dividing in a congruence

1 mark here, the whole question as soon as the result is not an integer

What not to write

“3x≡6(mod7)3x\equiv 6\pmod 7, so x≡63=2x\equiv\frac{6}{3}=2.”

What to write

“MULTIPLY by the inverse of 33 modulo 77, which is 55 since 3×5=15≡13\times 5=15\equiv 1: x≡5×6=30≡2(mod7)x\equiv 5\times 6=30\equiv 2\pmod 7.”

Why: Division does not exist modulo nn. The shortcut works by luck when the quotient comes out even, and it collapses as soon as it does not: 3x≡5(mod7)3x\equiv 5\pmod 7 has nothing to do with 53\frac{5}{3}.

4. Reducing the exponent modulo pp instead of p−1p-1

the whole question

What not to write

“3100 mod 73^{100}\bmod 7: I reduce 100100 modulo 77, which gives 22, so 32=9≡23^{2}=9\equiv 2.”

What to write

“The base is reduced modulo 77, the EXPONENT modulo 7−1=67-1=6. Since 100=6×16+4100=6\times 16+4, 3100≡34=81≡4(mod7)3^{100}\equiv 3^{4}=81\equiv 4\pmod 7.”

1234567891011121312345678period 6and it repeats
The powers 3k mod 73^{k}\bmod 7 repeat identically every SIX steps, and 6=7−16=7-1: that is Fermat's little theorem. So the exponent is reduced modulo p−1p-1, the base modulo pp.

Why: Fermat's little theorem says ap−1≡1a^{p-1}\equiv 1, so it is the period p−1p-1 that governs the exponent. Two moduli live side by side in the same calculation, and mixing them up gives a plausible, wrong result.

5. Believing that every nonzero element is invertible

the whole question, and a calculation that cannot succeed

What not to write

“Modulo 66, I look for the inverse of 22 to solve 2x≡42x\equiv 4.”

What to write

“gcd⁡(2,6)=2≠1\gcd(2, 6)=2\neq 1: 22 is NOT invertible modulo 66. It is even a zero divisor, since 2×3≡0(mod6)2\times 3\equiv 0\pmod 6.”

111234522240243330303444204255543212 × 3 ≡ 0: 2 has no inverse
Modulo 66, the red cells are ZERO products of nonzero factors: 22, 33 and 44 are zero divisors, and have no inverse.

Why: Being invertible modulo nn is exactly being relatively prime to nn. With a composite modulus, some of the elements have no inverse, and the congruence is then solved through the Bézout condition, not through an inverse.

6. Applying Fermat without checking its two hypotheses

the whole question, and a wrong conclusion in cryptography

What not to write

“an−1≡1(modn)a^{n-1}\equiv 1\pmod n for every aa and every nn.”

What to write

“The theorem requires nn PRIME and nn not dividing aa. Modulo 66, which is not prime, 25=32≡22^{5}=32\equiv 2, not 11.”

Why: With a composite modulus, it is Euler's theorem that applies, with φ(n)\varphi(n) in place of p−1p-1. Using Fermat outside its scope gives a result that looks like an answer.

7. Computing the whole power before reducing

your whole test time, and a calculator that overflows

What not to write

“750 mod 137^{50}\bmod 13: I compute 7507^{50}, then I divide by 1313.”

What to write

“I reduce at EVERY step: 72=49≡107^{2}=49\equiv 10, 74≡102=100≡97^{4}\equiv 10^{2}=100\equiv 9, and so on by successive squaring.”

Why: 7507^{50} has more than forty digits: no exam calculator gives it exactly, and rounding makes the remainder wrong. The compatibility of congruence with multiplication exists precisely to avoid this.

8. Announcing a single solution to a linear congruence

half the marks for the question

What not to write

“14x≡30(mod100)14x\equiv 30\pmod{100}: I find x≡45x\equiv 45, that's the solution.”

What to write

“d=gcd⁡(14,100)=2d=\gcd(14, 100)=2 divides 3030, so there are exactly TWO solutions modulo 100100: x≡45x\equiv 45 and x≡95x\equiv 95.”

0501004595+ 50 = 100 / 2
14x≡30(mod100)14x\equiv 30\pmod{100} has d=2d=2 solutions: 4545 and 9595, 1002=50\frac{100}{2}=50 apart.

Why: The number of solutions modulo nn is d=gcd⁡(a,n)d=\gcd(a,n) as soon as dd divides bb. Solving the reduced congruence gives one solution modulo nd\frac{n}{d}, which must then be unfolded into dd solutions modulo nn.

Which method to choose

What you may do in a congruence

Before each manipulation, look at which operation it is. Three are free, one is forbidden, two are conditional.

  • If addition, subtraction, multiplication → free: reduce each term BEFORE computing

    Example: 17×23≡2×3=6(mod5)17\times 23\equiv 2\times 3=6\pmod 5

  • If raising to a power, any modulus → free on the BASE, reducing at each squaring; the exponent, however, cannot be reduced without a hypothesis

    Example: 74≡102≡9(mod13)7^{4}\equiv 10^{2}\equiv 9\pmod{13}

  • If reducing the EXPONENT, prime modulus pp not dividing aa → allowed modulo p−1p-1, by Fermat's little theorem

    Example: 3100≡34≡4(mod7)3^{100}\equiv 3^{4}\equiv 4\pmod 7

    For a composite modulus, replace p−1p-1 by φ(n)\varphi(n) and check that gcd⁡(a,n)=1\gcd(a,n)=1.

  • If cancelling a common factor cc → allowed only if gcd⁡(c,n)=1\gcd(c,n)=1; otherwise ALSO divide the modulus by gcd⁡(c,n)\gcd(c,n)

    Example: 4×2≡4×5(mod6)4\times 2\equiv 4\times 5\pmod 6 gives 2≡5(mod3)2\equiv 5\pmod 3

  • If division → FORBIDDEN. Multiply by the inverse, which exists only if gcd⁡(a,n)=1\gcd(a,n)=1

    Example: 3x≡6(mod7)3x\equiv 6\pmod 7 is solved by ×5\times 5, not by ÷3\div 3

When the inverse does not exist, the congruence is not necessarily unsolvable: it has gcd⁡(a,n)\gcd(a,n) solutions as soon as this GCD divides the right-hand side. That is the most frequent case on tests.

Which tool for what the question asks

The questions of the chapter come down to six forms, and each one has a single tool. Identify it before writing the first line.

  • If “what is the remainder of”, “what day of the week” → Euclidean division, then reducing the calculation modulo nn

    Example: 21000 mod 72^{1000}\bmod 7 by periodicity

  • If “compute gcd⁡(a,b)\gcd(a,b)” → Euclidean algorithm, as a table of successive remainders

    Example: gcd⁡(48,18)=6\gcd(48, 18)=6 in three divisions

  • If “find uu and vv such that au+nv=dau+nv=d” → EXTENDED Euclidean algorithm, working back up the equalities

    Example: 50−7×7=150-7\times 7=1 gives u=−7u=-7

  • If “the inverse of aa modulo nn” → check gcd⁡(a,n)=1\gcd(a,n)=1, then Bézout: the inverse is uu reduced into [0,n−1][0, n-1]

    Example: the inverse of 77 modulo 5050 is −7≡43-7\equiv 43

  • If “solve ax≡b(modn)ax\equiv b\pmod n” → compute d=gcd⁡(a,n)d=\gcd(a,n); if d∤bd\nmid b, no solution; otherwise divide everything by dd, invert, then unfold into dd solutions

    Example: 14x≡30(mod100)14x\equiv 30\pmod{100} gives x≡45x\equiv 45 and x≡95x\equiv 95

  • If “a huge exponent” → Fermat to reduce the exponent, then fast exponentiation to finish

    Example: 3100 mod 73^{100}\bmod 7 comes down to 343^{4}

All these questions start with the same move: computing a GCD. It decides whether the inverse exists, how many solutions the congruence has, and whether Fermat applies.

How the answer is expected to be written

A marker ticks steps. Here they are in order, with the concluding sentence expected word for word.

Computing a modular inverse by the extended Euclidean algorithm

When to use it: The question asks for the inverse of aa modulo nn, or a solution that depends on it.

  1. 1 Check existence BEFORE computing: “gcd⁡(7,50)=1\gcd(7, 50)=1, so 77 is invertible modulo 5050.”
  2. 2 Run the Euclidean algorithm, writing out each division: “50=7×7+150=7\times 7+1, then 7=1×7+07=1\times 7+0.”
  3. 3 Work back up to isolate the GCD: “1=50−7×71=50-7\times 7.”
  4. 4 Read Bézout's identity modulo nn: “so −7×7≡1(mod50)-7\times 7\equiv 1\pmod{50}.”
  5. 5 Reduce the coefficient into [0,n−1][0, n-1] and CHECK: “−7≡43-7\equiv 43, and 7×43=301=6×50+17\times 43=301=6\times 50+1.”

Concluding sentence

The inverse of 77 modulo 5050 is 4343, since 7×43=301≡1(mod50)7\times 43=301\equiv 1\pmod{50}.

The trap: Giving a negative Bézout coefficient as the inverse. It is correct, but an inverse is written in [0,n−1][0, n-1], and the final check by multiplication is the only way to make sure you have not swapped uu and vv.

Marking: Usually 1 mark for checking the GCD, 1 mark for running the algorithm, 1 mark for the back substitution, 1 mark for the reduced and checked inverse.

Solving a linear congruence ax≡b(modn)ax\equiv b\pmod n

When to use it: Any linear congruence modulo nn, including one disguised as a check-digit or calendar problem.

  1. 1 Compute d=gcd⁡(a,n)d=\gcd(a,n) and DECIDE: “d=2d=2 divides 3030, so there are solutions, exactly 22 modulo 100100.”
  2. 2 Divide all three quantities by dd, modulus included: “7x≡15(mod50)7x\equiv 15\pmod{50}.”
  3. 3 Compute the inverse of ad\frac{a}{d} modulo nd\frac{n}{d} by the extended Euclidean algorithm.
  4. 4 Multiply and reduce: “x≡43×15≡45(mod50)x\equiv 43\times 15\equiv 45\pmod{50}.”
  5. 5 UNFOLD the dd solutions modulo nn by adding nd\frac{n}{d}, then check each one in the original congruence.

Concluding sentence

The congruence 14x≡30(mod100)14x\equiv 30\pmod{100} has exactly two solutions, x≡45(mod100)x\equiv 45\pmod{100} and x≡95(mod100)x\equiv 95\pmod{100}.

The trap: Forgetting to divide the MODULUS along with both sides. You then get 7x≡15(mod100)7x\equiv 15\pmod{100}, whose solution does satisfy the original congruence but leaves out the other one: you lose solutions.

Marking: 1 mark for the existence condition, 1 mark for dividing by dd modulus included, 1 mark for the inverse, 1 mark for unfolding the dd solutions.

Check before you hand in

Five minutes of checking recover more marks than one more problem started in a hurry.

The typical problem, taken apart

Solving 14x≡30(mod100)14x\equiv 30\pmod{100}, down to both solutions

Solve in Z\mathbb{Z} the linear congruence 14x≡30(mod100)14x\equiv 30\pmod{100}.

State the exact number of solutions modulo 100100 and check each one.

Step 1

gcd⁡(14,100)\gcd(14, 100): 100=7×14+2100=7\times 14+2, then 14=7×2+014=7\times 2+0, so d=2d=2. Since 22 divides 3030, the congruence has solutions, and it has exactly d=2d=2 of them modulo 100100.

Why

This first line decides everything: it says whether there are solutions and how many. A paper that skips it can find the right value and still lose half the marks, because it never counted the solutions.

Step 2

Divide BOTH sides and the MODULUS by d=2d=2: 7x≡15(mod50)7x\equiv 15\pmod{50}.

Why

The modulus is divided too, otherwise the resulting congruence is not equivalent. That omission is what makes solutions go missing.

Step 3

gcd⁡(7,50)=1\gcd(7, 50)=1, so 77 is invertible modulo 5050. Euclid: 50=7×7+150=7\times 7+1, so 1=50−7×71=50-7\times 7 and −7×7≡1(mod50)-7\times 7\equiv 1\pmod{50}.

Why

Check invertibility before looking for the inverse. After dividing by dd it is guaranteed, but writing it down earns the rigour mark and proves that the condition was understood.

Step 4

The inverse of 77 modulo 5050 is therefore −7≡43-7\equiv 43. Check: 7×43=301=6×50+17\times 43=301=6\times 50+1.

Why

Reduce into [0,49][0, 49] and check by multiplication. These two moves take ten seconds and eliminate every sign error in the back substitution.

Step 5

Multiply the congruence by 4343: x≡43×15=645(mod50)x\equiv 43\times 15=645\pmod{50}, and 645=12×50+45645=12\times 50+45, so x≡45(mod50)x\equiv 45\pmod{50}.

Why

You multiply, you never divide. The result here is a congruence modulo 5050, not modulo 100100: the question is not finished yet, and this is where half the papers stop.

Step 6

Unfold modulo 100100: x≡45x\equiv 45 and x≡45+50=95x\equiv 45+50=95. That makes d=2d=2 solutions, as announced in the first line.

Why

Unfolding means adding nd\frac{n}{d} as many times as needed. The count announced at the start serves as a check here: two solutions expected, two solutions obtained.

Step 7

Check: 14×45=630=6×100+3014\times 45=630=6\times 100+30 and 14×95=1330=13×100+3014\times 95=1330=13\times 100+30. Both values work.

Why

Substitute each solution into the ORIGINAL congruence, not into the reduced one. It catches arithmetic slips; a missing solution, however, only the count of the first line can reveal.

The conclusion, written out

The congruence 14x≡30(mod100)14x\equiv 30\pmod{100} has exactly two solutions modulo 100100: x≡45x\equiv 45 and x≡95x\equiv 95.

The classic mistake on this problem: Dividing only both sides and keeping the modulus: 7x≡15(mod100)7x\equiv 15\pmod{100}. The inverse of 77 modulo 100100 is also 4343, and you get x≡45(mod100)x\equiv 45\pmod{100}, so a single solution instead of two. The check in the original congruence reveals nothing, since 4545 works: only the condition on the GCD flags the missing solution.

Learn by heart

  • • The remainder of a Euclidean division is ALWAYS in [0,n−1][0, n-1], even for a negative dividend.
  • • Reduce BEFORE computing: congruence goes through addition and multiplication, never through division.
  • • ac≡bc(modn)ac\equiv bc\pmod n gives a≡ba\equiv b only if gcd⁡(c,n)=1\gcd(c,n)=1; otherwise you divide the modulus too.
  • • aa is invertible modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n)=1, and the inverse is Bézout's uu.
  • • All the nonzero elements are invertible if and only if nn is PRIME; otherwise there are zero divisors.
  • • Fermat: pp prime and p∤ap\nmid a give ap−1≡1(modp)a^{p-1}\equiv 1\pmod p. Base modulo pp, EXPONENT modulo p−1p-1.
  • • ax≡b(modn)ax\equiv b\pmod n has solutions if and only if gcd⁡(a,n)\gcd(a,n) divides bb, and then exactly gcd⁡(a,n)\gcd(a,n) of them.
  • • Everything starts with a GCD: it decides the inverse, the number of solutions and whether Fermat can be used.

Frequently asked questions

Can the remainder of a division be negative?

No, never in mathematics: Euclidean division requires a remainder between zero and the modulus minus one. Minus seventeen modulo five is therefore three, not minus two. The percent operator of the C, C plus plus and Java languages does return a negative remainder in that case, unlike Python: it is a programming convention, not a definition.

Can you cancel a common factor in a congruence?

Only if that factor is relatively prime to the modulus. Otherwise, you also divide the modulus by the greatest common divisor of the factor and the modulus. For example, four times two and four times five are congruent modulo six, whereas two and five are not: they become congruent only modulo three.

How do you compute a modular inverse?

First check that the number is relatively prime to the modulus, otherwise the inverse does not exist. Then run the Euclidean algorithm and work back up the equalities to write one equals a times u plus n times v. The coefficient u is the inverse, which you reduce between zero and the modulus minus one, then check by multiplication.

How do you compute a very large power modulo n?

You never compute the whole power. If the modulus is prime and does not divide the base, Fermat's little theorem lets you reduce the exponent modulo the modulus minus one. You finish with fast exponentiation, which squares and reduces at every step, with about twice as many multiplications as the base-two logarithm of the exponent.

How many solutions does a linear congruence have?

It has exactly as many as the greatest common divisor of the coefficient and the modulus, provided that this divisor also divides the right-hand side; otherwise it has none. You then divide both sides and the modulus by this divisor, solve, and unfold the solution obtained by adding the reduced modulus.

Practise it

Corrected exercises: Modular arithmetic, 201-N11

A method is proved on a paper, not on a sheet. The set for the same chapter takes each of these traps into a problem, with the solution written out step by step.

  • 12 corrected exercises
  • 120 points
  • 150 minutes
Do the exercises
Something missing from this sheet? A method, a trap, an exercise to practise it, 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.

What is it about?
I read every request myself, nobody else sees it. No need to write your name or your school.
When your request is dealt with, you hear about it by email and in your space.

By sending, you agree that I read your request to answer it. It is kept with your account, erased with it, and never published: privacy policy.

Previous sheet Number systems in computing Next sheet Set theory and relations

© Ahmed Squalli Houssaini. Revision sheet published at www.letuteurscientifique.ca/en/fiches/cegep-n11-modular-arithmetic. Free for personal and classroom use; republishing it elsewhere requires written permission (legal notice).

See also

Looking for a 201-N11 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.

Site by Studio Squalli