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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use num::arithmetic::traits::{CheckedLcm, Lcm, LcmAssign};
use num::basic::unsigneds::PrimitiveUnsigned;

#[inline]
fn lcm<T: PrimitiveUnsigned>(x: T, y: T) -> T {
    checked_lcm(x, y).unwrap()
}

fn checked_lcm<T: PrimitiveUnsigned>(x: T, y: T) -> Option<T> {
    if x == T::ZERO && y == T::ZERO {
        Some(T::ZERO)
    } else {
        (x / x.gcd(y)).checked_mul(y)
    }
}

macro_rules! impl_lcm {
    ($t:ident) => {
        impl Lcm<$t> for $t {
            type Output = $t;

            /// Computes the LCM (least common multiple) of two numbers.
            ///
            /// $$
            /// f(x, y) = \operatorname{lcm}(x, y).
            /// $$
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n^2)$
            ///
            /// $M(n) = O(1)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is
            /// `max(self.significant_bits(), other.significant_bits())`.
            ///
            /// # Panics
            /// Panics if the result is too large to be represented.
            ///
            /// # Examples
            /// See [here](super::lcm#lcm).
            #[inline]
            fn lcm(self, other: $t) -> $t {
                lcm(self, other)
            }
        }

        impl LcmAssign<$t> for $t {
            /// Replaces a number with the LCM (least common multiple) of it and another number.
            ///
            /// $$
            /// x \gets \operatorname{lcm}(x, y).
            /// $$
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n^2)$
            ///
            /// $M(n) = O(n)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is
            /// `max(self.significant_bits(), other.significant_bits())`.
            ///
            /// # Panics
            /// Panics if the result is too large to be represented.
            ///
            /// # Examples
            /// See [here](super::lcm#lcm_assign).
            #[inline]
            fn lcm_assign(&mut self, other: $t) {
                *self = lcm(*self, other);
            }
        }

        impl CheckedLcm<$t> for $t {
            type Output = $t;

            /// Computes the LCM (least common multiple) of two numbers, returning `None` if the
            /// result is too large to represent.
            ///
            /// $$
            /// f(x, y) = \\begin{cases}
            ///     \operatorname{Some}(\operatorname{lcm}(x, y)) &
            ///         \text{if} \\quad \operatorname{lcm}(x, y) < 2^W, \\\\
            ///     \operatorname{None} & \text{if} \\quad \operatorname{lcm}(x, y) \geq 2^W,
            /// \\end{cases}
            /// $$
            /// where $W$ is `Self::WIDTH`.
            ///
            /// # Worst-case complexity
            /// $T(n) = O(n^2)$
            ///
            /// $M(n) = O(n)$
            ///
            /// where $T$ is time, $M$ is additional memory, and $n$ is
            /// `max(self.significant_bits(), other.significant_bits())`.
            ///
            /// # Examples
            /// See [here](super::lcm#checked_lcm).
            #[inline]
            fn checked_lcm(self, other: $t) -> Option<$t> {
                checked_lcm(self, other)
            }
        }
    };
}
apply_to_unsigneds!(impl_lcm);