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
pub trait Gcd {
    fn gcd(self, other: Self) -> Self;
}

macro_rules! impl_uint {
    ( $($ty:ty)* ) => { $(
        impl Gcd for $ty {
            fn gcd(mut self, mut other: Self) -> Self {
                while other != 0 {
                    let tmp = self % other;
                    self = std::mem::replace(&mut other, tmp);
                }
                self
            }
        }
    )* }
}

impl_uint! { u8 u16 u32 u64 u128 usize }

#[test]
fn sanity_check() {
    assert_eq!(24_u32.gcd(16), 8);
    assert_eq!(0_u32.gcd(0), 0);
}