diff --git a/2016/src/common.rs b/2016/src/common.rs index bd023e5..fe1ed30 100644 --- a/2016/src/common.rs +++ b/2016/src/common.rs @@ -1,4 +1,6 @@ use std::io; +use std::ops; +use std::cmp; /// Apply Erathostenes's sieve to the supplied array /// @@ -30,11 +32,11 @@ pub fn prime_sieve(dest: &mut[bool]) { } /// Greatest common divisor -pub fn gcd(a: i32, b: i32) -> i32 { +pub fn gcd + cmp::PartialOrd + std::convert::From + Copy>(a: T, b: T) -> T { if a < b { gcd(b, a) } else { - if a % b == 0 { + if a % b == T::from(0) { b } else { gcd(a % b, b) @@ -43,7 +45,7 @@ pub fn gcd(a: i32, b: i32) -> i32 { } /// Least common multiple -pub fn lcm(a: i32, b: i32) -> i32 { +pub fn lcm + ops::Mul + ops::Div + cmp::PartialOrd + std::convert::From + Copy>(a: T, b: T) -> T { a * b / gcd(a, b) }