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
// Copyright 2025 Johanna Sörngård
// SPDX-License-Identifier: MIT OR Apache-2.0
//! This module contains implementations of functions that search for primes that neighbour a given number.
use crateis_prime;
/// Generalised function for nearest search by incrementing/decrementing by 1
/// Any attempt at optimising this would be largely pointless since the largest prime gap under 2^64 is only 1550
/// And `is_prime`'s trial division already eliminates most of those
const
/// Returns the largest prime smaller than `n` if there is one.
///
/// Scans for primes downwards from the input with [`is_prime`].
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use const_primes::previous_prime;
/// const PREV: Option<u64> = previous_prime(400);
/// assert_eq!(PREV, Some(397));
/// ```
///
/// There's no prime smaller than two:
///
/// ```
/// # use const_primes::previous_prime;
/// const NO_SUCH: Option<u64> = previous_prime(2);
/// assert_eq!(NO_SUCH, None);
/// ```
pub const
/// Returns the smallest prime greater than `n` if there is one that
/// can be represented by a `u64`.
///
/// Scans for primes upwards from the input with [`is_prime`].
///
/// # Example
///
/// Basic usage:
///
/// ```
/// # use const_primes::next_prime;
/// const NEXT: Option<u64> = next_prime(400);
/// assert_eq!(NEXT, Some(401));
/// ```
///
/// Primes larger than 18446744073709551557 can not be represented by a `u64`:
///
/// ```
/// # use const_primes::next_prime;
/// const NO_SUCH: Option<u64> = next_prime(18_446_744_073_709_551_557);
/// assert_eq!(NO_SUCH, None);
/// ```
pub const