#prime #numbers #const #context #sieve #compile-time #eratosthenes

const-primes

Generate and work with prime numbers in const contexts

20 unstable releases (3 breaking)

0.4.8 Oct 22, 2023
0.4.7 Oct 13, 2023
0.4.4 Sep 28, 2023
0.3.5 Sep 25, 2023
0.1.3 Sep 22, 2023

#283 in Math

Download history 43/week @ 2024-02-20 41/week @ 2024-02-27 13/week @ 2024-03-12

97 downloads per month

MIT/Apache

120KB
823 lines

Latest Version Build Status codecov

const-primes

A crate for generating and working with 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: [u32; 10] = primes();
assert_eq!(PRIMES[5], 13);
assert_eq!(PRIMES, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);

or with the type Primes which ensures that a non-zero number of primes are generated:

const PRIMES: Primes<10> = Primes::new();
assert_eq!(PRIMES[5], 13);
assert_eq!(PRIMES, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);

It also lets you reuse it as a cache of primes for related computations:

const CACHE: Primes<100> = Primes::new();

// For primality testing
const CHECK_42: Option<bool> = CACHE.is_prime(42);
const CHECK_541: Option<bool> = CACHE.is_prime(541);
assert_eq!(CHECK_42, Some(false));
assert_eq!(CHECK_541, Some(true));

// Or for prime counting
const PRIMES_LEQ_100: Option<usize> = CACHE.count_primes_leq(100);
assert_eq!(PRIMES_LEQ_100, Some(25));

// If questions are asked about numbers
// outside the cache it returns None
assert!(CACHE.is_prime(1000).is_none());
assert!(CACHE.count_primes_leq(1000).is_none());

Creating a Primes<0> is a compile fail in const contexts and a panic otherwise.

Other functionality

Use is_prime to test whether a given number is prime

const CHECK: bool = is_prime(18_446_744_073_709_551_557);
assert!(CHECK);

or are_prime to compute the prime status of the N first integers,

const N: usize = 10;
const PRIME_STATUS: [bool; N] = are_prime();
//                        0      1      2     3     4      5     6      7     8      9
assert_eq!(PRIME_STATUS, [false, false, true, true, false, true, false, true, false, false]);

or are_prime_below to compute the prime status of the N largest integers below a given value,

const N: usize = 70711;
const BIG_PRIME_STATUS: [bool; N] = are_prime_below(5_000_000_031);
//                                     5_000_000_028  5_000_000_029  5_000_000_030
assert_eq!(BIG_PRIME_STATUS[N - 3..], [false,         true,          false]);

and more!

License

Licensed under either of

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.

No runtime deps