@@ -5,8 +5,8 @@ use crate::is_prime;
5
5
/// Generalised function for nearest search by incrementing/decrementing by 1
6
6
/// Any attempt at optimising this would be largely pointless since the largest prime gap under 2^64 is only 1550
7
7
/// And is_prime's trial division already eliminates most of those
8
- const fn bounded_search ( mut n : u64 , stride : Stride ) -> Option < u64 > {
9
- let stride = stride. into_u64 ( ) ;
8
+ const fn bounded_search ( mut n : u64 , stride : u64 ) -> Option < u64 > {
9
+ debug_assert ! ( stride == 1 || stride == u64 :: MAX ) ;
10
10
11
11
loop {
12
12
// Addition over Z/2^64, aka regular addition under optimisation flags
@@ -46,7 +46,8 @@ const fn bounded_search(mut n: u64, stride: Stride) -> Option<u64> {
46
46
/// ```
47
47
#[ must_use = "the function only returns a new value and does not modify its input" ]
48
48
pub const fn previous_prime ( n : u64 ) -> Option < u64 > {
49
- bounded_search ( n, Stride :: Down )
49
+ // Adding by 2^64-1 over Z/2^64 is equivalent to subtracting by 1
50
+ bounded_search ( n, u64:: MAX )
50
51
}
51
52
52
53
/// Returns the smallest prime greater than `n` if there is one that
@@ -73,20 +74,5 @@ pub const fn previous_prime(n: u64) -> Option<u64> {
73
74
/// ```
74
75
#[ must_use = "the function only returns a new value and does not modify its input" ]
75
76
pub const fn next_prime ( n : u64 ) -> Option < u64 > {
76
- bounded_search ( n, Stride :: Up )
77
- }
78
-
79
- enum Stride {
80
- Up ,
81
- Down ,
82
- }
83
-
84
- impl Stride {
85
- const fn into_u64 ( self ) -> u64 {
86
- match self {
87
- Self :: Up => 1 ,
88
- // Adding by 2^64-1 over Z/2^64 is equivalent to subtracting by 1
89
- Self :: Down => u64:: MAX ,
90
- }
91
- }
77
+ bounded_search ( n, 1 )
92
78
}
0 commit comments