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
// numera::number::integer::prime::fns
//
//! Prime related standalone functions.
//
// TOC
//
// - prime_number_theorem
//
// - is_prime
// - is_prime_brute
// - nth_prime
// - prime_pi
//
// - is_prime_sieve
// - nth_prime_sieve
// - prime_pi_sieve
//
// - largest_prime_pow2_doublings
// - ten_primes_less_pow2
use Sieve;
use ;
use cratesqrt_fisr64;
/// The prime number theorem ([m][0m]/[w][0w]) formula.
///
/// $$ \large \pi(x) \sim \frac{x}{\ln(x)} $$
///
/// Returns the approximate count of primes less than the given `n`.
///
/// [0m]: https://mathworld.wolfram.com/PrimeNumberTheorem.html
/// [0w]: https://en.wikipedia.org/wiki/Prime_number_theorem
//
// IMPROVE: use big int and big float.
/// # Examples
/// ```
/// use numera::all::prime_number_theorem as pi;
///
/// // Showing the % difference against the real amount, if known.
/// // Note how precision increases in direct relationship to the power.
/// assert_eq![pi(u8::MAX.into()), 46]; // 14.81% < 54
/// assert_eq![pi(u16::MAX.into()), 5909]; // 9.67% < 6542
/// assert_eq![pi(u32::MAX.into()), 193635251]; // 4.74% < 203280221
/// assert_eq![pi(u64::MAX.into()), 415828534307635072]; // 2.30% < 425656284035217743
/// assert_eq![pi(2u128.pow(92)), 77650867634561160386183168]; // 1.59% < 78908656317357166866404346
/// assert_eq![pi(u128::MAX.into()), 3835341275459348115779911081237938176]; // ?% < ?
/// ```
///
/// # Links
/// - The exact prime count till $2^{92}$ is available in <https://oeis.org/A007053>.
/// Checks whether a `number` is prime, using optimized trial division.
///
/// This approach checks only odd numbers starting from 3 and up to the square
/// root of the given number. This is based on the fact that if a number is
/// divisible by a number larger than its square root, the result of the
/// division will be smaller than the square root, and it would have already
/// been checked in previous iterations.
///
/// # Examples
/// ```
/// use numera::all::is_prime;
///
/// assert![is_prime(13)];
/// assert![!is_prime(8)];
/// ```
/// Checks whether a `number` is prime, using basic trial division.
///
/// This naive approach checks all numbers from 2 to number/2.
///
/// # Examples
/// ```
/// use numera::all::is_prime_brute;
///
/// assert![is_prime_brute(13)];
/// assert![!is_prime_brute(8)];
/// ```
/// Finds the 0-indexed `nth` prime number using [`is_prime`].
///
/// # Examples
/// ```
/// use numera::all::nth_prime;
///
/// assert_eq![nth_prime(0), 2];
/// assert_eq![nth_prime(1), 3];
/// assert_eq![nth_prime(53), 251];
/// ```
/// Counts the number of primes upto and including `n`, using [`is_prime`].
///
/// # Notation
/// $\pi(x)$
///
/// # Examples
/// ```
/// use numera::all::prime_pi;
///
/// assert_eq![prime_pi(2), 1];
/// assert_eq![prime_pi(3), 2];
/// assert_eq![prime_pi(251), 54];
/// ```
///
/// # Links
/// - <https://mathworld.wolfram.com/PrimeCountingFunction.html>.
/// - <https://en.wikipedia.org/wiki/Prime-counting_function>.
/// Checks whether a `number` is prime using an optimized sieve.
// /// Finds the 1-indexed `nth` prime number, using [`is_prime_sieve`].
// #[inline]
// #[cfg(feature = "std")]
// #[cfg_attr(feature = "nightly", doc(cfg(feature = "std")))]
// pub fn nth_prime_sieve(nth: NonZeroUsize) -> usize {
// Sieve::new(nth.get()).nth_prime(nth.get())
// }
/// Finds the 0-indexed `nth` prime number using [`is_prime_sieve`] with
/// the provided `upper_bound`.
///
/// # Panics
/// Panics if nth == [`usize::MAX`].
//
// IMPROVE: make non-panicking version
/// Counts the number of primes upto and including `n` using [`is_prime_sieve`].
/* data extraction */
/// Returns a big integer containing the largest prime just less a power of two.
///
/// Valid `i` values are between 0 and 13 inclusive, which corresponds to
/// bit-sizes between 8 and 65,536.
///
/// Returns `None` if `index` is > 13.
///
/// It uses the [`LARGEST_PRIME_POW2_DOUBLINGS`] table.
///
/// # Examples
/// ```
/// use numera::all::{largest_prime_pow2_doublings, ConstUpperBounded, Nnz128, Prime64};
///
/// assert_eq![largest_prime_pow2_doublings(3).unwrap(), Prime64::MAX.into()];
/// assert_eq![largest_prime_pow2_doublings(4).unwrap(), (Nnz128::MAX - Nnz128::new(159 -1)).into()];
/// ```
/// Returns a big integer containing one of the ten largest primes just less
/// than a power of two, between 8 and 400 bits.
///
/// - Valid `bitsize` values are between 8 and 400 inclusive.
/// - Valid `index` values are from 0 to 9 inclusive, and indexes from the end.
/// E.g. index=0 is the largest prime for the given bitsize.
///
/// Returns `None` if `bitsize` is < 8 or > 400 or if `index` is more than 9.
///
/// It uses the [`TEN_PRIMES_LESS_POW2`] table.
///
/// # Examples
/// ```
/// use numera::number::{integer::prime::*, traits::ConstUpperBounded};
///
/// assert_eq![ten_primes_less_pow2(8, 0).unwrap(), Prime8::MAX.into()];
/// assert_eq![ten_primes_less_pow2(16, 0).unwrap(), Prime16::MAX.into()];
/// assert_eq![ten_primes_less_pow2(32, 0).unwrap(), Prime32::MAX.into()];
/// assert_eq![ten_primes_less_pow2(64, 0).unwrap(), Prime64::MAX.into()];
/// assert_eq![ten_primes_less_pow2(128, 0).unwrap(), Prime128::MAX.into()];
///
/// assert_eq![ten_primes_less_pow2(8, 0).unwrap(), PRIMES_U8[53].into()];
/// assert_eq![ten_primes_less_pow2(8, 1).unwrap(), PRIMES_U8[53-1].into()];
/// assert_eq![ten_primes_less_pow2(8, 9).unwrap(), PRIMES_U8[53-9].into()];
/// assert_eq![ten_primes_less_pow2(16, 0).unwrap(), PRIMES_U16[6_487].into()];
/// assert_eq![ten_primes_less_pow2(16, 1).unwrap(), PRIMES_U16[6_487-1].into()];
/// assert_eq![ten_primes_less_pow2(16, 9).unwrap(), PRIMES_U16[6_487-9].into()];
/// ```