Extension of Euclid’s Algorithm
We already know that, for any two whole numbers a and b, there exist integers s and t such that as + bt = gcd(a,b). In other words, gcd(a,b) is a linear combination of a and b. gcd(a,b) is the least positive combination of the two integers. a and b themselves are expressed as trivial combinations: a = 1· + 0·b and b = 0·a + 1·b. Starting with these two, an extension of Euclid’s algorithm finds s and t whose existence was so far only established in a formal way.
Write the two linear combinations in a column and apply one step of Euclid’s algorithm to the left-hand side. Assuming a = bp + r. Multiply the second equation by p and subtract it from the first equation:
a = 1·a + 0·b
b = 0·a + 1·b
r = 1·a + (-p)·b
Apply the same procedure to the last two equations. Continue in this manner until Euclid’s algorithm on the left stops. On the right, there will be the linear combination we are after. Let check this with an example: let a = 2322, b = 654. I adopt the usual convention of solving linear equations and omit all the terms in a linear combination but the left-hand side and the two coefficients on the right. The results are placed into a table with the fourth column being equal to p (from a = bp + r, which changes on each step. Multiply three numbers to the left of p by p and subtract them from the numbers directly above them. Record the results on the next line.
int algoritmoeuclides(int a,int b)
if (a%b==0)
return b;
return algoritmoeuclides(b,a%b);
}
int main(array<System::String ^> ^args)
{
int a=525;
int b=231;
printf("%d",algoritmoeuclides(a,b));
getch();
}
this is my code so far, it works perfect . The problem is when i try to find s and t. I dont know how to find it , ive searched on forums but idk whats the best way to program this algorithm to find S and T.
Ive put all the explanation to give you guys an idea.
PD:Sorry for my english im not an english-speaker. Any idea d be aprecciated.
Here you go
http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm