Merge branch 'master' of git.timshomepage.net:timw4mail/rusty-numbers
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good
This commit is contained in:
commit
9f8e57f3d3
36
src/num.rs
36
src/num.rs
@ -2,7 +2,8 @@
|
|||||||
//!
|
//!
|
||||||
//! Home to the numeric trait chain of doom, aka `Unsigned`
|
//! Home to the numeric trait chain of doom, aka `Unsigned`
|
||||||
#![allow(unused_comparisons)]
|
#![allow(unused_comparisons)]
|
||||||
use core::cmp::{max, min, Ordering};
|
|
||||||
|
use core::cmp::Ordering;
|
||||||
use core::convert::TryFrom;
|
use core::convert::TryFrom;
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use core::ops::{
|
use core::ops::{
|
||||||
@ -143,12 +144,6 @@ pub trait Unsigned:
|
|||||||
/// Find the greatest common denominator of two numbers
|
/// Find the greatest common denominator of two numbers
|
||||||
fn gcd(a: Self, b: Self) -> Self;
|
fn gcd(a: Self, b: Self) -> Self;
|
||||||
|
|
||||||
/// Euclid gcd algorithm
|
|
||||||
fn e_gcd(a: Self, b: Self) -> Self;
|
|
||||||
|
|
||||||
/// Stein gcd algorithm
|
|
||||||
fn stein_gcd(a: Self, b: Self) -> Self;
|
|
||||||
|
|
||||||
/// Find the least common multiple of two numbers
|
/// Find the least common multiple of two numbers
|
||||||
fn lcm(a: Self, b: Self) -> Self;
|
fn lcm(a: Self, b: Self) -> Self;
|
||||||
}
|
}
|
||||||
@ -246,31 +241,6 @@ macro_rules! impl_unsigned {
|
|||||||
Self::gcd((b - a) >> 1, a)
|
Self::gcd((b - a) >> 1, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn e_gcd(x: $Type, y: $Type) -> $Type {
|
|
||||||
let mut x = x;
|
|
||||||
let mut y = y;
|
|
||||||
while y != 0 {
|
|
||||||
let t = y;
|
|
||||||
y = x % y;
|
|
||||||
x = t;
|
|
||||||
}
|
|
||||||
x
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stein_gcd(a: Self, b: Self) -> Self {
|
|
||||||
match ((a, b), (a & 1, b & 1)) {
|
|
||||||
((x, y), _) if x == y => y,
|
|
||||||
((0, x), _) | ((x, 0), _) => x,
|
|
||||||
((x, y), (0, 1)) | ((y, x), (1, 0)) => Self::stein_gcd(x >> 1, y),
|
|
||||||
((x, y), (0, 0)) => Self::stein_gcd(x >> 1, y >> 1) << 1,
|
|
||||||
((x, y), (1, 1)) => {
|
|
||||||
let (x, y) = (min(x, y), max(x, y));
|
|
||||||
Self::stein_gcd((y - x) >> 1, x)
|
|
||||||
}
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lcm(a: $Type, b: $Type) -> $Type {
|
fn lcm(a: $Type, b: $Type) -> $Type {
|
||||||
if (a == 0 && b == 0) {
|
if (a == 0 && b == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -344,8 +314,6 @@ mod tests {
|
|||||||
assert_eq!(u8::gcd(2, 2), 2);
|
assert_eq!(u8::gcd(2, 2), 2);
|
||||||
assert_eq!(u8::gcd(2, 8), 2);
|
assert_eq!(u8::gcd(2, 8), 2);
|
||||||
assert_eq!(u16::gcd(36, 48), 12);
|
assert_eq!(u16::gcd(36, 48), 12);
|
||||||
assert_eq!(u16::e_gcd(36, 48), 12);
|
|
||||||
assert_eq!(u16::stein_gcd(36, 48), 12);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user