gcd/lib.rs
1pub trait Gcd {
2 fn gcd(self, other: Self) -> Self;
3}
4
5macro_rules! impl_uint {
6 ( $($ty:ty)* ) => { $(
7 impl Gcd for $ty {
8 fn gcd(mut self, mut other: Self) -> Self {
9 while other != 0 {
10 let tmp = self % other;
11 self = std::mem::replace(&mut other, tmp);
12 }
13 self
14 }
15 }
16 )* }
17}
18
19impl_uint! { u8 u16 u32 u64 u128 usize }
20
21#[test]
22fn sanity_check() {
23 assert_eq!(24_u32.gcd(16), 8);
24 assert_eq!(0_u32.gcd(0), 0);
25}