-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd.rs
246 lines (224 loc) · 5.6 KB
/
gcd.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/// Recursive Euclidean Algorithm
///
/// The key point of this algorithm:
/// - The divisor from the previous division becomes the next dividend.
/// - The remainder becomes the next divisor.
/// - This process continues until the remainder is 0, at which point the
/// GCD is found.
///
/// Example of how the division steps work:
///
/// - a / b = q1, remainder r1
/// - b / r1 = q2, remainder r2
/// - r1 / r2 = q3, remainder r3
/// - r2 / r3 = q4, remainder r4
/// - Continue until `rN = 0` and the corresponding dividend is the GCD.
///
/// Recursive relationship: $\gcd(a, b) = \gcd(b, a \bmod b)$
///
/// # See also
/// - [Euclidean algorithm explanation](https://scienceland.info/en/algebra8/euclid-algorithm)
pub fn gcd(a: i32, b: i32) -> i32 {
// when we reach b == 0 then gcd(a, 0) = a
if b == 0 {
return a;
}
// passing b as dividend and a % b (the remainder) as divisor
gcd(b, a % b).abs()
}
/// Iterative gcd
pub fn gcdi(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
// b is carried over as dividend
let carry: i32 = b;
// calculate the remainder
b = a % b;
// set a to the carry (b becomes the next dividend)
a = carry;
}
// at this point the current remainder is 0 => gcd(r_p, r) = gcd(0, r) = r
a.abs()
}
/// Recursive Extended Euclidean Algorithm
///
/// #### Overview
///
/// The Extended Euclidean Algorithm not only computes the greatest common divisor (GCD) of two integers $ a $ and $ b $ but also finds integers $ x $ and $ y $ satisfying:
///
/// \[
/// \gcd(a, b) = a \times x + b \times y
/// \]
///
/// #### The Algorithm Steps
///
/// 1. **Base Case**: If $ b = 0 $, the GCD is $ a $, and the coefficients are $ x = 1 $ and $ y = 0 $:
///
/// \[
/// \gcd(a, 0) = a = a \times 1 + 0 \times 0
/// \]
///
/// 2. **Recursive Case**: If $ b \neq 0 $, we recursively call the function with $ (b, a \bmod b) $:
///
/// \[
/// \gcd(a, b) = \gcd(b, a \bmod b)
/// \]
///
/// #### Deriving the Update Formulas
///
/// Let's derive the update formulas for $ x $ and $ y $ based on the recursive call.
///
/// ##### Step 1: Recursive Call
///
/// We start with the recursive call:
///
/// \[
/// \gcd(b, a \bmod b) = b \times x + (a \bmod b) \times y
/// \]
///
/// This gives us:
///
/// 1. $ \gcd(b, a \bmod b) = d $
/// 2. Coefficients $ x $ and $ y $ such that:
///
/// \[
/// d = b \times x + (a \bmod b) \times y
/// \]
///
/// ##### Step 2: Express $ a \bmod b $ in Terms of $ a $ and $ b $
///
/// Recall that:
///
/// \[
/// a \bmod b = a - \left\lfloor \frac{a}{b} \right\rfloor \times b
/// \]
///
/// Let $ q = \left\lfloor \frac{a}{b} \right\rfloor $, so:
///
/// \[
/// a \bmod b = a - q \times b
/// \]
///
/// ##### Step 3: Substitute Back into the GCD Equation
///
/// Substitute $ a \bmod b $ into the equation:
///
/// \[
/// d = b \times x + (a - q \times b) \times y
/// \]
///
/// Simplify:
///
/// \[
/// d = b \times x + a \times y - q \times b \times y
/// \]
///
/// Group like terms:
///
/// \[
/// d = a \times y + b \times (x - q \times y)
/// \]
///
/// ##### Step 4: Identify New Coefficients $ x $ and $ y $
///
/// From the rearranged equation, we can identify:
///
/// - Coefficient of $ a $: $ y $
/// - Coefficient of $ b $: $ x - q \times y $
///
/// Thus, we can set:
///
/// \[
/// x = y \newline
/// y = x - q \times y
/// \]
///
/// This ensures:
///
/// \[
/// d = a \times x + b \times y
/// \]
pub fn egcd(a: i32, b: i32) -> (i32, i32, i32) {
// when we reach b == 0, then gcd = a, xp = 1, yp = 0
if b == 0 {
return (a, 1, 0);
}
let (gcd, x, y) = egcd(b, a % b);
// going backward to find previous x_p and y_p
let x_p = y;
let y_p = x - a / b * y;
return (gcd, x_p, y_p);
}
/// Iterative Extended Euclidean algorithm
///
/// Finds integer coefficients x and y such that: ax + by = gcd(a, b)
/// Suppose the previous remainder is $r_p = ax_p + by_p$ and the current remainder is $r = ax + by$. We have:
///
/// $$
/// \begin{align}
/// &r_p = q + new r \newline
/// &new \ r = r_p - qr = (ax_p + by_p) - q(ax + by) = a(x_p - x) + b(y_p - y) \newline
/// &new \ x = x_p - x,\ new \ y = y_p - y \newline
/// \end{align}
/// $$
///
/// # See also
/// - [Extended Euclidean Algorithm](http://anh.cs.luc.edu/331/notes/xgcd.pdf)
pub fn egcdi(mut a: i32, mut b: i32) -> (i32, i32, i32) {
// first remainder
let x0 = 1;
let y0 = 0;
// second remainder
let x1 = 0;
let y1 = 1;
// set initial values
let mut x_p = x0;
let mut x = x1;
let mut y_p = y0;
let mut y = y1;
while b != 0 {
let q = a / b;
let new_x = x_p - q * x;
let new_y = y_p - q * y;
let new_a = b;
let new_b = a % b;
// update prev values
x_p = x;
y_p = y;
// update current values
x = new_x;
y = new_y;
// update a and b
a = new_a;
b = new_b;
}
return (a, x_p, y_p);
}
#[test]
fn test_gcd() {
assert_eq!(gcd(14, 15), 1);
assert_eq!(gcd(216, 111), 3);
assert_eq!(gcd(216, -111), 3);
assert_eq!(gcd(-216, -111), 3);
assert_eq!(gcdi(15, 14), 1);
assert_eq!(gcdi(216, 111), 3);
assert_eq!(gcdi(216, -111), 3);
assert_eq!(gcdi(-216, -111), 3);
}
#[test]
fn test_egcd() {
let (gcd, x_p, y_p) = egcd(35, 15);
assert_eq!(gcd, 5);
assert_eq!(x_p, 1);
assert_eq!(y_p, -2);
let (gcd, x_p, y_p) = egcd(222, 97);
assert_eq!(gcd, 1);
assert_eq!(x_p, -45);
assert_eq!(y_p, 103);
}
#[test]
fn test_egcdi() {
let (gcd, x, y) = egcdi(35, 15);
assert_eq!(gcd, 5);
assert_eq!(x, 1);
assert_eq!(y, -2);
}