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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/* Copyright 2022 Mario Finelli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! Utility functions for Advent of Code.
use std::ops::{Div, Mul, Rem};
/// Compute the greatest common divisor.
///
/// The greatest common divisor is defined as largest positive integer (among
/// a set of integers that are not all zero) that divides each of the integers.
///
/// Computes the greatest common divisor using the [Euclidean
/// algorithm](https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclidean_algorithm).
///
/// # Example
/// ```rust
/// # use aoc::util;
/// assert_eq!(util::gcd(48, 18), 6);
/// assert_eq!(util::gcd(18, 48), 6);
/// ```
pub fn gcd<T>(a: T, b: T) -> T
where
T: PartialEq + Rem<Output = T> + From<u8> + Copy,
{
if b == 0u8.into() {
a
} else {
gcd(b, a % b)
}
}
/// Compure the least common multiple.
///
/// The least common multiple is defined as the smallest possible integer that
/// is divisible by both of two other integers.
///
/// Computes the least common multiple using the [greatest common
/// divisor](https://en.wikipedia.org/wiki/Least_common_multiple#Using_the_greatest_common_divisor).
///
/// # Example
/// ```rust
/// # use aoc::util;
/// assert_eq!(util::lcm(21, 6), 42);
/// ```
pub fn lcm<T>(a: T, b: T) -> T
where
T: PartialEq
+ Div<Output = T>
+ Mul<Output = T>
+ Rem<Output = T>
+ From<u8>
+ Copy,
{
a * b / gcd(a, b)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gcd() {
assert_eq!(gcd(1, 0), 1);
assert_eq!(gcd(0, 1), 1);
assert_eq!(gcd(7, 1), 1);
assert_eq!(gcd(120, 84), 12);
assert_eq!(gcd(84, 120), 12);
}
#[test]
fn test_lcm() {
assert_eq!(lcm(1, 0), 0);
assert_eq!(lcm(1, 1), 1);
assert_eq!(lcm(9, 12), 36);
assert_eq!(lcm(7, 1), 7);
}
}