We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 90840ce commit f6a7a8bCopy full SHA for f6a7a8b
src/algebra/euclid-algorithm.md
@@ -112,18 +112,18 @@ It's based on a few properties:
112
Using only these properties, and some fast bitwise functions from GCC, we can implement a fast version:
113
114
```cpp
115
-int gcd(int u, int v) {
116
- if (!u || !v)
117
- return u | v;
118
- unsigned shift = __builtin_ctz(u | v);
119
- u >>= __builtin_ctz(u);
+int gcd(int a, int b) {
+ if (!a || !b)
+ return a | b;
+ unsigned shift = __builtin_ctz(a | b);
+ a >>= __builtin_ctz(a);
120
do {
121
- v >>= __builtin_ctz(v);
122
- if (u > v)
123
- swap(u, v);
124
- v -= u;
125
- } while (v);
126
- return u << shift;
+ b >>= __builtin_ctz(b);
+ if (a > b)
+ swap(a, b);
+ b -= a;
+ } while (b);
+ return a << shift;
127
}
128
```
129
0 commit comments