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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// numera::number::integer::z::ops::sqrt
//
//! Implement the square root operations.
//
use crate::number::{integer::*, traits::Sign};
use devela::paste;
macro_rules! impl_integer_sub {
// impl square root ops for multiple integer types
//
// # Args
// $a: integer base abbreviation. e.g. Z
// $t: integer base name. e.g. Integer
// $p: inner primitive base name. e.g. i
// $b: integer and primitive bitsize. e.g. 8
( $($a:ident | $t:ident + $p:ident + $b:literal, cast: $bcast:literal);+ ) => {
$(
impl_integer_sub![sqrt: $a | $t + $p + $b];
)+
};
// square root operations
//
// impl variants:
// - sqrt_floor
// - sqrt_ceil
// - sqrt_round
(sqrt: $a: ident | $t:ident + $p:ident + $b:literal) => { paste! {
/// # Square root
impl [<$t$b>] {
/// Returns `true` if self is a perfect square,
/// meaning the square root of self is an integer.
///
/// Returns `false` otherwise, which includes all negative values.
///
/// # Algorithm
/// $$
/// \text{is\textunderscore square}(n) = \begin{cases}
/// \text{true} & \text{if } \left(\lfloor \sqrt{n} \rfloor\right)^2 = n \cr
/// \text{false} & \text{if } \left(\lfloor \sqrt{n} \rfloor\right)^2 \neq n
/// \end{cases}
/// $$
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).is_square(), false];"]
#[doc="assert_eq!["[<$t$b>]"(13).is_square(), false];"]
#[doc="assert_eq!["[<$t$b>]"(16).is_square(), true];"]
#[doc="assert_eq!["[<$t$b>]"(20).is_square(), false];"]
#[doc="assert_eq!["[<$t$b>]"(21).is_square(), false];"]
/// ```
#[inline]
#[must_use]
pub fn is_square(self) -> bool {
if let Some(sqrt) = self.sqrt_floor() {
sqrt * sqrt == self
} else {
false
}
}
/// Returns the floored integer square root.
///
/// Returns `None` if self is negative.
///
/// # Algorithm
/// $$ \left\lfloor \sqrt{x} \right\rfloor = n_{k} $$
///
/// Where $n_{k}$ is the result of a sequence of estimates that
/// starts with an initial $n_{0} = x/2$ which is updated using
/// [*Heron's method*](
/// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Heron's_method):
///
/// $$
/// n_{i+1} = n_{i} - ( n_{i}^{2} - x) / 2n_{i},
/// \quad \small\text{for} \quad i = 0, 1, \ldots, k,
/// $$
///
/// Where $n_{i}$ is the current estimate, $n_{i+1}$ is the next
/// estimate, $x$ is self, and $k$ is the number of iterations
/// needed to converge to a solution, on the order of the number of
/// bits of self, about $O(\log_2 b)$, which for e.g. 128 bits would
/// be $ ±7 $ iterations.
///
/// Hence, the function continues updating the estimate until
/// reaching $n_{k}$, which provides the largest integer less than
/// or equal to the square root of `x`.
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).sqrt_floor(), Some("[<$t$b>]"(3))];"]
#[doc="assert_eq!["[<$t$b>]"(13).sqrt_floor(), Some("[<$t$b>]"(3))];"]
#[doc="assert_eq!["[<$t$b>]"(16).sqrt_floor(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(20).sqrt_floor(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(21).sqrt_floor(), Some("[<$t$b>]"(4))];"]
/// ```
#[inline]
#[must_use]
pub fn sqrt_floor(self) -> Option<[<$t$b>]> {
const _2: [<$t$b>] = [<$t$b>](2);
if self.is_negative() {
None
} else if self < _2 {
Some(self)
} else {
let mut x = self;
let mut y = (x + self / x) / _2;
while y < x {
x = y;
y = (x + self / x) / _2;
}
Some(x)
}
}
/// Returns the ceiled integer square root.
///
/// Returns `None` if self is negative.
///
/// # Algorithm
/// $$
/// \begin{align}
/// \notag \left\lceil \sqrt{x} \thinspace\right\rceil = \begin{cases}
/// n & \text{if } n^2 = x \cr
/// n+1 & \text{if } n^2 < x \end{cases} \cr
/// \notag \small\text{where } n = \lfloor \sqrt{x} \rfloor &
/// \end{align}
/// $$
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).sqrt_ceil(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(13).sqrt_ceil(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(16).sqrt_ceil(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(20).sqrt_ceil(), Some("[<$t$b>]"(5))];"]
#[doc="assert_eq!["[<$t$b>]"(21).sqrt_ceil(), Some("[<$t$b>]"(5))];"]
/// ```
#[inline]
#[must_use]
pub fn sqrt_ceil(self) -> Option<[<$t$b>]> {
const _1: [<$t$b>] = [<$t$b>](1);
if let Some(floor) = self.sqrt_floor() {
if floor * floor == self {
Some(floor)
} else {
Some(floor + _1)
}
} else {
None
}
}
/// Returns the rounded integer square root.
///
/// Returns `None` if self is negative.
///
/// # Algorithm
/// $$
/// \begin{align}
/// \notag \left\lfloor\sqrt{x} \thinspace\right\rceil = \begin{cases}
/// n & \text{if } x - n^2 < (n+1)^2 - x \cr
/// n+1 & \text{if } x - n^2 \geq (n+1)^2 - x \end{cases} \cr
/// \notag \small\text{where } n = \lfloor \sqrt{x} \rfloor &
/// \end{align}
/// $$
///
/// [`sqrt_floor`]: #method.sqrt_floor
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).sqrt_round(), Some("[<$t$b>]"(3))];"]
#[doc="assert_eq!["[<$t$b>]"(13).sqrt_round(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(16).sqrt_round(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(20).sqrt_round(), Some("[<$t$b>]"(4))];"]
#[doc="assert_eq!["[<$t$b>]"(21).sqrt_round(), Some("[<$t$b>]"(5))];"]
/// ```
#[inline]
#[must_use]
pub fn sqrt_round(self) -> Option<[<$t$b>]> {
const _1: [<$t$b>] = [<$t$b>](1);
const _2: [<$t$b>] = [<$t$b>](2);
if self.is_negative() {
None
} else if self < _2 {
Some(self)
} else {
// sqrt_floor
let mut x = self;
let mut y = (x + self / x) / _2;
while y < x {
x = y;
y = (x + self / x) / _2;
}
// do we have to round up?
if self - x * x >= (x + _1) * (x + _1) - self {
Some(x + _1)
} else {
Some(x)
}
}
}
/// Returns the checked floored integer square root, as a tuple containing
/// [`sqrt_floor`](#method.is_floor) and [`is_square`](#method.is_square).
///
/// Returns `None` if self is negative.
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).checked_sqrt_floor(), Some(("[<$t$b>]"(3), false))];"]
#[doc="assert_eq!["[<$t$b>]"(13).checked_sqrt_floor(), Some(("[<$t$b>]"(3), false))];"]
#[doc="assert_eq!["[<$t$b>]"(16).checked_sqrt_floor(), Some(("[<$t$b>]"(4), true))];"]
#[doc="assert_eq!["[<$t$b>]"(20).checked_sqrt_floor(), Some(("[<$t$b>]"(4), false))];"]
#[doc="assert_eq!["[<$t$b>]"(21).checked_sqrt_floor(), Some(("[<$t$b>]"(4), false))];"]
/// ```
#[inline]
#[must_use]
pub fn checked_sqrt_floor(self) -> Option<([<$t$b>], bool)> {
const _2: [<$t$b>] = [<$t$b>](2);
if self.is_negative() {
None
} else if self < _2 {
Some((self, true))
} else {
let mut x = self;
let mut y = (x + self / x) / _2;
while y < x {
x = y;
y = (x + self / x) / _2;
}
Some((x, x * x == self))
}
}
/// Returns the checked ceiled integer square root, as a tuple containing
/// [`sqrt_ceil`](#method.is_ceil) and [`is_square`](#method.is_square).
///
/// Returns `None` if self is negative.
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).checked_sqrt_ceil(), Some(("[<$t$b>]"(4), false))];"]
#[doc="assert_eq!["[<$t$b>]"(13).checked_sqrt_ceil(), Some(("[<$t$b>]"(4), false))];"]
#[doc="assert_eq!["[<$t$b>]"(16).checked_sqrt_ceil(), Some(("[<$t$b>]"(4), true))];"]
#[doc="assert_eq!["[<$t$b>]"(20).checked_sqrt_ceil(), Some(("[<$t$b>]"(5), false))];"]
#[doc="assert_eq!["[<$t$b>]"(21).checked_sqrt_ceil(), Some(("[<$t$b>]"(5), false))];"]
/// ```
#[inline]
#[must_use]
pub fn checked_sqrt_ceil(self) -> Option<([<$t$b>], bool)> {
const _1: [<$t$b>] = [<$t$b>](1);
if let Some(floor) = self.sqrt_floor() {
if floor * floor == self {
Some((floor, true))
} else {
Some((floor + _1, false))
}
} else {
None
}
}
/// Returns the checked rounded integer square root, as a tuple containing
/// [`sqrt_round`](#method.sqrt_round) and [`is_square`](#method.is_square).
///
/// Returns `None` if self is negative.
///
/// # Examples
/// ```
#[doc="use numera::all::" [<$t$b>] ";"]
///
#[doc="assert_eq!["[<$t$b>]"(12).checked_sqrt_round(), Some(("[<$t$b>]"(3), false))];"]
#[doc="assert_eq!["[<$t$b>]"(13).checked_sqrt_round(), Some(("[<$t$b>]"(4), false))];"]
#[doc="assert_eq!["[<$t$b>]"(16).checked_sqrt_round(), Some(("[<$t$b>]"(4), true))];"]
#[doc="assert_eq!["[<$t$b>]"(20).checked_sqrt_round(), Some(("[<$t$b>]"(4), false))];"]
#[doc="assert_eq!["[<$t$b>]"(21).checked_sqrt_round(), Some(("[<$t$b>]"(5), false))];"]
/// ```
#[inline]
#[must_use]
pub fn checked_sqrt_round(self) -> Option<([<$t$b>], bool)> {
const _1: [<$t$b>] = [<$t$b>](1);
const _2: [<$t$b>] = [<$t$b>](2);
if self.is_negative() {
None
} else if self < _2 {
Some((self, true))
} else {
let mut x = self;
let mut y = (x + self / x) / _2;
while y < x {
x = y;
y = (x + self / x) / _2;
}
let square = x * x;
if self - square >= (x + _1) * (x + _1) - self {
Some((x + _1, false))
} else {
Some((x, square == self))
}
}
}
}
}};
}
impl_integer_sub![
Z|Integer+i+8, cast:16;
Z|Integer+i+16, cast:32;
Z|Integer+i+32, cast:64;
Z|Integer+i+64, cast:128;
Z|Integer+i+128, cast:128
];