malachite_base/num/factorization/is_prime.rs
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 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
// Copyright © 2025 Mikhail Hogrefe
//
// Uses code adopted from the FLINT Library.
//
// Copyright © 2008 Peter Shrimpton
//
// Copyright © 2009 Tom Boothby
//
// Copyright © 2009, 2010, 2013, 2015, 2016 William Hart
//
// Copyright © 2010 Fredrik Johansson
//
// Copyright © 2014 Dana Jacobsen
//
// Copyright © 2023 Mathieu Gouttenoire
//
// 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::num::arithmetic::mod_pow::mul_mod_helper;
use crate::num::arithmetic::traits::{
Gcd, JacobiSymbol, ModAdd, ModInverse, ModMulPrecomputed, ModMulPrecomputedAssign, ModSub,
Parity, PowerOf2, WrappingAddAssign, WrappingNegAssign, XMulYToZZ, XXAddYYToZZ,
};
use crate::num::basic::integers::{PrimitiveInt, USIZE_IS_U32};
use crate::num::comparison::traits::PartialOrdAbs;
use crate::num::conversion::traits::WrappingFrom;
use crate::num::factorization::traits::IsPrime;
use crate::num::logic::traits::{BitAccess, LeadingZeros, SignificantBits, TrailingZeros};
// This is FLINT_ODD_PRIME_LOOKUP when FLINT64 is true, from ulong_extras/is_oddprime.c, FLINT
// 3.1.2.
const ODD_PRIME_LOOKUP_U64: [u64; 32] = [
0x816d129a64b4cb6e,
0x2196820d864a4c32,
0xa48961205a0434c9,
0x4a2882d129861144,
0x834992132424030,
0x148a48844225064b,
0xb40b4086c304205,
0x65048928125108a0,
0x80124496804c3098,
0xc02104c941124221,
0x804490000982d32,
0x220825b082689681,
0x9004265940a28948,
0x6900924430434006,
0x12410da408088210,
0x86122d22400c060,
0x110d301821b0484,
0x14916022c044a002,
0x92094d204a6400c,
0x4ca2100800522094,
0xa48b081051018200,
0x34c108144309a25,
0x2084490880522502,
0x241140a218003250,
0xa41a00101840128,
0x2926000836004512,
0x10100480c0618283,
0xc20c26584822006d,
0x4520582024894810,
0x10c0250219002488,
0x802832ca01140868,
0x60901300264b0400,
];
// This is FLINT_ODD_PRIME_LOOKUP when FLINT64 is false, from ulong_extras/is_oddprime.c, FLINT
// 3.1.2.
const ODD_PRIME_LOOKUP_U32: [u32; 64] = [
0x64b4cb6e, 0x816d129a, 0x864a4c32, 0x2196820d, 0x5a0434c9, 0xa4896120, 0x29861144, 0x4a2882d1,
0x32424030, 0x8349921, 0x4225064b, 0x148a4884, 0x6c304205, 0xb40b408, 0x125108a0, 0x65048928,
0x804c3098, 0x80124496, 0x41124221, 0xc02104c9, 0x982d32, 0x8044900, 0x82689681, 0x220825b0,
0x40a28948, 0x90042659, 0x30434006, 0x69009244, 0x8088210, 0x12410da4, 0x2400c060, 0x86122d2,
0x821b0484, 0x110d301, 0xc044a002, 0x14916022, 0x4a6400c, 0x92094d2, 0x522094, 0x4ca21008,
0x51018200, 0xa48b0810, 0x44309a25, 0x34c1081, 0x80522502, 0x20844908, 0x18003250, 0x241140a2,
0x1840128, 0xa41a001, 0x36004512, 0x29260008, 0xc0618283, 0x10100480, 0x4822006d, 0xc20c2658,
0x24894810, 0x45205820, 0x19002488, 0x10c02502, 0x1140868, 0x802832ca, 0x264b0400, 0x60901300,
];
// This is FLINT_D_BITS when FLINT64 is true, from flint.h, FLINT 3.1.2.
const FLINT_D_BITS: u64 = 53;
// This is n_is_oddprime_small_u64 when FLINT64 is true, from ulong_extras/is_oddprime.c, FLINT
// 3.1.2.
#[inline]
fn is_odd_prime_small_u64(n: u64) -> bool {
ODD_PRIME_LOOKUP_U64[(n >> 7) as usize].get_bit((n >> 1) & u64::WIDTH_MASK)
}
// This is n_is_oddprime_small_u64 when FLINT64 is false, from ulong_extras/is_oddprime.c, FLINT
// 3.1.2.
#[inline]
fn is_odd_prime_small_u32(n: u32) -> bool {
ODD_PRIME_LOOKUP_U32[(n >> 6) as usize].get_bit(u64::from(n >> 1) & u32::WIDTH_MASK)
}
// This is n_mod2_preinv when FLINT64 is false, from ulong_extras/mod2_preinv.c, FLINT 3.1.2.
fn mod_preinverted_u32(a: u32, mut n: u32, inverse: u32) -> u32 {
assert_ne!(n, 0);
let norm = LeadingZeros::leading_zeros(n);
n <<= norm;
let u1 = a >> (u32::WIDTH - norm);
let u0 = a << norm;
let (mut q1, mut q0) = u32::x_mul_y_to_zz(inverse, u1);
(q1, q0) = u32::xx_add_yy_to_zz(q1, q0, u1, u0);
let mut r = u0 - (q1 + 1) * n;
if r > q0 {
r += n;
}
if r < n {
r >> norm
} else {
(r - n) >> norm
}
}
// This is n_mod2_preinv when FLINT64 is true, from ulong_extras/mod2_preinv.c, FLINT 3.1.2.
fn mod_preinverted_u64(a: u64, mut n: u64, inverse: u64) -> u64 {
assert_ne!(n, 0);
let norm = LeadingZeros::leading_zeros(n);
n <<= norm;
let u1 = a >> (u64::WIDTH - norm);
let u0 = a << norm;
let (mut q1, mut q0) = u64::x_mul_y_to_zz(inverse, u1);
(q1, q0) = u64::xx_add_yy_to_zz(q1, q0, u1, u0);
let mut r = u0 - (q1 + 1) * n;
if r > q0 {
r += n;
}
if r < n {
r >> norm
} else {
(r - n) >> norm
}
}
// This is n_powmod2_ui_preinv when FLINT64 is false, from ulong_extras/powmod2_ui_preinv.c, FLINT
// 3.1.2.
fn mod_pow_preinverted_u32(mut a: u32, mut exp: u32, mut n: u32, inverse: u32) -> u32 {
assert_ne!(n, 0);
if exp == 0 {
// anything modulo 1 is 0
return u32::from(n != 1);
}
if a == 0 {
return 0;
}
if a >= n {
a = mod_preinverted_u32(a, n, inverse);
}
let norm = LeadingZeros::leading_zeros(n);
a <<= norm;
n <<= norm;
while exp.even() {
a = mul_mod_helper::<u32, u64>(a, a, n, inverse, norm);
exp >>= 1;
}
let mut x = a;
loop {
exp >>= 1;
if exp == 0 {
break;
}
a = mul_mod_helper::<u32, u64>(a, a, n, inverse, norm);
if exp.odd() {
x = mul_mod_helper::<u32, u64>(x, a, n, inverse, norm);
}
}
x >> norm
}
// This is n_powmod2_ui_preinv when FLINT64 is true, from ulong_extras/powmod2_ui_preinv.c, FLINT
// 3.1.2.
fn mod_pow_preinverted_u64(mut a: u64, mut exp: u64, mut n: u64, inverse: u64) -> u64 {
assert_ne!(n, 0);
if exp == 0 {
// anything modulo 1 is 0
return u64::from(n != 1);
}
if a == 0 {
return 0;
}
if a >= n {
a = mod_preinverted_u64(a, n, inverse);
}
let norm = LeadingZeros::leading_zeros(n);
a <<= norm;
n <<= norm;
while exp.even() {
a = mul_mod_helper::<u64, u128>(a, a, n, inverse, norm);
exp >>= 1;
}
let mut x = a;
loop {
exp >>= 1;
if exp == 0 {
break;
}
a = mul_mod_helper::<u64, u128>(a, a, n, inverse, norm);
if exp.odd() {
x = mul_mod_helper::<u64, u128>(x, a, n, inverse, norm);
}
}
x >> norm
}
// This is n_mulmod_precomp when FLINT64 is true, from ulong_extras/mulmod_precomp.c, FLINT 3.1.2.
fn mod_mul_preinverted_float(a: u64, b: u64, n: u64, inverse: f64) -> u64 {
let q = ((a as f64) * (b as f64) * inverse) as u64;
let mut r = (a.wrapping_mul(b)).wrapping_sub(q.wrapping_mul(n));
if r.get_highest_bit() {
r.wrapping_add_assign(n);
if r.get_highest_bit() {
return r.wrapping_add(n);
}
} else if r >= n {
return r - n;
}
r
}
// This is n_powmod_ui_precomp when FLINT64 is true, from ulong_extras/powmod_precomp.c, FLINT
// 3.1.2.
fn mod_pow_preinverted_float(a: u64, mut exp: u64, n: u64, inverse: f64) -> u64 {
if n == 1 {
return 0;
}
let mut x = 1;
let mut y = a;
while exp != 0 {
if exp.odd() {
x = mod_mul_preinverted_float(x, y, n, inverse);
}
exp >>= 1;
if exp != 0 {
y = mod_mul_preinverted_float(y, y, n, inverse);
}
}
x
}
// This is n_is_probabprime_fermat when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT
// 3.1.2.
fn is_probable_prime_fermat(n: u64, i: u64) -> bool {
(if n.significant_bits() <= FLINT_D_BITS {
mod_pow_preinverted_float(i, n - 1, n, 1.0 / (n as f64))
} else {
mod_pow_preinverted_u64(i, n - 1, n, u64::precompute_mod_mul_data(&n))
}) == 1
}
// This is fchain_precomp when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT 3.1.2.
fn fibonacci_chain_precomputed(m: u64, n: u64, inverse: f64) -> (u64, u64) {
let mut x = 2;
let mut y = n - 3;
let mut power = u64::power_of_2(m.significant_bits() - 1);
while power != 0 {
let xy = mod_mul_preinverted_float(x, y, n, inverse).mod_add(3, n);
(x, y) = if m & power != 0 {
(
xy,
mod_mul_preinverted_float(y, y, n, inverse).mod_sub(2, n),
)
} else {
(
mod_mul_preinverted_float(x, x, n, inverse).mod_sub(2, n),
xy,
)
};
power >>= 1;
}
(x, y)
}
// This is fchain2_preinv when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT 3.1.2.
fn fibonacci_chain_preinvert(m: u64, n: u64, ninv: u64) -> (u64, u64) {
let mut x = 2;
let mut y = n - 3;
let mut power = u64::power_of_2(m.significant_bits() - 1);
while power != 0 {
let xy = x.mod_mul_precomputed(y, n, &ninv).mod_add(3, n);
(x, y) = if m & power != 0 {
(xy, y.mod_mul_precomputed(y, n, &ninv).mod_sub(2, n))
} else {
(x.mod_mul_precomputed(x, n, &ninv).mod_sub(2, n), xy)
};
power >>= 1;
}
(x, y)
}
// This is n_is_probabprime_fibonacci when FLINT64 is true, from ulong_extras/is_probabprime.c,
// FLINT 3.1.2.
fn is_probable_prime_fibonacci(n: u64) -> bool {
if i64::wrapping_from(n).le_abs(&3) {
return n >= 2;
}
// cannot overflow as (5 / n) = 0 for n = 2 ^ 64 - 1
let m = n.wrapping_sub(u64::wrapping_from(5.jacobi_symbol(n))) >> 1;
if n.significant_bits() <= FLINT_D_BITS {
let inverse = 1.0 / (n as f64);
let (x, y) = fibonacci_chain_precomputed(m, n, inverse);
mod_mul_preinverted_float(n - 3, x, n, inverse)
== mod_mul_preinverted_float(2, y, n, inverse)
} else {
let inverse = u64::precompute_mod_mul_data(&n);
let (x, y) = fibonacci_chain_preinvert(m, n, inverse);
(n - 3).mod_mul_precomputed(x, n, &inverse) == 2.mod_mul_precomputed(y, n, &inverse)
}
}
// This is lchain_precomp when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT 3.1.2.
fn lucas_chain_precomputed(m: u64, a: u64, n: u64, npre: f64) -> (u64, u64) {
let mut x = 2;
let mut y = n - 3;
let mut power = u64::power_of_2(m.significant_bits() - 1);
while power != 0 {
let xy = mod_mul_preinverted_float(x, y, n, npre).mod_sub(a, n);
(x, y) = if m & power != 0 {
(xy, mod_mul_preinverted_float(y, y, n, npre).mod_sub(2, n))
} else {
(mod_mul_preinverted_float(x, x, n, npre).mod_sub(2, n), xy)
};
power >>= 1;
}
(x, y)
}
// This is lchain2_preinv when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT 3.1.2.
fn lucas_chain_preinvert(m: u64, a: u64, n: u64, ninv: u64) -> (u64, u64) {
let mut x = 2;
let mut y = a;
let mut power = u64::power_of_2(m.significant_bits() - 1);
while power != 0 {
let xy = x.mod_mul_precomputed(y, n, &ninv).mod_sub(a, n);
(x, y) = if m & power != 0 {
(xy, y.mod_mul_precomputed(y, n, &ninv).mod_sub(2, n))
} else {
(x.mod_mul_precomputed(x, n, &ninv).mod_sub(2, n), xy)
};
power >>= 1;
}
(x, y)
}
// This is n_is_probabprime_lucas when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT
// 3.1.2, where n is odd and greater than 52, and only true or false is returned, rather than 0, 1,
// or -1.
fn is_probable_prime_lucas(n: u64) -> bool {
let mut d = 0u64;
if i64::wrapping_from(n).le_abs(&2) {
return n == 2;
}
let mut neg_d = false;
let mut j = 0;
for i in 0..100 {
d = 5 + (i << 1);
neg_d = false;
if d.gcd(n % d) == 1 {
if i.odd() {
neg_d = true;
}
let jacobi = if neg_d {
(-i128::from(d)).jacobi_symbol(i128::from(n))
} else {
d.jacobi_symbol(n)
};
if jacobi == -1 {
break;
}
} else if n != d {
return false;
}
j += 1;
}
if j == 100 {
return true;
}
if neg_d {
d.wrapping_neg_assign();
}
let mut q = u64::wrapping_from(1i64.wrapping_sub(i64::wrapping_from(d)) / 4);
if q.get_highest_bit() {
q.wrapping_add_assign(n);
}
let a = q.mod_inverse(n).unwrap().mod_sub(2, n);
let (left, right) = if n <= FLINT_D_BITS {
let inverse = 1.0 / (n as f64);
let (x, y) = lucas_chain_precomputed(n + 1, a, n, inverse);
(
mod_mul_preinverted_float(a, x, n, inverse),
mod_mul_preinverted_float(2, y, n, inverse),
)
} else {
let inverse = u64::precompute_mod_mul_data(&n);
let (x, y) = lucas_chain_preinvert(n + 1, a, n, inverse);
(
a.mod_mul_precomputed(x, n, &inverse),
2.mod_mul_precomputed(y, n, &inverse),
)
};
left == right
}
// This is n_is_strong_probabprime2_preinv when FLINT64 is false, from
// ulong_extras/is_strong_probabprime2_preinv.c, FLINT 3.1.2.
fn is_strong_probable_prime_preinverted_u32(n: u32, inverse: u32, a: u32, d: u32) -> bool {
assert!(a < n);
let nm1 = n - 1;
if a <= 1 || a == nm1 {
return true;
}
let mut t = d;
let mut y = mod_pow_preinverted_u32(a, t, n, inverse);
if y == 1 {
return true;
}
t <<= 1;
while t != nm1 && y != nm1 {
y.mod_mul_precomputed_assign(y, n, &inverse);
t <<= 1;
}
y == nm1
}
// This is n_is_strong_probabprime2_preinv when FLINT64 is true, from
// ulong_extras/is_strong_probabprime2_preinv.c, FLINT 3.1.2.
fn is_strong_probable_prime_preinverted_u64(n: u64, inverse: u64, a: u64, d: u64) -> bool {
assert!(a < n);
let nm1 = n - 1;
if a <= 1 || a == nm1 {
return true;
}
let mut t = d;
let mut y = mod_pow_preinverted_u64(a, t, n, inverse);
if y == 1 {
return true;
}
t <<= 1;
while t != nm1 && y != nm1 {
y.mod_mul_precomputed_assign(y, n, &inverse);
t <<= 1;
}
y == nm1
}
// This is n_mod2_precomp when FLINT64 is true, from ulong_extras/mod2_precomp.c, FLINT 3.1.2.
fn mod_preinverted_float(a: u64, n: u64, inverse: f64) -> u64 {
if a < n {
return a;
}
let ni = i64::wrapping_from(n);
if ni < 0 {
return a.wrapping_sub(n);
}
let (mut q, mut r) = if n == 1 {
(a, 0)
} else {
let q = ((a as f64) * inverse) as u64;
(
q,
i64::wrapping_from(a).wrapping_sub(i64::wrapping_from(q.wrapping_mul(n))),
)
};
if r < ni.wrapping_neg() {
q -= ((r.wrapping_neg() as f64) * inverse) as u64;
} else if r >= ni {
q += ((r as f64) * inverse) as u64;
} else if r < 0 {
return u64::wrapping_from(r + ni);
} else {
return u64::wrapping_from(r);
}
r = i64::wrapping_from(a) - i64::wrapping_from(q.wrapping_mul(n));
u64::wrapping_from(if r >= ni {
r.wrapping_sub(ni)
} else if r < 0 {
r.wrapping_add(ni)
} else {
r
})
}
// This is n_is_strong_probabprime_precomp when FLINT64 is true, from
// ulong_extras/is_strong_probabprime_precomp.c, FLINT 3.1.2.
fn is_strong_probable_prime_preinverted_float(n: u64, inverse: f64, mut a: u64, d: u64) -> bool {
// Map large base to range 2 ... n - 1
if a >= n {
a = mod_preinverted_float(a, n, inverse);
}
let nm1 = n - 1;
if a <= 1 || a == nm1 {
return true;
}
let mut t = d;
let mut y = mod_pow_preinverted_float(a, t, n, inverse);
if y == 1 {
return true;
}
t <<= 1;
while t != nm1 && y != nm1 {
y = mod_mul_preinverted_float(y, y, n, inverse);
t <<= 1;
}
y == nm1
}
// This is n_is_probabprime_BPSW when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT
// 3.1.2, where n is odd and greater than 1.
fn is_probable_prime_bpsw(n: u64) -> bool {
let nm10 = n % 10;
if nm10 == 3 || nm10 == 7 {
return is_probable_prime_fermat(n, 2) && is_probable_prime_fibonacci(n);
}
let mut d = n - 1;
while d.even() {
d >>= 1;
}
let result = if n.significant_bits() <= FLINT_D_BITS {
is_strong_probable_prime_preinverted_float(n, 1.0 / (n as f64), 2, d)
} else {
is_strong_probable_prime_preinverted_u64(n, u64::precompute_mod_mul_data(&n), 2, d)
};
if !result {
return false;
}
is_probable_prime_lucas(n)
}
const FLINT_ODDPRIME_SMALL_CUTOFF: u32 = 4096;
// This is n_is_probabprime when FLINT64 is false, from ulong_extras/is_probabprime.c, FLINT 3.1.2,
// assuming n is odd and greater than 2.
fn is_probable_prime_u32(n: u32) -> bool {
if n < FLINT_ODDPRIME_SMALL_CUTOFF {
return is_odd_prime_small_u32(n);
}
let mut d = n - 1;
d >>= TrailingZeros::trailing_zeros(d);
// For 32-bit, just the 2-base or 3-base Miller-Rabin is enough.
let inverse = u32::precompute_mod_mul_data(&n);
if n < 9080191 {
is_strong_probable_prime_preinverted_u32(n, inverse, 31, d)
&& is_strong_probable_prime_preinverted_u32(n, inverse, 73, d)
} else {
is_strong_probable_prime_preinverted_u32(n, inverse, 2, d)
&& is_strong_probable_prime_preinverted_u32(n, inverse, 7, d)
&& is_strong_probable_prime_preinverted_u32(n, inverse, 61, d)
}
}
// This is n_is_probabprime when FLINT64 is true, from ulong_extras/is_probabprime.c, FLINT 3.1.2,
// assuming n is odd and greater than 2.
fn is_probable_prime_u64(n: u64) -> bool {
if n < u64::from(FLINT_ODDPRIME_SMALL_CUTOFF) {
return is_odd_prime_small_u64(n);
} else if n >= 1050535501 {
// Avoid the unnecessary inverse
return is_probable_prime_bpsw(n);
}
let mut d = n - 1;
d >>= TrailingZeros::trailing_zeros(d);
let inverse = 1.0 / (n as f64);
// For 64-bit, BPSW seems to be a little bit faster than 3 bases.
if n < 341531 {
is_strong_probable_prime_preinverted_float(n, inverse, 9345883071009581737, d)
} else {
is_strong_probable_prime_preinverted_float(n, inverse, 336781006125, d)
&& is_strong_probable_prime_preinverted_float(n, inverse, 9639812373923155, d)
}
}
impl IsPrime for u8 {
/// Tests whether a `u8` is prime.
///
/// This implementation just does a few divisibility checks.
///
/// If you want to generate many small primes, try using
/// [`u8::primes`][crate::num::factorization::traits::Primes::primes] instead.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// ```
/// use malachite_base::num::factorization::traits::IsPrime;
///
/// assert_eq!(5u8.is_prime(), true);
/// assert_eq!(6u8.is_prime(), false);
/// ```
fn is_prime(&self) -> bool {
let n = *self;
if n < 11 {
n == 2 || n == 3 || n == 5 || n == 7
} else if n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0 {
false
} else {
n < 121 || n % 11 != 0 && n % 13 != 0
}
}
}
impl IsPrime for u16 {
/// Tests whether a `u16` is prime.
///
/// This implementation does a few divisibility checks, then performs strong probable prime
/// tests with bases 31 and 73, which is enough to prove primality for any integer less than
/// $2^{16}$.
///
/// If you want to generate many small primes, try using
/// [`u16::primes`][crate::num::factorization::traits::Primes::primes] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::factorization::traits::IsPrime;
///
/// assert_eq!(5u16.is_prime(), true);
/// assert_eq!(6u16.is_prime(), false);
/// assert_eq!(65521u16.is_prime(), true);
/// ```
fn is_prime(&self) -> bool {
// Flint's "BPSW" (which Malachite's code is based on) checked against Feitsma and Galway's
// database [1, 2] up to 2^64 by Dana Jacobsen.
// - [1] http://www.janfeitsma.nl/math/psp2/database
// - [2] http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html
let n = *self;
if n < 11 {
n == 2 || n == 3 || n == 5 || n == 7
} else if n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0 {
false
} else if n < 121 {
// 11*11
true
} else if n % 11 == 0
|| n % 13 == 0
|| n % 17 == 0
|| n % 19 == 0
|| n % 23 == 0
|| n % 29 == 0
|| n % 31 == 0
|| n % 37 == 0
|| n % 41 == 0
|| n % 43 == 0
|| n % 47 == 0
|| n % 53 == 0
{
false
} else {
n < 3481 || is_probable_prime_u32(u32::from(n))
}
}
}
impl IsPrime for u32 {
/// Tests whether a `u32` is prime.
///
/// This implementation does a few divisibility checks, then performs a few strong probable
/// prime tests, which is enough to prove primality for any integer less than $2^{32}$.
///
/// If you want to generate many small primes, try using
/// [`u32::primes`][crate::num::factorization::traits::Primes::primes] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::factorization::traits::IsPrime;
///
/// assert_eq!(5u32.is_prime(), true);
/// assert_eq!(6u32.is_prime(), false);
/// assert_eq!(4294967291u32.is_prime(), true);
/// ```
fn is_prime(&self) -> bool {
// Flint's "BPSW" (which Malachite's code is based on) checked against Feitsma and Galway's
// database [1, 2] up to 2^64 by Dana Jacobsen.
// - [1] http://www.janfeitsma.nl/math/psp2/database
// - [2] http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html
let n = *self;
if n < 11 {
n == 2 || n == 3 || n == 5 || n == 7
} else if n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0 {
false
} else if n < 121 {
// 11*11
true
} else if n % 11 == 0
|| n % 13 == 0
|| n % 17 == 0
|| n % 19 == 0
|| n % 23 == 0
|| n % 29 == 0
|| n % 31 == 0
|| n % 37 == 0
|| n % 41 == 0
|| n % 43 == 0
|| n % 47 == 0
|| n % 53 == 0
{
false
} else if n < 3481 {
// 59*59
true
} else if n > 1000000
&& (n % 59 == 0
|| n % 61 == 0
|| n % 67 == 0
|| n % 71 == 0
|| n % 73 == 0
|| n % 79 == 0
|| n % 83 == 0
|| n % 89 == 0
|| n % 97 == 0
|| n % 101 == 0
|| n % 103 == 0
|| n % 107 == 0
|| n % 109 == 0
|| n % 113 == 0
|| n % 127 == 0
|| n % 131 == 0
|| n % 137 == 0
|| n % 139 == 0
|| n % 149 == 0)
{
false
} else {
is_probable_prime_u32(n)
}
}
}
impl IsPrime for u64 {
/// Tests whether a `u64` is prime.
///
/// This implementation first does a few divisibility checks. Then, depending on the input, it
/// either runs a few strong probable prime tests or the Baillie–PSW test. This is enough to
/// prove primality for any integer less than $2^{64}$.
///
/// If you want to generate many small primes, try using
/// [`u64::primes`][crate::num::factorization::traits::Primes::primes] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::factorization::traits::IsPrime;
///
/// assert_eq!(5u64.is_prime(), true);
/// assert_eq!(6u64.is_prime(), false);
/// assert_eq!(5509785649208481923u64.is_prime(), true);
/// ```
fn is_prime(&self) -> bool {
// Flint's "BPSW" (which Malachite's code is based on) checked against Feitsma and Galway's
// database [1, 2] up to 2^64 by Dana Jacobsen.
// - [1] http://www.janfeitsma.nl/math/psp2/database
// - [2] http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html
let n = *self;
if n < 11 {
n == 2 || n == 3 || n == 5 || n == 7
} else if n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n % 7 == 0 {
false
} else if n < 121 {
// 11*11
true
} else if n % 11 == 0
|| n % 13 == 0
|| n % 17 == 0
|| n % 19 == 0
|| n % 23 == 0
|| n % 29 == 0
|| n % 31 == 0
|| n % 37 == 0
|| n % 41 == 0
|| n % 43 == 0
|| n % 47 == 0
|| n % 53 == 0
{
false
} else if n < 3481 {
// 59*59
true
} else if n > 1000000
&& (n % 59 == 0
|| n % 61 == 0
|| n % 67 == 0
|| n % 71 == 0
|| n % 73 == 0
|| n % 79 == 0
|| n % 83 == 0
|| n % 89 == 0
|| n % 97 == 0
|| n % 101 == 0
|| n % 103 == 0
|| n % 107 == 0
|| n % 109 == 0
|| n % 113 == 0
|| n % 127 == 0
|| n % 131 == 0
|| n % 137 == 0
|| n % 139 == 0
|| n % 149 == 0)
{
false
} else {
is_probable_prime_u64(n)
}
}
}
impl IsPrime for usize {
/// Tests whether a `usize` is prime.
///
/// This implementation first does a few divisibility checks. Then, depending on the input, it
/// either runs a few strong probable prime tests or the Baillie–PSW test. This is enough to
/// prove primality for any integer that fits in a `usize`.
///
/// If you want to generate many small primes, try using
/// [`usize::primes`][crate::num::factorization::traits::Primes::primes] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// use malachite_base::num::factorization::traits::IsPrime;
///
/// assert_eq!(5usize.is_prime(), true);
/// assert_eq!(6usize.is_prime(), false);
/// assert_eq!(4294967291usize.is_prime(), true);
/// ```
#[inline]
fn is_prime(&self) -> bool {
if USIZE_IS_U32 {
u32::wrapping_from(*self).is_prime()
} else {
u64::wrapping_from(*self).is_prime()
}
}
}