This site already has The greatest common divisor of two integers, which uses the Euclidean algorithm. As it turns out (for me), there exists an Extended Euclidean algorithm. This algorithm computes, besides the greatest common divisor of integers a and b, the coefficients of Bézout's identity, that is, integers x and y such that
So it allows computing the quotients of a and b by their greatest common divisor.
You can see the calculator below, and theory, as usual, us under the calculator.
The extended algorithm uses recursion and computes coefficients on its backtrack. The formulas for calculations can be obtained from the following considerations:
Let us know coefficients for pair , such as:
and we need to calculate coefficients for pair , such as:
First, we replace with:
- quotient from integer division of b to a,
and use it as substitute in:
Then, after regroup we get:
By comparing this with starting equation we can express x and y:
The start of recursion backtracking is the end of the Euclidean algorithm, when a = 0 and GCD = b, so first x and y are 0 and 1, respectively. Further coefficients are computed using the formulas above.