forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extended_euclidean_algorithm.rs
37 lines (31 loc) · 1.15 KB
/
extended_euclidean_algorithm.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
fn update_step(a: &mut i32, old_a: &mut i32, quotient: i32) {
let temp = *a;
*a = *old_a - quotient * temp;
*old_a = temp;
}
pub fn extended_euclidean_algorithm(a: i32, b: i32) -> (i32, i32, i32) {
let (mut old_r, mut rem) = (a, b);
let (mut old_s, mut coeff_s) = (1, 0);
let (mut old_t, mut coeff_t) = (0, 1);
while rem != 0 {
let quotient = old_r / rem;
update_step(&mut rem, &mut old_r, quotient);
update_step(&mut coeff_s, &mut old_s, quotient);
update_step(&mut coeff_t, &mut old_t, quotient);
}
(old_r, old_s, old_t)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic() {
assert_eq!(extended_euclidean_algorithm(101, 13), (1, 4, -31));
assert_eq!(extended_euclidean_algorithm(123, 19), (1, -2, 13));
assert_eq!(extended_euclidean_algorithm(25, 36), (1, 13, -9));
assert_eq!(extended_euclidean_algorithm(69, 54), (3, -7, 9));
assert_eq!(extended_euclidean_algorithm(55, 79), (1, 23, -16));
assert_eq!(extended_euclidean_algorithm(33, 44), (11, -1, 1));
assert_eq!(extended_euclidean_algorithm(50, 70), (10, 3, -2));
}
}