forked from mocobeta/rust99
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
30 lines (28 loc) · 620 Bytes
/
Copy pathlib.rs
File metadata and controls
30 lines (28 loc) · 620 Bytes
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
pub fn gcd(a: u32, b: u32) -> u32 {
fn gcd_rec(m: u32, n: u32) -> u32 {
assert!(
m >= n,
"m must be larger than or equal to n; got m={}, n={}",
m,
n
);
if n == 0 {
m
} else {
gcd_rec(n, m % n)
}
}
let (m, n) = if a >= b { (a, b) } else { (b, a) };
gcd_rec(m, n)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gcd() {
assert_eq!(gcd(36, 63), 9);
assert_eq!(gcd(63, 36), 9);
assert_eq!(gcd(7, 21), 7);
assert_eq!(gcd(19, 13), 1);
}
}