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 , at least one of which is nonzero, take the gcd to be positive.
Bézout’s identity states that there are integers such that
The coefficients need not be unique. Given one pair, further pairs are obtained as follows.
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 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
The positive remainders decrease, so the algorithm terminates. The last nonzero remainder is the gcd. Starting there, repeatedly substitute
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 , update the coefficients with the remainders, avoiding a separate backward substitution. Every row satisfies
Start with these two rows.
|
Remainder |
Coefficient of the first input |
Coefficient of the second input |
|---|---|---|
|
|
|
|
|
|
|
|
Take the quotient of the two current remainders. Apply the same row operation to all three columns.
This preserves the linear identity in every row. When the new remainder is zero, return the preceding row. Even when , a zero first quotient handles the input order.
4. Congruences and modular inverses
Fix an integer modulus . The notation
means that divides . A residue class consists of all integers with the same remainder. Its least nonnegative representative lies between zero and .
An inverse of modulo is an integer satisfying
An inverse exists exactly when . In that case the extended algorithm gives
Reducing modulo gives the inverse as the coefficient . 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 , with infinitely many integer representatives.
5. Solving a linear congruence
Given and , find all integers such that
Let . Solutions exist exactly when . To see necessity, write the equivalent integer equation.
Its left side is divisible by . For sufficiency, take a Bézout identity and multiply by . This gives as one solution.
When , divide the coefficient, the right-hand side and the modulus together.
If , then and are coprime. Find the inverse of modulo . There is a single solution class for the reduced modulus.
Taking the representative gives all integer solutions as
For the original modulus , these same integers lie in exactly classes, with distinct representatives
Subtract two solutions of the reduced congruence. Coprimality forces their difference to be a multiple of . Between zero and this gives exactly the representatives listed above.
Zero coefficient or reduced modulus equal to one
If is a multiple of , then and . No inverse modulo one is needed. If is also a multiple of , every integer is a solution. Otherwise there are no solutions. This includes 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 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 positions numbered 0 through 1023. Start at zero and advance by the same integer step each time, reducing the result modulo . After steps the index is
The first return to zero takes steps. With , only 16 positions are visited, all multiples of 64. The cycle skips the other 1008 positions. Index 128 is reached when , and index 137 is unreachable.
With the gcd is one, so all 1024 positions are visited before the first return. The Bézout identity
gives the inverse . To find the step at which index 137 is visited, calculate
Direct substitution gives . Index 137 first occurs after 585 steps, with 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 means that is a multiple of . 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 .
Bézout’s identity expresses the gcd as an integer linear combination of the inputs.
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 . If 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 and , find the gcd and integers satisfying 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
The gcd is . In the second division, substitute the value of from the first division.
One pair of coefficients is therefore , .
Check. The number divides both inputs since and . It also satisfies
Every common divisor divides this combination. No positive common divisor can exceed , and no factorization of the inputs was needed.
Exercise 2. Keeping the coefficients in a table
Apply the tabular algorithm to and . For each remainder record the coefficients in the identity .
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.
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
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 is the row for minus four times the row for . The first coefficient is and the second is .
When zero appears, read the preceding row.
Check. The identity gives . The number divides both inputs. The final row instead expresses zero.
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 modulo . Give its representative between zero and .
Aim. Handle signs and normalization systematically, without testing possible inverses.
Method. Apply Euclid’s algorithm to the positive inputs and . Work back to an identity equal to one, then rewrite it using .
The divisions give
The gcd is one, so the inverse exists. Substitute backwards.
Restoring the negative input gives
The required coefficient is already normalized because .
Check. The product leaves remainder one.
The coefficient would invert . For the sign of both the input and its coefficient must change.
Exercise 4. One reduced class and two original classes
Find all solutions of
Give the entire set of integer solutions and the representatives between zero and .
Aim. Keep every solution when the coefficient and modulus share a factor.
Assumptions. The unknown is an integer. The original modulus is . One class in the reduced modulus may contain several classes modulo .
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
The gcd divides , so solutions exist. To see why the modulus must also be divided, write the congruence as an equation with an integer parameter k.
Dividing the coefficient, the right-hand side and the modulus gives
To invert , isolate the remainder in the division.
The coefficient of is , equivalent to modulo . Thus
All integer solutions form the family
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 leaves remainder modulo . Verify the two classes directly in the original congruence.
|
Representative |
Difference from the right-hand side |
Quotient on division by the modulus |
|---|---|---|
|
45 |
|
6 |
|
95 |
|
13 |
There are no further solutions. In the reduced congruence is invertible, so every solution differs from by a multiple of . Between zero and this gives exactly the two values above.
Pitfall. Writing 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
Aim. Determine existence before trying to invert the coefficient.
The divisions
give . Since does not divide , the congruence has no solution.
Check. The equivalent integer equation is
For all integers the left side is a multiple of , 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.
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 . Every integer is a solution. Modulo this gives all nine classes with representatives
In the second case the difference is always , which is not a multiple of . There is no integer solution.
Check. The gcd is . 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
- Brian Kell, Carnegie Mellon University, The extended Euclidean algorithm. Algorithms and integer linear combinations.
- Michel Goemans, MIT OpenCourseWare, Modular Arithmetic and Elementary Algebra. Bézout’s identity and modular arithmetic.