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
use std::ops::Rem;
use super::integer::Integer;
/// return greatest common divisor (GCD) of integers.
///
/// # Arguments
///
/// * `numbers` - The integers to calculate gcd.
///
/// # Returns
///
/// Greatest common divisor of integers.
///
/// # Examples
///
/// ```
/// use rufl::math;
///
/// assert_eq!(5, math::gcd(&vec![5]));
///
/// assert_eq!(6, math::gcd(&vec![6, 12, 18]));
///
/// ```
pub fn gcd<T>(numbers: &Vec<T>) -> T
where
T: Integer + Rem<Output = T>,
{
if numbers.is_empty() {
return T::MIN;
}
let mut result = numbers[0];
for n in &numbers[1..] {
result = calculate_gcd(*n, result);
}
result
}
pub(crate) fn calculate_gcd<T: Integer + Rem<Output = T>>(a: T, b: T) -> T {
if a == T::ZERO {
b
} else if b == T::ZERO {
a
} else {
calculate_gcd(b, a % b)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gcd() {
assert_eq!(6, gcd(&vec![6]));
assert_eq!(6, gcd(&vec![12, 6]));
assert_eq!(6, gcd(&vec![48, 18, 6]));
}
}