Search the atlas

The Euclidean algorithm and linear congruences

Use the extended Euclidean algorithm and Bézout coefficients to test and solve linear congruences.

The extended Euclidean algorithm, Bézout’s identity and congruences

The extended Euclidean algorithm finds the greatest common divisor of two integers and expresses it as an integer linear combination of them. These coefficients can be used to find modular inverses and solve linear congruences without testing candidates one at a time.

Start with Euclidean division and the greatest common divisor and arithmetic with signed integers. The extra step is to track where each remainder comes from until the gcd is expressed in terms of the inputs.

1. Conventions and Bézout’s identity

For integers a,ba,b, at least one of which is nonzero, take the gcd to be positive.

d=gcd⁡(a,b)=gcd⁡(∣a∣,∣b∣)>0.d=\gcd(a,b)=\gcd(|a|,|b|)>0.

Bézout’s identity states that there are integers u,vu,v such that

au+bv=d.au+bv=d.

The coefficients need not be unique. Given one pair, further pairs are obtained as follows.

ut=u+bdt,vt=v−adt,t∈Z.\begin{aligned} u_t&=u+\dfrac{b}{d}t,\\ v_t&=v-\dfrac{a}{d}t, \qquad t\in\mathbb Z. \end{aligned}

The extra terms cancel. To prove that a positive integer is the gcd, check that it divides both inputs and is an integer linear combination of them. Every common divisor divides this combination, so no positive common divisor can exceed it.

If only one input is zero, the gcd is the absolute value of the nonzero input. Choose the coefficient of that input to equal its sign. The pair (0,0)(0,0) is excluded by the convention that the gcd is positive.

2. Working back through the divisions

Apply the Euclidean algorithm to the absolute values of the inputs. Each division gives

ri−1=qiri+ri+1,0≤ri+1<ri.\begin{aligned} r_{i-1}&=q_i r_i+r_{i+1},\\ 0&\le r_{i+1}<r_i. \end{aligned}

The positive remainders decrease, so the algorithm terminates. The last nonzero remainder is the gcd. Starting there, repeatedly substitute

ri+1=ri−1−qiri.r_{i+1}=r_{i-1}-q_i r_i.

until only the original inputs remain. If an input was negative, reverse the sign of its coefficient when replacing its absolute value by the original signed integer. The zero remainder marks termination. Its coefficients express zero rather than the gcd.

3. Tracking coefficients in a table

For a,b>0a,b>0, update the coefficients with the remainders, avoiding a separate backward substitution. Every row satisfies

ri=sia+tib.r_i=s_i a+t_i b.

Start with these two rows.

Remainder

Coefficient of the first input

Coefficient of the second input

aa

11

00

bb

00

11

Take the quotient of the two current remainders. Apply the same row operation to all three columns.

ri+1=ri−1−qiri,si+1=si−1−qisi,ti+1=ti−1−qiti.\begin{aligned} r_{i+1}&=r_{i-1}-q_i r_i,\\ s_{i+1}&=s_{i-1}-q_i s_i,\\ t_{i+1}&=t_{i-1}-q_i t_i. \end{aligned}

This preserves the linear identity in every row. When the new remainder is zero, return the preceding row. Even when a<ba<b, a zero first quotient handles the input order.

4. Congruences and modular inverses

Fix an integer modulus m≥2m\ge2. The notation

x≡y(modm)x\equiv y\pmod m

means that mm divides x−yx-y. A residue class consists of all integers with the same remainder. Its least nonnegative representative lies between zero and m−1m-1.

An inverse of aa modulo mm is an integer uu satisfying

au≡1(modm).au\equiv1\pmod m.

An inverse exists exactly when gcd⁡(a,m)=1\gcd(a,m)=1. In that case the extended algorithm gives

au+mv=1.au+mv=1.

Reducing modulo mm gives the inverse as the coefficient uu. Conversely, an inverse yields an integer combination equal to one, so the two inputs cannot share any divisor greater than one. The inverse represents a unique class modulo mm, with infinitely many integer representatives.

5. Solving a linear congruence

Given a,c∈Za,c\in\mathbb Z and m≥2m\ge2, find all integers xx such that

ax≡c(modm).ax\equiv c\pmod m.

Let d=gcd⁡(a,m)d=\gcd(a,m). Solutions exist exactly when d∣cd\mid c. To see necessity, write the equivalent integer equation.

ax−my=c.ax-my=c.

Its left side is divisible by dd. For sufficiency, take a Bézout identity au+mv=dau+mv=d and multiply by c/dc/d. This gives x0=u(c/d)x_0=u(c/d) as one solution.

When d∣cd\mid c, divide the coefficient, the right-hand side and the modulus together.

a′=ad,c′=cd,m′=md.a'=\dfrac{a}{d},\qquad c'=\dfrac{c}{d},\qquad m'=\dfrac{m}{d}.

If m′>1m'>1, then a′a' and m′m' are coprime. Find the inverse u′u' of a′a' modulo m′m'. There is a single solution class for the reduced modulus.

x≡u′c′(modm′).x\equiv u'c'\pmod {m'}.

Taking the representative 0≤x0<m′0\le x_0<m' gives all integer solutions as

x=x0+km′,k∈Z.x=x_0+km',\qquad k\in\mathbb Z.

For the original modulus mm, these same integers lie in exactly dd classes, with distinct representatives

x0, x0+m′, …, x0+(d−1)m′.x_0,\ x_0+m',\ \ldots,\ x_0+(d-1)m'.

Subtract two solutions of the reduced congruence. Coprimality forces their difference to be a multiple of m′m'. Between zero and m−1m-1 this gives exactly the dd representatives listed above.

Zero coefficient or reduced modulus equal to one

If aa is a multiple of mm, then d=md=m and m′=1m'=1. No inverse modulo one is needed. If cc is also a multiple of mm, every integer is a solution. Otherwise there are no solutions. This includes a=0a=0 and distinguishes an identity from an impossible congruence.

6. Interpreting and checking the solutions

  • When dividing a congruence by a common factor, divide the modulus too. Keeping the old modulus can discard solutions.
  • A negative Bézout coefficient is valid. Reducing it modulo mm changes the representative while preserving the class.
  • The integer solution set can be described as a family, as one class in the reduced modulus or as several classes in the original modulus.
  • Test divisibility by the gcd before attempting inversion.

With a circular index, the congruence determines which positions a fixed step can reach. In elementary cryptography examples, a modular inverse undoes a multiplication. These examples illustrate the arithmetic. Security of a cryptographic system requires additional properties.

The worked problems on the extended Euclidean algorithm and congruences cover backward substitution, coefficient tables, inverses of negative integers, multiple solution classes, nonexistence and zero coefficients. Related cryptography exercises illustrate applications.

Covering a circular index

A circular array has m=1024m=1024 positions numbered 0 through 1023. Start at zero and advance by the same integer step aa each time, reducing the result modulo mm. After kk steps the index is

jk≡ak(mod1024).j_k\equiv ak\pmod {1024}.

The first return to zero takes m/gcd⁡(a,m)m/\gcd(a,m) steps. With a=64a=64, only 16 positions are visited, all multiples of 64. The cycle skips the other 1008 positions. Index 128 is reached when k≡2(mod16)k\equiv2\pmod {16}, and index 137 is unreachable.

With a=65a=65 the gcd is one, so all 1024 positions are visited before the first return. The Bézout identity

65(−63)+1024⋅4=165(-63)+1024\cdot4=1

gives the inverse −63≡961(mod1024)-63\equiv961\pmod {1024}. To find the step at which index 137 is visited, calculate

k≡961⋅137(mod1024)≡585(mod1024).\begin{aligned} k&\equiv961\cdot137\pmod {1024}\\ &\equiv585\pmod {1024}. \end{aligned}

Direct substitution gives 65⋅585=137+37⋅102465\cdot585=137+37\cdot1024. Index 137 first occurs after 585 steps, with k=0k=0 denoting the initial position.

The connection to groups clarifies two properties. Adding a fixed step is reversible even for step 64, but reversibility alone does not put every index in one cycle. Full coverage requires the step and the length to be coprime.

This test applies to cyclic scans, index permutations and deterministic access sequences. It cannot establish randomness, cryptographic security or cache performance. In software, guard against overflow before reduction and normalize negative remainders consistently. The identities assume exact integers.

Worked linear congruences

How do you solve an equation when only the remainder upon division matters? The six examples below start with the Euclidean algorithm. They use the gcd and its coefficients to solve congruences, including cases with several solution classes or none. The theory is given in the extended Euclidean algorithm, Bézout’s identity and congruences. For practice with the prerequisites, see the divisibility, gcd and lcm problems.

Prerequisites and checks

Use exact integers, a positive gcd and moduli of at least two. The relation ax≡c(modm)ax\equiv c\pmod m means that ax−cax-c is a multiple of mm. Two integers belong to the same residue class modulo m when they differ by a multiple of m. For example, 2, 11 and −7 are interchangeable modulo 9. To list the classes without repetitions, choose a representative between zero and m−1m-1.

Bézout’s identity expresses the gcd as an integer linear combination of the inputs.

au+mv=d,d=gcd⁡(a,m).au+mv=d,\qquad d=\gcd(a,m).

The extended algorithm gives u and v along with the gcd. If d = 1, reducing the identity modulo m removes the term mv and gives au ≡ 1. Thus u is the modular inverse of a. Multiplication by u turns a into the class of 1. The real reciprocal 1/a is a different operation.

First find d=gcd⁡(a,m)d=\gcd(a,m). If dd fails to divide the right-hand side, the congruence has no solution. Otherwise reduce the coefficient, the right-hand side and the modulus, then use Bézout’s identity to find the inverse in the reduced problem.

The examples derive Bézout coefficients by two methods. Backward substitution retraces the divisions. A coefficient table preserves a linear identity at every step and provides enough information to check the calculation directly.

Exercise 1. From remainders to Bézout coefficients

For a=391a=391 and b=299b=299, find the gcd and integers u,vu,v satisfying 391u+299v=gcd⁡(391,299)391u+299v=\gcd(391,299) by backward substitution.

Aim. Obtain the gcd and an identity that can be checked with a single substitution.

Method. Carry out the divisions, isolate the last nonzero remainder and substitute backwards to the two inputs.

The divisions give

391=1⋅299+92,299=3⋅92+23,92=4⋅23+0.\begin{aligned} 391&=1\cdot299+92,\\ 299&=3\cdot92+23,\\ 92&=4\cdot23+0. \end{aligned}

The gcd is 2323. In the second division, substitute the value of 9292 from the first division.

23=299−3⋅92=299−3(391−299)=−3⋅391+4⋅299.\begin{aligned} 23&=299-3\cdot92\\ &=299-3(391-299)\\ &=-3\cdot391+4\cdot299. \end{aligned}

One pair of coefficients is therefore u=−3u=-3, v=4v=4.

Check. The number 2323 divides both inputs since 391=17⋅23391=17\cdot23 and 299=13⋅23299=13\cdot23. It also satisfies

−1173+1196=23.-1173+1196=23.

Every common divisor divides this combination. No positive common divisor can exceed 2323, and no factorization of the inputs was needed.

Exercise 2. Keeping the coefficients in a table

Apply the tabular algorithm to a=240a=240 and b=46b=46. For each remainder record the coefficients s,ts,t in the identity r=240s+46tr=240s+46t.

Aim. Find Bézout coefficients without backward substitution and check each update along the way.

Method. Express every remainder as a combination of 240 and 46. Subtract the most recent row times the quotient from the preceding row to obtain the next row. Apply the same operation to both coefficients so that their combination still equals the new remainder.

The divisions give the quotients.

240=5⋅46+10,46=4⋅10+6,10=1⋅6+4,6=1⋅4+2,4=2⋅2+0.\begin{aligned} 240&=5\cdot46+10,\\ 46&=4\cdot10+6,\\ 10&=1\cdot6+4,\\ 6&=1\cdot4+2,\\ 4&=2\cdot2+0. \end{aligned}

The first rows express 240 = 1·240 + 0·46 and 46 = 0·240 + 1·46. The first quotient is 5, so the new remainder is 240 − 5·46 = 10 with coefficients

snew=1−5⋅0=1,tnew=0−5⋅1=−5.\begin{aligned} s_{\mathrm{new}}&=1-5\cdot0=1,\\ t_{\mathrm{new}}&=0-5\cdot1=-5. \end{aligned}

Thus 10 = 1·240 − 5·46. Repeat the operation until the remainder is zero. In the table, each quotient produces its row from the two preceding rows. The first two rows supply the starting values.

Remainder

Quotient used

Coefficient of 240

Coefficient of 46

240

—

1

0

46

—

0

1

10

5

1

−5

6

4

−4

21

4

1

5

−26

2

1

−9

47

0

2

23

−120

For example, the row for remainder 66 is the row for 4646 minus four times the row for 1010. The first coefficient is −4-4 and the second is 1−4(−5)=211-4(-5)=21.

When zero appears, read the preceding row.

2=−9⋅240+47⋅46.2=-9\cdot240+47\cdot46.

Check. The identity gives −2160+2162=2-2160+2162=2. The number 22 divides both inputs. The final row instead expresses zero.

23⋅240−120⋅46=0.23\cdot240-120\cdot46=0.

One row certifies the gcd and the other marks termination.

Exercise 3. Inverting a negative integer

Use the extended Euclidean algorithm to find the inverse of −37-37 modulo 101101. Give its representative between zero and 100100.

Aim. Handle signs and normalization systematically, without testing possible inverses.

Method. Apply Euclid’s algorithm to the positive inputs 101101 and 3737. Work back to an identity equal to one, then rewrite it using −37-37.

The divisions give

101=2⋅37+27,37=1⋅27+10,27=2⋅10+7,10=1⋅7+3,7=2⋅3+1,3=3⋅1+0.\begin{aligned} 101&=2\cdot37+27,\\ 37&=1\cdot27+10,\\ 27&=2\cdot10+7,\\ 10&=1\cdot7+3,\\ 7&=2\cdot3+1,\\ 3&=3\cdot1+0. \end{aligned}

The gcd is one, so the inverse exists. Substitute backwards.

1=7−2⋅3=7−2(10−7)=3⋅7−2⋅10=3(27−2⋅10)−2⋅10=3⋅27−8⋅10=3⋅27−8(37−27)=11⋅27−8⋅37=11(101−2⋅37)−8⋅37=11⋅101−30⋅37.\begin{aligned} 1&=7-2\cdot3\\ &=7-2(10-7)\\ &=3\cdot7-2\cdot10\\ &=3(27-2\cdot10)-2\cdot10\\ &=3\cdot27-8\cdot10\\ &=3\cdot27-8(37-27)\\ &=11\cdot27-8\cdot37\\ &=11(101-2\cdot37)-8\cdot37\\ &=11\cdot101-30\cdot37. \end{aligned}

Restoring the negative input gives

1=30(−37)+11⋅101.1=30(-37)+11\cdot101.

The required coefficient is already normalized because 0≤30<1010\le30<101.

(−37)−1≡30(mod101).(-37)^{-1}\equiv30\pmod{101}.

Check. The product leaves remainder one.

(−37)⋅30=−11⋅101+1.(-37)\cdot30=-11\cdot101+1.

The coefficient −30-30 would invert 3737. For −37-37 the sign of both the input and its coefficient must change.

Exercise 4. One reduced class and two original classes

Find all solutions of

14x≡30(mod100).14x\equiv30\pmod{100}.

Give the entire set of integer solutions and the representatives between zero and 9999.

Aim. Keep every solution when the coefficient and modulus share a factor.

Assumptions. The unknown is an integer. The original modulus is 100100. One class in the reduced modulus may contain several classes modulo 100100.

Method. Test divisibility by the gcd, reduce all three inputs, compute the inverse and lift the resulting class to the original modulus.

The Euclidean algorithm gives

100=7⋅14+2,14=7⋅2+0.\begin{aligned} 100&=7\cdot14+2,\\ 14&=7\cdot2+0. \end{aligned}

The gcd 22 divides 3030, so solutions exist. To see why the modulus must also be divided, write the congruence as an equation with an integer parameter k.

14x−30=100k,7x−15=50k.\begin{aligned} 14x-30&=100k,\\ 7x-15&=50k. \end{aligned}

Dividing the coefficient, the right-hand side and the modulus gives

7x≡15(mod50).7x\equiv15\pmod{50}.

To invert 77, isolate the remainder in the division.

50=7⋅7+1,1=50−7⋅7.\begin{aligned} 50&=7\cdot7+1,\\ 1&=50-7\cdot7. \end{aligned}

The coefficient of 77 is −7-7, equivalent to 4343 modulo 5050. Thus

x≡43⋅15(mod50),x≡45(mod50).\begin{aligned} x&\equiv43\cdot15\pmod{50},\\ x&\equiv45\pmod{50}. \end{aligned}

All integer solutions form the family

x=45+50k,k∈Z.x=45+50k,\qquad k\in\mathbb Z.

Return to the original modulus by finding family members between zero and 99. The values k = 0 and k = 1 give 45 and 95, while k = −1 and k = 2 give −5 and 145, outside the interval. The two classes in the original modulus are represented by 45 and 95. They alternate in the family because two increments of 50 give the original modulus, 100.

Check. The product 43⋅15=64543\cdot15=645 leaves remainder 4545 modulo 5050. Verify the two classes directly in the original congruence.

Representative

Difference from the right-hand side

Quotient on division by the modulus

45

14⋅45−30=60014\cdot45-30=600

6

95

14⋅95−30=130014\cdot95-30=1300

13

There are no further solutions. In the reduced congruence 77 is invertible, so every solution differs from 4545 by a multiple of 5050. Between zero and 9999 this gives exactly the two values above.

Pitfall. Writing 7x≡15(mod100)7x\equiv15\pmod{100} after dividing by two would keep the wrong modulus and lose one of the original classes.

Imagine starting at index zero and advancing 14 positions at a time in a circular array of 100 cells. After x steps, the index is the remainder of 14x modulo 100. Cell 30 is first reached after 45 steps and then every 50 steps. Between zero and 99 the two step counts are exactly 45 and 95. Only the 50 even indices can be reached. The index returns to zero before visiting the other cells. Distinguishing the gcd from the least common multiple in periodic problems helps explain this repetition. Checking the indices alone cannot establish that a program handles data or overwrites correctly.

Exercise 5. Proving that no solution exists

Decide whether there are integer solutions to

18x≡7(mod30).18x\equiv7\pmod{30}.

Aim. Determine existence before trying to invert the coefficient.

The divisions

30=1⋅18+12,18=1⋅12+6,12=2⋅6+0\begin{aligned} 30&=1\cdot18+12,\\ 18&=1\cdot12+6,\\ 12&=2\cdot6+0 \end{aligned}

give gcd⁡(18,30)=6\gcd(18,30)=6. Since 66 does not divide 77, the congruence has no solution.

Check. The equivalent integer equation is

18x−30y=7.18x-30y=7.

For all integers x,yx,y the left side is a multiple of 66, while the right side is not. This contradiction rules out every integer without having to test individual candidates.

Exercise 6. When the coefficient of x is zero

Compare the following congruences.

0x≡0(mod9),0x≡4(mod9).0x\equiv0\pmod9, \qquad 0x\equiv4\pmod9.

Aim. Distinguish an identity from an impossible congruence without trying to invert zero.

In the first case the difference of the two sides is always zero, a multiple of 99. Every integer is a solution. Modulo 99 this gives all nine classes with representatives

0, 1, 2, 3, 4, 5, 6, 7, 8.0,\ 1,\ 2,\ 3,\ 4,\ 5,\ 6,\ 7,\ 8.

In the second case the difference is always −4-4, which is not a multiple of 99. There is no integer solution.

Check. The gcd is gcd⁡(0,9)=9\gcd(0,9)=9. It divides the right-hand side zero but does not divide four. The reduced modulus would be one in the first case, so describe the solution set directly as all integers. The same reasoning applies when the coefficient is a nonzero multiple of nine.

Further reading

These abstract algebra exercises relate invertibility to the structure of rings and fields. In the cryptography exercises modular inverses appear in affine decryption and elementary RSA examples. For the general conditions and sources, return to the extended Euclidean algorithm and Bézout’s identity.

Sources

Back to contents