Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Let $a$ and $b$ be two numbers whose $\gcd{(a,b)}=ax+by$. How do I find $x$ and $y$?
I did it like this.

There is a $c$ which is a multiple of $\gcd{(a,b)}$ . Then $c=d \gcd{(a,b)}=d(ax+by)$ . Then I used this equation and the above most equation to find $x$ and $y$.

The thing is I have to write an algorithm and code it in c++ to find $x$ and $y$.

share|improve this question
2  
See this answer for one convenient method of implementing the extended Euclidean algorithm. –  Bill Dubuque Nov 12 '12 at 16:57
    
Maybe look at Wikipedia. It has pseudo-code for a couple of approaches. –  André Nicolas Nov 12 '12 at 17:34

1 Answer 1

It depends partially on whether you want to save space or time. Here's an example. Suppose I want to express $(213, 87)$ as a linear combination of $213$ and $87$. First, I do the Euclidean algorithm to find the gcd: $$\matrix{ 213 & - \cr 87 & 2 \cr 39 & 2 \cr 9 & 4 \cr 3 & 3 \cr}$$

So, e.g., $213 = 87 \cdot 2 + 39$: The quotients go in the second column, the remainders in the first. I continue dividing the previous remainder by the current remainder until the remainder is $0$.

The gcd is the last nonzero remainder, which is $3$.

Next, I do a "backward recursion", starting from the bottom in a third column with $0$, then $1$:

$$\matrix{ 213 & - & \cr 87 & 2 & \cr 39 & 2 & \cr 9 & 4 & 1 \cr 3 & 3 & 0 \cr}$$

I build up the numbers in the third column this way: $$\matrix{ & & 4 \cr & & \uparrow \cr 4 & \cdot & 1 \cr & & + \cr & & 0 \cr}$$

(i.e. $4 \cdot 1 + 0 = 4$, which is the next element). Continuing in this way, I get

$$\matrix{ 213 & - & 22 \cr 87 & 2 & 9 \cr 39 & 2 & 4 \cr 9 & 4 & 1 \cr 3 & 3 & 0 \cr}$$

To get the linear combination, "cross-multiply" the original two numbers with the first two numbers in the third column, subtracting in the order that gives the gcd $3$ (the wrong order gives $-3$ --- you can tell which way to go by just considering the units digits, or by working out an appropriate formula): $$22 \cdot 87 + (-9) \cdot 213 = 3 = (213, 87).$$

The proof that this works is a straightforward (backward) induction.

Pros and cons: The "two-column" approaches that are often used are space-efficient, since you only need to retain the last two rows to get the next one. The backward recursion requires that you retain the entire list of Euclidean algorithm quotients.

On the other hand, the backward recursion does roughly half the computations as the "two-column" approach. For that reason, I find it better for my classes, where hand computations are going to be relatively small.

By the way, all of these work for computing polynomial gcds as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.