Skip to content

Commit

Permalink
Move consts to start of block
Browse files Browse the repository at this point in the history
  • Loading branch information
JSorngard committed Jul 8, 2024
1 parent 3aaba18 commit 1b2fbeb
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ use crate::integer_math::{mod_mul, mod_pow};
/// ```
#[must_use]
pub const fn is_prime(n: u64) -> bool {
// Since we know the maximum size of the numbers we test against
// we can use the fact that there are known perfect bases
// in order to make the test both fast and deterministic.
// This list of witnesses was taken from
// <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>.
const NUM_BASES: usize = 11;
const WITNESSES: [(u64, &[u64]); NUM_BASES] = [
(2_046, &[2]),
(1_373_652, &[2, 3]),
(9_080_190, &[31, 73]),
(25_326_000, &[2, 3, 5]),
(4_759_123_140, &[2, 7, 61]),
(1_112_004_669_632, &[2, 13, 23, 1_662_803]),
(2_152_302_898_746, &[2, 3, 5, 7, 11]),
(3_474_749_660_382, &[2, 3, 5, 7, 11, 13]),
(341_550_071_728_320, &[2, 3, 5, 7, 11, 13, 17]),
(3_825_123_056_546_413_050, &[2, 3, 5, 7, 11, 13, 17, 19, 23]),
(u64::MAX, &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]),
];

if n == 2 || n == 3 {
return true;
} else if n <= 1 || n % 2 == 0 || n % 3 == 0 {
Expand All @@ -40,26 +60,6 @@ pub const fn is_prime(n: u64) -> bool {
d >>= 1;
}

// Since we know the maximum size of the numbers we test against
// we can use the fact that there are known perfect bases
// in order to make the test both fast and deterministic.
// This list of witnesses was taken from
// <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>.
const NUM_BASES: usize = 11;
const WITNESSES: [(u64, &[u64]); NUM_BASES] = [
(2_046, &[2]),
(1_373_652, &[2, 3]),
(9_080_190, &[31, 73]),
(25_326_000, &[2, 3, 5]),
(4_759_123_140, &[2, 7, 61]),
(1_112_004_669_632, &[2, 13, 23, 1_662_803]),
(2_152_302_898_746, &[2, 3, 5, 7, 11]),
(3_474_749_660_382, &[2, 3, 5, 7, 11, 13]),
(341_550_071_728_320, &[2, 3, 5, 7, 11, 13, 17]),
(3_825_123_056_546_413_050, &[2, 3, 5, 7, 11, 13, 17, 19, 23]),
(u64::MAX, &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]),
];

let mut i = 0;
while i < NUM_BASES && WITNESSES[i].0 < n {
i += 1;
Expand Down

0 comments on commit 1b2fbeb

Please sign in to comment.