mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 13:20:32 +01:00
Implement day 15 of 2016.
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user