1
0
Fork 0
2023/src/math.rs
Matej Focko caa38e535a
feat(math): add ‹gcd› and ‹lcm› to the library
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-09 13:29:39 +01:00

19 lines
359 B
Rust

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
}