prime_data/data/
mod.rs

1pub use prime_byte::PrimeByte;
2mod prime_byte;
3
4pub use self::prime_data::PrimeData;
5mod prime_data;
6
7pub use iterators::{CoprimeIter, PrimeIter};
8mod iterators;
9
10pub use error::{PrimeResult, PrimeError};
11pub mod error;
12
13pub mod estimate;
14
15#[cfg(feature = "factors")]
16mod factors;
17#[cfg(feature = "factors")]
18pub use factors::{Factorization, all_factors_of};
19
20mod utils;
21
22/// A list of all values `N % 30`, where N is coprime with 2, 3, and 5
23/// 
24/// These values are: {1, 7, 11, 13, 17, 19, 23, 29}
25pub const K_VALUES: [u8; 8] = [1, 7, 11, 13, 17, 19, 23, 29];
26
27
28pub use public_methods::*;
29mod public_methods {
30
31    use super::utils::IntSqrt;
32
33    /// Verifies if `x` is a prime number
34    /// 
35    /// Currently, this function is an abstraction over generating prime data up to sqrt(x) then
36    /// calling the [check prime](super::PrimeData::check_prime) method.
37    /// 
38    /// Therefore, if you need to check if lots of numbers are prime, it's heavily encouraged to
39    /// [generate](super::PrimeData::generate) prime numbers then calling that method.
40    /// 
41    /// However, it is planned to make this function faster by using primality tests instead of 
42    /// generating data. See [here](crate::guide::future).
43    /// 
44    /// # Examples
45    /// 
46    /// ```
47    /// use prime_data::is_prime;
48    /// assert!( is_prime(65_537));
49    /// assert!(!is_prime(4_294_967_297));
50    /// ```
51    pub fn is_prime(x: u64) -> bool {
52        let sqrt = x.sqrt_floor();
53
54        super::PrimeData::generate(0..=sqrt).check_prime(x)
55    }
56
57    /// Counts how many prime numbers are there less than or equal to `x`
58    /// 
59    /// This function is an abstraction for [generating](super::PrimeData::generate) prime numbers
60    /// up to x, then calling the [count primes](super::PrimeData::count_primes) method.
61    /// 
62    /// If you only need an approximation, see [estimates](crate::estimate).
63    /// 
64    /// # Examples
65    /// 
66    /// ```
67    /// use prime_data::count_primes;
68    /// assert_eq!(count_primes(1_000),   168);
69    /// assert_eq!(count_primes(100_000), 9592);
70    /// ```
71    pub fn count_primes(x: u64) -> u64 {
72        super::PrimeData::generate(0..=x).count_primes()
73    }
74}