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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
// Copyright © 2024 Mikhail Hogrefe
//
// Uses code adopted from the GNU MP Library.
//
// Copyright © 2000–2002, 2005, 2009, 2014, 2017, 2018 Free Software Foundation, Inc.
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::natural::arithmetic::div_exact::{
limbs_modular_div_mod_barrett, limbs_modular_div_mod_barrett_scratch_len,
limbs_modular_div_mod_divide_and_conquer, limbs_modular_div_mod_schoolbook,
limbs_modular_invert_limb,
};
use crate::natural::arithmetic::eq_mod::limbs_mod_exact_odd_limb;
use crate::natural::arithmetic::mod_op::limbs_mod_limb;
use crate::natural::arithmetic::shr::{limbs_shr_to_out, limbs_slice_shr_in_place};
use crate::natural::InnerNatural::{Large, Small};
use crate::natural::Natural;
use crate::platform::{
Limb, BMOD_1_TO_MOD_1_THRESHOLD, DC_BDIV_QR_THRESHOLD, MU_BDIV_QR_THRESHOLD,
};
use alloc::vec::Vec;
use malachite_base::num::arithmetic::traits::{DivisibleBy, DivisibleByPowerOf2, Parity};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::logic::traits::TrailingZeros;
use malachite_base::slices::{slice_leading_zeros, slice_test_zero};
// Interpreting a slice of `Limb`s as the limbs of a `Natural` in ascending order, determines
// whether that `Natural` is divisible by a given limb.
//
// This function assumes that `ns` has at least two elements and that `d` is nonzero.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `ns.len()`.
//
// This is equivalent to `mpz_divisible_ui_p` from `mpz/divis_ui.c`, GMP 6.2.1, where `a` is
// non-negative and the `ABOVE_THRESHOLD` branch is excluded.
pub_crate_test! {limbs_divisible_by_limb(ns: &[Limb], d: Limb) -> bool {
assert!(ns.len() > 1);
if d.even() {
let twos = TrailingZeros::trailing_zeros(d);
ns[0].divisible_by_power_of_2(twos) && limbs_mod_exact_odd_limb(ns, d >> twos, 0) == 0
} else {
limbs_mod_exact_odd_limb(ns, d, 0) == 0
}
}}
fn limbs_mod_limb_helper(ns: &[Limb], d_low: Limb) -> Limb {
if ns.len() < BMOD_1_TO_MOD_1_THRESHOLD {
limbs_mod_exact_odd_limb(ns, d_low, 0)
} else {
limbs_mod_limb(ns, d_low)
}
}
// Interpreting two slices of `Limb`s, `ns` and `ds`, as the limbs (in ascending order) of two
// `Natural`s, determines whether the first `Natural` is divisible by the second. Both `Natural`s
// are taken by value.
//
// `ns` must be at least as long as `ds`, both slices must be nonempty, and the last limb of both
// must be nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `ns.len()`.
//
// # Panics
// Panics if `ns` is shorter than `ds`, `ds` is empty, or the last limbs of either slice are zero.
//
// This is equivalent to `mpn_divisible_p` from `mpn/generic/divis.c`, GMP 6.2.1, where `an >= dn`
// and neither are zero.
pub_crate_test! {
#[allow(clippy::absurd_extreme_comparisons)]
limbs_divisible_by(ns: &mut [Limb], ds: &mut [Limb]) -> bool {
let n_len = ns.len();
let d_len = ds.len();
assert_ne!(d_len, 0);
assert!(n_len >= d_len);
assert_ne!(*ns.last().unwrap(), 0);
assert_ne!(*ds.last().unwrap(), 0);
// Strip low zero limbs from ds, requiring n == 0 on those.
let offset = slice_leading_zeros(ds);
let (ns_lo, ns) = ns.split_at_mut(offset);
if !slice_test_zero(ns_lo) {
// n has fewer low zero limbs than d, so not divisible
return false;
}
let n_len = ns.len();
let ds = &mut ds[offset..];
let d_len = ds.len();
let n_0 = ns[0];
let d_0 = ds[0];
// n must have at least as many low zero bits as d
let d_mask = (d_0 & d_0.wrapping_neg()).wrapping_sub(1);
if n_0 & d_mask != 0 {
return false;
}
if d_len == 1 {
return if n_len >= BMOD_1_TO_MOD_1_THRESHOLD {
limbs_mod_limb(ns, d_0) == 0
} else {
limbs_mod_exact_odd_limb(ns, d_0 >> d_0.trailing_zeros(), 0) == 0
};
}
let trailing_zeros = TrailingZeros::trailing_zeros(d_0);
if d_len == 2 {
let d_1 = ds[1];
if d_1 <= d_mask {
let d_low = (d_0 >> trailing_zeros) | (d_1 << (Limb::WIDTH - trailing_zeros));
return limbs_mod_limb_helper(ns, d_low) == 0;
}
}
let n_len_plus_1 = n_len + 1;
let mut qs = vec![0; n_len_plus_1 - d_len];
if trailing_zeros != 0 {
assert_eq!(limbs_slice_shr_in_place(ds, trailing_zeros), 0);
assert_eq!(limbs_slice_shr_in_place(ns, trailing_zeros), 0);
}
let mut rs_vec;
let rs = if ns[n_len - 1] >= ds[d_len - 1] {
rs_vec = Vec::with_capacity(n_len_plus_1);
rs_vec.extend_from_slice(ns);
rs_vec.push(0);
&mut rs_vec
} else if n_len == d_len {
return false;
} else {
ns
};
let r_len = rs.len();
let q_len = r_len - d_len;
let rs = if d_len < DC_BDIV_QR_THRESHOLD || q_len < DC_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_schoolbook(&mut qs, rs, ds, d_inv);
&mut rs[q_len..]
} else if d_len < MU_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_divide_and_conquer(&mut qs, rs, ds, d_inv);
&mut rs[q_len..]
} else {
let mut scratch = vec![0; limbs_modular_div_mod_barrett_scratch_len(r_len, d_len)];
let ns = rs.to_vec();
limbs_modular_div_mod_barrett(&mut qs, rs, &ns, ds, &mut scratch);
&mut rs[..d_len]
};
slice_test_zero(rs)
}}
// Interpreting two slices of `Limb`s, `ns` and `ds`, as the limbs (in ascending order) of two
// `Natural`s, determines whether the first `Natural` is divisible by the second. The first
// `Natural` is taken by value, and the second by reference.
//
// `ns` must be at least as long as `ds`, both slices must be nonempty, and the last limb of both
// must be nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `ns.len()`.
//
// # Panics
// Panics if `ns` is shorter than `ds`, `ds` is empty, or the last limbs of either slice are zero.
//
// This is equivalent to `mpn_divisible_p` from `mpn/generic/divis.c`, GMP 6.2.1, where `an >= dn`
// and neither are zero.
pub_crate_test! {
#[allow(clippy::absurd_extreme_comparisons)]
limbs_divisible_by_val_ref(ns: &mut [Limb], ds: &[Limb]) -> bool {
let n_len = ns.len();
let d_len = ds.len();
assert_ne!(d_len, 0);
assert!(n_len >= d_len);
assert_ne!(*ns.last().unwrap(), 0);
assert_ne!(*ds.last().unwrap(), 0);
// Strip low zero limbs from ds, requiring n == 0 on those.
let offset = slice_leading_zeros(ds);
let (ns_lo, ns) = ns.split_at_mut(offset);
if !slice_test_zero(ns_lo) {
// n has fewer low zero limbs than d, so not divisible
return false;
}
let n_len = ns.len();
let mut scratch;
let mut ds = &ds[offset..];
let d_len = ds.len();
let n_0 = ns[0];
let d_0 = ds[0];
// n must have at least as many low zero bits as d
let d_mask = (d_0 & d_0.wrapping_neg()).wrapping_sub(1);
if n_0 & d_mask != 0 {
return false;
}
if d_len == 1 {
return if n_len >= BMOD_1_TO_MOD_1_THRESHOLD {
limbs_mod_limb(ns, d_0) == 0
} else {
limbs_mod_exact_odd_limb(ns, d_0 >> d_0.trailing_zeros(), 0) == 0
};
}
let trailing_zeros = TrailingZeros::trailing_zeros(d_0);
if d_len == 2 {
let d_1 = ds[1];
if d_1 <= d_mask {
let d_low = (d_0 >> trailing_zeros) | (d_1 << (Limb::WIDTH - trailing_zeros));
return limbs_mod_limb_helper(ns, d_low) == 0;
}
}
let n_len_plus_1 = n_len + 1;
let mut qs = vec![0; n_len_plus_1 - d_len];
if trailing_zeros != 0 {
scratch = vec![0; d_len];
assert_eq!(limbs_shr_to_out(&mut scratch, ds, trailing_zeros), 0);
ds = &scratch;
assert_eq!(limbs_slice_shr_in_place(ns, trailing_zeros), 0);
}
let mut rs_vec;
let rs = if ns[n_len - 1] >= ds[d_len - 1] {
rs_vec = Vec::with_capacity(n_len_plus_1);
rs_vec.extend_from_slice(ns);
rs_vec.push(0);
&mut rs_vec
} else if n_len == d_len {
return false;
} else {
ns
};
let r_len = rs.len();
let q_len = r_len - d_len;
let rs = if d_len < DC_BDIV_QR_THRESHOLD || q_len < DC_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_schoolbook(&mut qs, rs, ds, d_inv);
&mut rs[q_len..]
} else if d_len < MU_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_divide_and_conquer(&mut qs, rs, ds, d_inv);
&mut rs[q_len..]
} else {
let mut scratch = vec![0; limbs_modular_div_mod_barrett_scratch_len(r_len, d_len)];
let ns = rs.to_vec();
limbs_modular_div_mod_barrett(&mut qs, rs, &ns, ds, &mut scratch);
&mut rs[..d_len]
};
slice_test_zero(rs)
}}
// Interpreting two slices of `Limb`s, `ns` and `ds`, as the limbs (in ascending order) of two
// `Natural`s, determines whether the first `Natural` is divisible by the second. The first
// `Natural` is taken by reference, and the second by value.
//
// `ns` must be at least as long as `ds`, both slices must be nonempty, and the last limb of both
// must be nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `ns.len()`.
//
// # Panics
// Panics if `ns` is shorter than `ds`, `ds` is empty, or the last limbs of either slice are zero.
//
// This is equivalent to `mpn_divisible_p` from `mpn/generic/divis.c`, GMP 6.2.1, where `an >= dn`
// and neither are zero.
pub_test! {
#[allow(clippy::absurd_extreme_comparisons)]
limbs_divisible_by_ref_val(ns: &[Limb], ds: &mut [Limb]) -> bool {
let n_len = ns.len();
let d_len = ds.len();
assert_ne!(d_len, 0);
assert!(n_len >= d_len);
assert_ne!(*ns.last().unwrap(), 0);
assert_ne!(*ds.last().unwrap(), 0);
// Strip low zero limbs from ds, requiring n == 0 on those.
let offset = slice_leading_zeros(ds);
let (ns_lo, ns) = ns.split_at(offset);
if !slice_test_zero(ns_lo) {
// n has fewer low zero limbs than d, so not divisible
return false;
}
let n_len = ns.len();
let ds = &mut ds[offset..];
let d_len = ds.len();
let n_0 = ns[0];
let d_0 = ds[0];
// n must have at least as many low zero bits as d
let d_mask = (d_0 & d_0.wrapping_neg()).wrapping_sub(1);
if n_0 & d_mask != 0 {
return false;
}
if d_len == 1 {
return if n_len >= BMOD_1_TO_MOD_1_THRESHOLD {
limbs_mod_limb(ns, d_0) == 0
} else {
limbs_mod_exact_odd_limb(ns, d_0 >> d_0.trailing_zeros(), 0) == 0
};
}
let trailing_zeros = TrailingZeros::trailing_zeros(d_0);
if d_len == 2 {
let d_1 = ds[1];
if d_1 <= d_mask {
let d_low = (d_0 >> trailing_zeros) | (d_1 << (Limb::WIDTH - trailing_zeros));
return limbs_mod_limb_helper(ns, d_low) == 0;
}
}
let n_len_plus_1 = n_len + 1;
let mut rs_qs = vec![0; (n_len_plus_1 << 1) - d_len];
let (rs, qs) = rs_qs.split_at_mut(n_len_plus_1);
if trailing_zeros != 0 {
assert_eq!(limbs_slice_shr_in_place(ds, trailing_zeros), 0);
assert_eq!(limbs_shr_to_out(rs, ns, trailing_zeros), 0);
} else {
rs[..n_len].copy_from_slice(ns);
}
let r_len = if rs[n_len - 1] >= ds[d_len - 1] {
n_len_plus_1
} else if n_len == d_len {
return false;
} else {
n_len
};
let rs = &mut rs[..r_len];
let q_len = r_len - d_len;
let rs = if d_len < DC_BDIV_QR_THRESHOLD || q_len < DC_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_schoolbook(qs, rs, ds, d_inv);
&mut rs[q_len..]
} else if d_len < MU_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_divide_and_conquer(qs, rs, ds, d_inv);
&mut rs[q_len..]
} else {
let mut scratch = vec![0; limbs_modular_div_mod_barrett_scratch_len(r_len, d_len)];
let ns = rs.to_vec();
limbs_modular_div_mod_barrett(qs, rs, &ns, ds, &mut scratch);
&mut rs[..d_len]
};
slice_test_zero(rs)
}}
// Interpreting two slices of `Limb`s, `ns` and `ds`, as the limbs (in ascending order) of two
// `Natural`s, determines whether the first `Natural` is divisible by the second. Both `Natural`s
// are taken by reference.
//
// `ns` must be at least as long as `ds`, both slices must be nonempty, and the last limb of both
// must be nonzero.
//
// # Worst-case complexity
// $T(n) = O(n \log n \log \log n)$
//
// $M(n) = O(n \log n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `ns.len()`.
//
// # Panics
// Panics if `ns` is shorter than `ds`, `ds` is empty, or the last limbs of either slice are zero.
//
// This is equivalent to `mpn_divisible_p` from `mpn/generic/divis.c`, GMP 6.2.1, where `an >= dn`
// and neither are zero.
pub_test! {
#[allow(clippy::absurd_extreme_comparisons)]
limbs_divisible_by_ref_ref(ns: &[Limb], ds: &[Limb]) -> bool {
let n_len = ns.len();
let d_len = ds.len();
assert_ne!(d_len, 0);
assert!(n_len >= d_len);
assert_ne!(*ns.last().unwrap(), 0);
assert_ne!(*ds.last().unwrap(), 0);
// Strip low zero limbs from ds, requiring n == 0 on those.
let offset = slice_leading_zeros(ds);
let (ns_lo, ns) = ns.split_at(offset);
if !slice_test_zero(ns_lo) {
// n has fewer low zero limbs than d, so not divisible
return false;
}
let n_len = ns.len();
let mut scratch;
let mut ds = &ds[offset..];
let d_len = ds.len();
let d_0 = ds[0];
// n must have at least as many low zero bits as d
let d_mask = (d_0 & d_0.wrapping_neg()).wrapping_sub(1);
if ns[0] & d_mask != 0 {
return false;
}
if d_len == 1 {
return if n_len >= BMOD_1_TO_MOD_1_THRESHOLD {
limbs_mod_limb(ns, d_0) == 0
} else {
limbs_mod_exact_odd_limb(ns, d_0 >> d_0.trailing_zeros(), 0) == 0
};
}
let trailing_zeros = TrailingZeros::trailing_zeros(d_0);
if d_len == 2 {
let d_1 = ds[1];
if d_1 <= d_mask {
let d_low = (d_0 >> trailing_zeros) | (d_1 << (Limb::WIDTH - trailing_zeros));
return limbs_mod_limb_helper(ns, d_low) == 0;
}
}
let n_len_plus_1 = n_len + 1;
let mut rs_qs = vec![0; (n_len_plus_1 << 1) - d_len];
let (rs, qs) = rs_qs.split_at_mut(n_len_plus_1);
if trailing_zeros != 0 {
scratch = vec![0; d_len];
assert_eq!(limbs_shr_to_out(&mut scratch, ds, trailing_zeros), 0);
ds = &scratch;
assert_eq!(limbs_shr_to_out(rs, ns, trailing_zeros), 0);
} else {
rs[..n_len].copy_from_slice(ns);
}
let r_len = if rs[n_len - 1] >= ds[d_len - 1] {
n_len_plus_1
} else if n_len == d_len {
return false;
} else {
n_len
};
let rs = &mut rs[..r_len];
let q_len = r_len - d_len;
let rs = if d_len < DC_BDIV_QR_THRESHOLD || q_len < DC_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_schoolbook(qs, rs, ds, d_inv);
&mut rs[q_len..]
} else if d_len < MU_BDIV_QR_THRESHOLD {
let d_inv = limbs_modular_invert_limb(ds[0]).wrapping_neg();
limbs_modular_div_mod_divide_and_conquer(qs, rs, ds, d_inv);
&mut rs[q_len..]
} else {
let mut scratch = vec![0; limbs_modular_div_mod_barrett_scratch_len(r_len, d_len)];
let ns = rs.to_vec();
limbs_modular_div_mod_barrett(qs, rs, &ns, ds, &mut scratch);
&mut rs[..d_len]
};
slice_test_zero(rs)
}}
impl Natural {
fn divisible_by_limb(&self, other: Limb) -> bool {
match (self, other) {
(&Natural::ZERO, _) => true,
(_, 0) => false,
(&Natural(Small(small)), y) => small.divisible_by(y),
(&Natural(Large(ref limbs)), y) => limbs_divisible_by_limb(limbs, y),
}
}
// Tests whether other is divisible by self
fn limb_divisible_by_natural(&self, other: Limb) -> bool {
match (other, self) {
(0, _) => true,
(_, &Natural::ZERO) | (_, &Natural(Large(_))) => false,
(x, &Natural(Small(small))) => x.divisible_by(small),
}
}
}
impl DivisibleBy<Natural> for Natural {
/// Returns whether a [`Natural`] is divisible by another [`Natural`]; in other words, whether
/// the first is a multiple of the second. Both [`Natural`]s are taken by value.
///
/// This means that zero is divisible by any [`Natural`], including zero; but a nonzero
/// [`Natural`] is never divisible by zero.
///
/// It's more efficient to use this function than to compute the remainder and check whether
/// it's zero.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::DivisibleBy;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(Natural::ZERO.divisible_by(Natural::ZERO), true);
/// assert_eq!(Natural::from(100u32).divisible_by(Natural::from(3u32)), false);
/// assert_eq!(Natural::from(102u32).divisible_by(Natural::from(3u32)), true);
/// assert_eq!(
/// Natural::from_str("1000000000000000000000000").unwrap()
/// .divisible_by(Natural::from_str("1000000000000").unwrap()),
/// true
/// );
/// ```
fn divisible_by(mut self, mut other: Natural) -> bool {
match (&mut self, &mut other) {
(x, &mut Natural(Small(y))) => x.divisible_by_limb(y),
(&mut Natural(Small(x)), y) => y.limb_divisible_by_natural(x),
(Natural(Large(ref mut xs)), Natural(Large(ref mut ys))) => {
xs.len() >= ys.len() && limbs_divisible_by(xs, ys)
}
}
}
}
impl<'a> DivisibleBy<&'a Natural> for Natural {
/// Returns whether a [`Natural`] is divisible by another [`Natural`]; in other words, whether
/// the first is a multiple of the second. The first [`Natural`]s is taken by reference and the
/// second by value.
///
/// This means that zero is divisible by any [`Natural`], including zero; but a nonzero
/// [`Natural`] is never divisible by zero.
///
/// It's more efficient to use this function than to compute the remainder and check whether
/// it's zero.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::DivisibleBy;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!(Natural::ZERO.divisible_by(&Natural::ZERO), true);
/// assert_eq!(Natural::from(100u32).divisible_by(&Natural::from(3u32)), false);
/// assert_eq!(Natural::from(102u32).divisible_by(&Natural::from(3u32)), true);
/// assert_eq!(
/// Natural::from_str("1000000000000000000000000").unwrap()
/// .divisible_by(&Natural::from_str("1000000000000").unwrap()),
/// true
/// );
/// ```
fn divisible_by(mut self, other: &'a Natural) -> bool {
match (&mut self, other) {
(x, &Natural(Small(y))) => x.divisible_by_limb(y),
(&mut Natural(Small(x)), y) => y.limb_divisible_by_natural(x),
(Natural(Large(ref mut xs)), &Natural(Large(ref ys))) => {
xs.len() >= ys.len() && limbs_divisible_by_val_ref(xs, ys)
}
}
}
}
impl<'a> DivisibleBy<Natural> for &'a Natural {
/// Returns whether a [`Natural`] is divisible by another [`Natural`]; in other words, whether
/// the first is a multiple of the second. The first [`Natural`]s are taken by reference and the
/// second by value.
///
/// This means that zero is divisible by any [`Natural`], including zero; but a nonzero
/// [`Natural`] is never divisible by zero.
///
/// It's more efficient to use this function than to compute the remainder and check whether
/// it's zero.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::DivisibleBy;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!((&Natural::ZERO).divisible_by(Natural::ZERO), true);
/// assert_eq!((&Natural::from(100u32)).divisible_by(Natural::from(3u32)), false);
/// assert_eq!((&Natural::from(102u32)).divisible_by(Natural::from(3u32)), true);
/// assert_eq!(
/// (&Natural::from_str("1000000000000000000000000").unwrap())
/// .divisible_by(Natural::from_str("1000000000000").unwrap()),
/// true
/// );
/// ```
fn divisible_by(self, mut other: Natural) -> bool {
match (self, &mut other) {
(x, &mut Natural(Small(y))) => x.divisible_by_limb(y),
(&Natural(Small(x)), y) => y.limb_divisible_by_natural(x),
(&Natural(Large(ref xs)), Natural(Large(ref mut ys))) => {
xs.len() >= ys.len() && limbs_divisible_by_ref_val(xs, ys)
}
}
}
}
impl<'a, 'b> DivisibleBy<&'b Natural> for &'a Natural {
/// Returns whether a [`Natural`] is divisible by another [`Natural`]; in other words, whether
/// the first is a multiple of the second. Both [`Natural`]s are taken by reference.
///
/// This means that zero is divisible by any [`Natural`], including zero; but a nonzero
/// [`Natural`] is never divisible by zero.
///
/// It's more efficient to use this function than to compute the remainder and check whether
/// it's zero.
///
/// # Worst-case complexity
/// $T(n) = O(n \log n \log \log n)$
///
/// $M(n) = O(n \log n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::arithmetic::traits::DivisibleBy;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::natural::Natural;
/// use core::str::FromStr;
///
/// assert_eq!((&Natural::ZERO).divisible_by(&Natural::ZERO), true);
/// assert_eq!((&Natural::from(100u32)).divisible_by(&Natural::from(3u32)), false);
/// assert_eq!((&Natural::from(102u32)).divisible_by(&Natural::from(3u32)), true);
/// assert_eq!(
/// (&Natural::from_str("1000000000000000000000000").unwrap())
/// .divisible_by(&Natural::from_str("1000000000000").unwrap()),
/// true
/// );
/// ```
fn divisible_by(self, other: &'b Natural) -> bool {
match (self, other) {
(x, &Natural(Small(y))) => x.divisible_by_limb(y),
(&Natural(Small(x)), y) => y.limb_divisible_by_natural(x),
(&Natural(Large(ref xs)), &Natural(Large(ref ys))) => {
xs.len() >= ys.len() && limbs_divisible_by_ref_ref(xs, ys)
}
}
}
}