const-primes
A crate for generating prime numbers at compile time.
Also contains various functions for computing other things related to prime numbers in const
contexts.
#![no_std]
compatible.
Examples
Generate arrays of prime numbers with the function primes
which uses a segmented sieve of Eratosthenes.
const PRIMES: = primes;
assert_eq!;
assert_eq!;
or with the type Primes
which ensures that a non-zero number of primes are generated:
const PRIMES: = new;
assert_eq!;
assert_eq!;
It also lets you reuse it as a cache of primes for related computations:
const CACHE: = new;
// For primality testing
const CHECK_42: = CACHE.is_prime;
const CHECK_541: = CACHE.is_prime;
assert_eq!;
assert_eq!;
// Or for prime counting
const PRIMES_LEQ_100: = CACHE.count_primes_leq;
assert_eq!;
// If questions are asked about numbers outside the cache it returns None
assert!;
assert!;
Creating a Primes<0>
is a compile fail in const contexts and a panic otherwise.
Other functionality
Use are_prime
to compute the prime status of all integers below a given value
const PRIME_STATUS: = are_prime;
// 0 1 2 3 4 5 6 7 8 9
assert_eq!;
or is_prime
to test whether a given number is prime
const CHECK: bool = is_prime;
assert!;
or prime_counts
to count the number of primes less than or equal to each index of an array
# use prime_counts;
const COUNTS: = prime_counts;
assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0 LICENSE-APACHE
- MIT license LICENSE-MIT
at your option.
Contribution
Contributions are welcome!
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.