1
0
Fork 0

feat(math): add ‹gcd› and ‹lcm› to the library

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-09 13:29:39 +01:00
parent 88d06b99ec
commit caa38e535a
Signed by: mfocko
GPG key ID: 7C47D46246790496
3 changed files with 28 additions and 18 deletions

View file

@ -20,18 +20,6 @@ impl Direction {
}
}
fn gcd(mut x: u64, mut y: u64) -> u64 {
while y != 0 {
(x, y) = (y, x % y);
}
x
}
fn lcm(x: u64, y: u64) -> u64 {
x / gcd(x, y) * y
}
struct Day08 {
instructions: Vec<Direction>,
graph: HashMap<String, (String, String)>,

View file

@ -1,8 +1,17 @@
#![allow(unused_imports)]
mod data_structures;
pub use data_structures::*;
mod input;
pub use input::*;
mod iterators;
pub use iterators::*;
mod math;
pub use math::*;
mod solution;
pub use solution::*;
@ -11,9 +20,3 @@ pub use testing::*;
mod vectors;
pub use vectors::*;
mod iterators;
pub use iterators::*;
mod data_structures;
pub use data_structures::*;

19
src/math.rs Normal file
View file

@ -0,0 +1,19 @@
use std::ops::{Div, Mul, Rem};
pub fn gcd<T>(mut x: T, mut y: T) -> T
where
T: Copy + Default + PartialEq + Rem<Output = T>,
{
while y != T::default() {
(x, y) = (y, x % y);
}
x
}
pub fn lcm<T>(x: T, y: T) -> T
where
T: Copy + Default + PartialEq + Div<Output = T> + Mul<Output = T> + Rem<Output = T>,
{
x / gcd(x, y) * y
}