This site already has The greatest common divisor of two integers, which uses Euclidean algorithm. As it turns out (for me), there exists 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 to compute 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
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:
Start of recursion backtracking is the end of 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.