The extended euclidean algorithm
The extended euclidean algorithm Given two integers a and b, we often need to find other two integers s and t such that s * a + t * b = gcd ( a, b ) The extended euclidean algorithm can calculate the gcd ( a, b ) and at the same time calculate the values of s and t. The algorithm uses three sets of variables r's, s's and t's. In each state r1, r2 and r have same value in the euclidean algorithm. The variables r1, r2 are initializes t the value of a and b respectively. The variables s1, s2 are initialized to 1 and 0 respectively. The variables t1 and t2 are initialized t 0 and 1 respectively. r1 = a ; r2 = b ; ( Initialization) s1 ← 1 ; s2 ← 0 ; t1 ← 0 ; t2 ← 1 ; while ( r2 > 0) { q = r1 / r2; r = r1 - q * r2 ; (updating r ) r1 ← r2; r2 ← r ; s ← s1 - q * s2 ; s1 ← s2 ; s2 ← s ; (updating S's) t ← ...