const_primes

Macro sieve_segment

Source
macro_rules! sieve_segment {
    ($n:expr; < $lim:expr) => { ... };
    ($n:expr; >= $lim:expr) => { ... };
}
Expand description

Generate arrays of the prime status of large numbers without having to store the prime status of every single integer smaller than the target in the result, and thus potentially the binary.

Calls sieve_geq or sieve_lt, and automatically computes the memory requirement of the sieve.

Sieve the N smallest numbers larger than or equal to some limit as sieve_segment!(N; >= LIMIT), and the N largest numbers smaller than some limit as sieve_segment!(N; < LIMIT).

Computes the sieve size as isqrt(upper_limit) + 1 for sieve_lt and as isqrt(lower_limit) + 1 + N for sieve_geq. This may overestimate the memory requirement for sieve_geq.

§Examples

const PRIME_STATUS_LT: Result<[bool; 5], SieveError> = sieve_segment!(5; < 100_005);
const PRIME_STATUS_GEQ: Result<[bool; 7], SieveError> = sieve_segment!(7; >= 615);
assert_eq!(
    PRIME_STATUS_LT,
//      100_000  100_101  100_102  100_103  100_104
    Ok([false,   false,   false,   true,    false])
);
assert_eq!(
    PRIME_STATUS_GEQ,
//      615    616    617   618    619   620    621
    Ok([false, false, true, false, true, false, false])
);

§Errors

Has the same error behaviour as sieve_geq and sieve_lt, with the exception that it sets MEM such that the sieve doesn’t run out of memory.