You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algebra/extended-euclid-algorithm.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -72,10 +72,10 @@ This implementation of extended Euclidean algorithm produces correct results for
72
72
## Iterative version
73
73
74
74
It's also possible to write the Extended Euclidean algorithm in an iterative way.
75
-
Because we it avoids recursion, the code will run a little bit faster than the recursive one.
75
+
Because it avoids recursion, the code will run a little bit faster than the recursive one.
76
76
77
77
```cpp extended_gcd_iter
78
-
int gcd(int a, int b, int &x, int &y) {
78
+
int gcd(int a, int b, int& x, int& y) {
79
79
x = 1, y = 0;
80
80
int x1 = 0, y1 = 1, a1 = a, b1 = b;
81
81
while (b1) {
@@ -88,9 +88,9 @@ int gcd(int a, int b, int &x, int &y) {
88
88
}
89
89
```
90
90
91
-
If you look at the variable `a1` and `b1`, they taking exactly the same values as in the iterative version of the normal [Euclidean algorithm](algebra/euclid-algorithm.html).
91
+
If you look closely at the variable `a1` and `b1`, you can notice that they taking exactly the same values as in the iterative version of the normal [Euclidean algorithm](algebra/euclid-algorithm.html). So the algorithm will at least compute the correct GCD.
92
92
93
-
To see why the algorithm work, you can check that the following invariants will at all time (before the while loop, and at the end of each iteration): $x \cdot a + y \cdot b = a_1$ and $x_1 \cdot a + y_1 \cdot b = b_1$.
93
+
To see why the algorithm also computes the correct coefficients, you can check that the following invariants will hold at any time (before the while loop, and at the end of each iteration): $x \cdot a + y \cdot b = a_1$ and $x_1 \cdot a + y_1 \cdot b = b_1$.
94
94
It's trivial to see, that these two equations are satisfied at the beginning.
95
95
And you can check that the update in the loop iteration will still keep those equalities valid.
0 commit comments