malachite_nz/integer/arithmetic/divisible_by.rs
1// Copyright © 2025 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::integer::Integer;
10use malachite_base::num::arithmetic::traits::DivisibleBy;
11
12impl DivisibleBy<Self> for Integer {
13 /// Returns whether an [`Integer`] is divisible by another [`Integer`]; in other words, whether
14 /// the first is a multiple of the second. Both [`Integer`]s are taken by value.
15 ///
16 /// This means that zero is divisible by any [`Integer`], including zero; but a nonzero
17 /// [`Integer`] is never divisible by zero.
18 ///
19 /// It's more efficient to use this function than to compute the remainder and check whether
20 /// it's zero.
21 ///
22 /// # Worst-case complexity
23 /// $T(n) = O(n \log n \log \log n)$
24 ///
25 /// $M(n) = O(n \log n)$
26 ///
27 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
28 ///
29 /// # Examples
30 /// ```
31 /// use core::str::FromStr;
32 /// use malachite_base::num::arithmetic::traits::DivisibleBy;
33 /// use malachite_base::num::basic::traits::Zero;
34 /// use malachite_nz::integer::Integer;
35 ///
36 /// assert_eq!(Integer::ZERO.divisible_by(Integer::ZERO), true);
37 /// assert_eq!(Integer::from(-100).divisible_by(Integer::from(-3)), false);
38 /// assert_eq!(Integer::from(102).divisible_by(Integer::from(-3)), true);
39 /// assert_eq!(
40 /// Integer::from_str("-1000000000000000000000000")
41 /// .unwrap()
42 /// .divisible_by(Integer::from_str("1000000000000").unwrap()),
43 /// true
44 /// );
45 /// ```
46 fn divisible_by(self, other: Self) -> bool {
47 self.abs.divisible_by(other.abs)
48 }
49}
50
51impl DivisibleBy<&Self> for Integer {
52 /// Returns whether an [`Integer`] is divisible by another [`Integer`]; in other words, whether
53 /// the first is a multiple of the second. The first [`Integer`] is taken by value and the
54 /// second by reference.
55 ///
56 /// This means that zero is divisible by any [`Integer`], including zero; but a nonzero
57 /// [`Integer`] is never divisible by zero.
58 ///
59 /// It's more efficient to use this function than to compute the remainder and check whether
60 /// it's zero.
61 ///
62 /// # Worst-case complexity
63 /// $T(n) = O(n \log n \log \log n)$
64 ///
65 /// $M(n) = O(n \log n)$
66 ///
67 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
68 ///
69 /// # Examples
70 /// ```
71 /// use core::str::FromStr;
72 /// use malachite_base::num::arithmetic::traits::DivisibleBy;
73 /// use malachite_base::num::basic::traits::Zero;
74 /// use malachite_nz::integer::Integer;
75 ///
76 /// assert_eq!(Integer::ZERO.divisible_by(&Integer::ZERO), true);
77 /// assert_eq!(Integer::from(-100).divisible_by(&Integer::from(-3)), false);
78 /// assert_eq!(Integer::from(102).divisible_by(&Integer::from(-3)), true);
79 /// assert_eq!(
80 /// Integer::from_str("-1000000000000000000000000")
81 /// .unwrap()
82 /// .divisible_by(&Integer::from_str("1000000000000").unwrap()),
83 /// true
84 /// );
85 /// ```
86 fn divisible_by(self, other: &Self) -> bool {
87 self.abs.divisible_by(&other.abs)
88 }
89}
90
91impl DivisibleBy<Integer> for &Integer {
92 /// Returns whether an [`Integer`] is divisible by another [`Integer`]; in other words, whether
93 /// the first is a multiple of the second. The first [`Integer`] is taken by reference and the
94 /// second by value.
95 ///
96 /// This means that zero is divisible by any [`Integer`], including zero; but a nonzero
97 /// [`Integer`] is never divisible by zero.
98 ///
99 /// It's more efficient to use this function than to compute the remainder and check whether
100 /// it's zero.
101 ///
102 /// # Worst-case complexity
103 /// $T(n) = O(n \log n \log \log n)$
104 ///
105 /// $M(n) = O(n \log n)$
106 ///
107 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
108 ///
109 /// # Examples
110 /// ```
111 /// use core::str::FromStr;
112 /// use malachite_base::num::arithmetic::traits::DivisibleBy;
113 /// use malachite_base::num::basic::traits::Zero;
114 /// use malachite_nz::integer::Integer;
115 ///
116 /// assert_eq!((&Integer::ZERO).divisible_by(Integer::ZERO), true);
117 /// assert_eq!(
118 /// (&Integer::from(-100)).divisible_by(Integer::from(-3)),
119 /// false
120 /// );
121 /// assert_eq!((&Integer::from(102)).divisible_by(Integer::from(-3)), true);
122 /// assert_eq!(
123 /// (&Integer::from_str("-1000000000000000000000000").unwrap())
124 /// .divisible_by(Integer::from_str("1000000000000").unwrap()),
125 /// true
126 /// );
127 /// ```
128 fn divisible_by(self, other: Integer) -> bool {
129 (&self.abs).divisible_by(other.abs)
130 }
131}
132
133impl DivisibleBy<&Integer> for &Integer {
134 /// Returns whether an [`Integer`] is divisible by another [`Integer`]; in other words, whether
135 /// the first is a multiple of the second. Both [`Integer`]s are taken by reference.
136 ///
137 /// This means that zero is divisible by any [`Integer`], including zero; but a nonzero
138 /// [`Integer`] is never divisible by zero.
139 ///
140 /// It's more efficient to use this function than to compute the remainder and check whether
141 /// it's zero.
142 ///
143 /// # Worst-case complexity
144 /// $T(n) = O(n \log n \log \log n)$
145 ///
146 /// $M(n) = O(n \log n)$
147 ///
148 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
149 ///
150 /// # Examples
151 /// ```
152 /// use core::str::FromStr;
153 /// use malachite_base::num::arithmetic::traits::DivisibleBy;
154 /// use malachite_base::num::basic::traits::Zero;
155 /// use malachite_nz::integer::Integer;
156 ///
157 /// assert_eq!((&Integer::ZERO).divisible_by(&Integer::ZERO), true);
158 /// assert_eq!(
159 /// (&Integer::from(-100)).divisible_by(&Integer::from(-3)),
160 /// false
161 /// );
162 /// assert_eq!((&Integer::from(102)).divisible_by(&Integer::from(-3)), true);
163 /// assert_eq!(
164 /// (&Integer::from_str("-1000000000000000000000000").unwrap())
165 /// .divisible_by(&Integer::from_str("1000000000000").unwrap()),
166 /// true
167 /// );
168 /// ```
169 fn divisible_by(self, other: &Integer) -> bool {
170 (&self.abs).divisible_by(&other.abs)
171 }
172}