100% found this document useful (1 vote)
24 views3 pages

GCD and Extended GCD Algorithms

The document discusses two algorithms for finding the greatest common divisor (GCD) of two numbers: 1. The standard GCD algorithm which repeatedly applies the division algorithm to replace the numbers with the remainder and divisor until a remainder of 0 is reached. 2. The extended GCD algorithm which finds integers A and B such that the GCD is equal to Am + Bn. It sets up a spreadsheet to calculate the values through successive rows until the last non-zero remainder is reached.

Uploaded by

infokiran2002
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
24 views3 pages

GCD and Extended GCD Algorithms

The document discusses two algorithms for finding the greatest common divisor (GCD) of two numbers: 1. The standard GCD algorithm which repeatedly applies the division algorithm to replace the numbers with the remainder and divisor until a remainder of 0 is reached. 2. The extended GCD algorithm which finds integers A and B such that the GCD is equal to Am + Bn. It sets up a spreadsheet to calculate the values through successive rows until the last non-zero remainder is reached.

Uploaded by

infokiran2002
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Math 103A Winter,2001 Professor John J Wavrik

The GCD algorithm


Given m,n find gcd(m,n) We proved in class that the gcd can be found by repeatedly applying the division algorithm: a = bq + r. We start with a=m, b=n. The next pair is (b,r) [the quotient is not needed here]. We continue replacing a by the divisor and b by the remainder until we get a remainder 0. The last non-zero remainder is the gcd. This algorithm can be performed on a spreadsheet: A 1 2 3 4 5 6 7 8 9 10 11 12 13 14
m 123456 a 123456 654321 123456 37041 12333 42 27 15 12 3 0 #DIV/0!

B
n 654321 b 654321 123456 37041 12333 42 27 15 12 3

C 1 2 3 4 5 6 7 8 9 10 11 12 13 14
m

A
n

123456 654321

r 123456 37041 12333 42 27 15 12 3 0

=A2 =B4 =B5 =B6 =B7 =B8 =B9 =B10 =B11 =B12 =B13

=B2 =C4 =C5 =C6 =C7 =C8 =C9 =C10 =C11 =C12 =C13

=MOD(A4,B4) =MOD(A5,B5) =MOD(A6,B6) =MOD(A7,B7) =MOD(A8,B8) =MOD(A9,B9) =MOD(A10,B10) =MOD(A11,B11) =MOD(A12,B12) =MOD(A13,B13) =MOD(A14,B14)

0 #DIV/0! #DIV/0!

Once row 5 is entered, it is copied to all lower rows. The spreadsheet automatically updates the formulas (that is what spreadsheets do!). A new pair of numbers can be entered in A2 and B2. Note that when a zero remainder occurs, the spreadsheet gives an error message on the following line.

We can produce a more economical version of this by using only one column: the column of remainders.

12345 m 54321 n 12345 4941 2463 15 3 0 #DIV/0! #DIV/0!

12345 54321 =MOD(A2,A3) =MOD(A3,A4) =MOD(A4,A5) =MOD(A5,A6) =MOD(A6,A7) =MOD(A7,A8) =MOD(A8,A9) =MOD(A9,A10)

m n

The formula is entered in the 3rd row and copied to the rows below.

Extended GCD algorithm


Given m,n find A,B so that gcd(m,n) = Am + Bn Set up a spreadsheet as follows. The numbers m and n in cells A3 and B3 can be changed for different problems -- the rest of the spreadsheet does calculations based on what is in these cells.

A 1 2 3 4 5 6 7
m 12345 A 1 0 =A5-E6*A6 0 1 54321

B
n

B =A3 =B3

rem

quot =INT(C5/C6) =INT(C6/C7)

=B5-E6*B6

=C5-E6*C6

Now copy and paste row 7 as many times as you wish to rows 8, 9, . Notice that the formulas adjust themselves.

Here is an example with m=12345 and n=54321

A 1 2 3 4 5 6 7 8 9 10 11 12 13
m 12345 A 1 0 1 -4 9 -22 3617 -18107

B
n 54321 B 0 1 0 1 -2 5 -822 4115

E
Notice that the last non-zero remainder (Column C) is 3. So gcd(m,n)=3. One can prove that Ak*m + Bk*n = Ck In this case the numbers on line 12 show give the result 3 = (3617)m + (-822)n

rem 12345 54321 12345 4941 2463 15 3 0

quot 0 4 2 2 164 5 #DIV/0! #DIV/0!

#DIV/0! #DIV/0! #DIV/0!

In the spreadsheet we have retained all the A, B, r and q that arise in the calculation. When writing a computer program to perform this calculation we note that each row depends only on the two previous rows. We do not have to store all the A, B, r -- just the most recent two values of each. This makes the program a bit harder to understand than the spreadsheet. We will use variables A0, B0, and r0 to represent the previous values, A1, B1 and r1 to represent the current values, and q to represent the current quotient. Program: Extended Greatest Common Divisor (EGCD) Input: positive integers m,n Output: integers A, B ,g so that g=gcd(m,n) and Am+Bn=g Initialization: A0:=1, B0:=0; r0:=m A1:=0, B1:=1; r1:=n

While r1 <> 0 do

% Loop invariant: Aim + Bin = ri

q:=quot(r0,r1) temp := A0-A1*q, A0:=A1, A1:=temp; temp := B0-B1*q, B0:=B1, B1:=temp; temp := r0-r1*q, r0:=r1, r1:=temp; Return A:=A0, B:=B0, g:=r0

Common questions

Powered by AI

Copying and pasting rows in a spreadsheet facilitates GCD calculation by allowing quick replication of the formulaic structure necessary for the Euclidean algorithm. Each row corresponds to a step that uses formulas to calculate the remainder. Once set initially, these formulas can be easily extended to more rows, automatically adjusting cell references, which complete the sequence of operations required by the algorithm without manual recalculation. As new data inputs translate through pasted formulas, this reduces setup time and minimizes errors .

The GCD and its algorithms are foundational in cryptography, particularly in RSA encryption, where they ensure key properties like mutual primality of exponent and totient function. The Euclidean algorithm efficiently checks coprimality needed in generating keys, while the extended GCD algorithm finds multiplicative inverses under modular arithmetic—crucial for decryption in RSA. Specifically, it enables the construction of private keys by resolving the equation d*e ≡ 1 (mod φ(n)), where e is the public exponent and φ(n) is the totient .

In the extended GCD algorithm, coefficients A and B satisfy the equation gcd(m, n) = Am + Bn. These coefficients are calculated throughout the algorithm using a series of iterations where each new pair of coefficients is derived from the previous two through a linear combination involving the quotient from the division process. Initially, A0 and B0 are set to 1 and 0, respectively, and A1 and B1 to 0 and 1. In each step, an update is made: A := A0 - A1*q, and B := B0 - B1*q, where q is the quotient of the current division. This iterative process continues until the remainder, r1, becomes zero, at which point the coefficients A0 and B0 corresponding to the previous remainder are the sought solutions .

Using spreadsheets to compute the GCD enhances understanding by visually breaking down each step of the Euclidean algorithm, allowing learners to observe the process of remainder calculation iteratively. Spreadsheets automatically handle the update and calculation of values based on formulas, aiding efficiency by reducing manual errors and ensuring consistency. Visualization through spreadsheets helps in understanding dependencies across steps and allows easy experimentation with different inputs, aiding deeper comprehension of the algorithm’s mechanics .

The initial conditions for A0, B0, and r0 in the extended GCD algorithm are critical as they establish the foundation on which all subsequent computations build. A0 is set to 1 and B0 to 0, with r0 set to the initial value of m. This ensures that the linear combination starts correctly with m itself. By establishing these initial conditions, the iterative updates maintain the invariant Am + Bn = r throughout. These steps ensure that, at termination, the computed values provide the correct coefficients and GCD, aligning the computations with the algorithm's theoretical principles .

The extended GCD algorithm improves upon the basic Euclidean algorithm by not only computing the GCD but also finding integers A and B such that gcd(m, n) = Am + Bn. This is useful in various applications such as solving linear Diophantine equations and cryptographic algorithms like RSA. By maintaining additional computations for A and B across iterations, the extended algorithm offers a broader utility while maintaining the time efficiency of the basic Euclidean algorithm .

The Euclidean algorithm computes the GCD of two numbers by repeatedly applying the division algorithm, expressed as a = bq + r, where r is the remainder. Starting with a = m and b = n, the process involves replacing a with b and b with r, until r equals zero. The last non-zero remainder is the GCD of m and n. The effectiveness of this algorithm lies in its iterative reduction of the problem size by using the remainder, which is always less than the divisor. This leads to a logarithmic number of steps relative to the size of the smaller initial number, making it efficient .

In the Euclidean algorithm, the significance of the 'last non-zero remainder' lies in its property as the greatest common divisor of the original numbers. As the process of continually computing remainders proceeds, each remainder becomes a potential divisor of both initial numbers. Thus, when the remainder becomes zero, the last non-zero remainder before that point is the largest number that divides both numbers without leaving any remainder, establishing it as the GCD .

Implementing the extended GCD algorithm in a programming context can be more complex than using a spreadsheet because it requires maintaining and updating multiple variables (A, B, r) and ensuring correct handling of changes on each iteration without direct formula updates that spreadsheets provide. The need to manage memory and variable states manually and track relationships across iterations increases the cognitive load and risks errors if any single dependency isn't handled correctly. However, programs offer flexibility, automated execution, and potential integration into larger systems .

The division algorithm focuses on remainders because the Euclidean algorithm reduces the problem size by continually narrowing down the possible divisors of the GCD. The quotient is extraneous to these calculations since it only describes how many times one number fits into another, without affecting the GCD itself. The usefulness of the remainder is in its reduction of magnitude, which directly relates to the efficiency and progress of the algorithm by ensuring each step brings m and n closer to zero or the termination condition .

You might also like