扩展欧几里得算法
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));
}
}
当前内容版权归 rustlang-cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rustlang-cn .