Implement day 15 of 2016.

This commit is contained in:
2018-11-12 15:22:37 +01:00
parent 42d075f8f4
commit 6ad8f4cb56
5 changed files with 108 additions and 0 deletions

View File

@@ -29,6 +29,24 @@ pub fn prime_sieve(dest: &mut[bool]) {
}
}
/// Greatest common divisor
pub fn gcd(a: i32, b: i32) -> i32 {
if a < b {
gcd(b, a)
} else {
if a % b == 0 {
b
} else {
gcd(a % b, b)
}
}
}
/// Least common multiple
pub fn lcm(a: i32, b: i32) -> i32 {
a * b / gcd(a, b)
}
/// Solution trait
///
/// Every day's solution should implement this function so that it can
@@ -60,4 +78,15 @@ mod tests {
assert_eq!(output, input);
}
#[test]
fn test_gcd() {
assert_eq!(12, gcd(24, 36));
assert_eq!(1, gcd(1, 7));
}
#[test]
fn test_lcm() {
assert_eq!(12, lcm(6, 4));
}
}