Skip to content

Commit c61c196

Browse files
committed
Test with fewer witnesses for smaller numbers
1 parent 38d430e commit c61c196

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/check.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,31 @@ pub const fn is_prime(n: u64) -> bool {
4444
// we can use the fact that there are known perfect bases
4545
// in order to make the test both fast and deterministic.
4646
// This list of witnesses was taken from
47-
// <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>
48-
// and is sufficient for all numbers smaller than 2^64.
49-
const NUM_WITNESSES: usize = 12;
50-
const WITNESSES: [u64; NUM_WITNESSES] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
47+
// <https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases>.
48+
const WITNESSES: &[(u64, &[u64])] = &[
49+
(2_046, &[2]),
50+
(1_373_652, &[2, 3]),
51+
(9_080_190, &[31, 73]),
52+
(25_326_000, &[2, 3, 5]),
53+
(4_759_123_140, &[2, 7, 61]),
54+
(1_112_004_669_632, &[2, 13, 23, 1662803]),
55+
(2_152_302_898_746, &[2, 3, 5, 7, 11]),
56+
(3_474_749_660_382, &[2, 3, 5, 7, 11, 13]),
57+
(341_550_071_728_320, &[2, 3, 5, 7, 11, 13, 17]),
58+
(3_825_123_056_546_413_050, &[2, 3, 5, 7, 11, 13, 17, 19, 23]),
59+
(u64::MAX, &[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]),
60+
];
5161

5262
let mut i = 0;
53-
while i < NUM_WITNESSES && WITNESSES[i] < n {
54-
if !miller_test(d, n, WITNESSES[i]) {
63+
while WITNESSES[i].0 < n {
64+
i += 1;
65+
}
66+
let witnesses = WITNESSES[i].1;
67+
let num_witnesses = witnesses.len();
68+
69+
let mut i = 0;
70+
while i < num_witnesses && witnesses[i] < n {
71+
if !miller_test(d, n, witnesses[i]) {
5572
return false;
5673
}
5774
i += 1;

0 commit comments

Comments
 (0)