Skip to main content

malachite_nz/natural/arithmetic/
mod_is_reduced.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::natural::Natural;
10use malachite_base::num::arithmetic::traits::ModIsReduced;
11use malachite_base::num::basic::traits::Zero;
12
13impl ModIsReduced for Natural {
14    /// Returns whether a [`Natural`] is reduced modulo another [`Natural`] $m$; in other words,
15    /// whether it is less than $m$.
16    ///
17    /// $m$ cannot be zero.
18    ///
19    /// $f(x, m) = (x < m)$.
20    ///
21    /// # Worst-case complexity
22    /// $T(n) = O(n)$
23    ///
24    /// $M(n) = O(1)$
25    ///
26    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
27    ///
28    /// # Panics
29    /// Panics if `m` is 0.
30    ///
31    /// # Examples
32    /// ```
33    /// use malachite_base::num::arithmetic::traits::{ModIsReduced, Pow};
34    /// use malachite_base::num::basic::traits::{One, Zero};
35    /// use malachite_nz::natural::Natural;
36    ///
37    /// assert_eq!(Natural::ZERO.mod_is_reduced(&Natural::from(5u32)), true);
38    /// assert_eq!(
39    ///     Natural::from(10u32)
40    ///         .pow(12)
41    ///         .mod_is_reduced(&Natural::from(10u32).pow(12)),
42    ///     false
43    /// );
44    /// assert_eq!(
45    ///     Natural::from(10u32)
46    ///         .pow(12)
47    ///         .mod_is_reduced(&(Natural::from(10u32).pow(12) + Natural::ONE)),
48    ///     true
49    /// );
50    /// ```
51    #[inline]
52    fn mod_is_reduced(&self, m: &Self) -> bool {
53        assert_ne!(*m, Self::ZERO);
54        self < m
55    }
56}