Struct primal_sieve::StreamingSieve

source ·
pub struct StreamingSieve { /* private fields */ }
Expand description

A heavily optimised prime sieve.

This is a streaming segmented sieve, meaning it sieves numbers in intervals, extracting whatever it needs and discarding it. See Sieve for a wrapper that caches the information to allow for repeated queries, at the cost of O(limit) memory use.

This uses O(sqrt(limit)) memory, and is designed to be as cache-friendly as possible. StreamingSieve should be used for one-off calls, or simple linear iteration.

The design is heavily inspired/adopted from Kim Walisch’s primesieve, and has similar speed (around 5-20% slower).

§Examples

let count = primal::StreamingSieve::prime_pi(123456);
println!("𝜋(123456) = {}", count);

Implementations§

source§

impl StreamingSieve

source

pub fn prime_pi(n: usize) -> usize

Count the number of primes upto and including n, that is, 𝜋, the prime counting function.

§Examples
assert_eq!(primal::StreamingSieve::prime_pi(10), 4);
// the endpoint is included
assert_eq!(primal::StreamingSieve::prime_pi(11), 5);

assert_eq!(primal::StreamingSieve::prime_pi(100), 25);
assert_eq!(primal::StreamingSieve::prime_pi(1000), 168);
source

pub fn nth_prime(n: usize) -> usize

Compute pn, the n prime number, 1-indexed (i.e. p1 = 2, p2 = 3).

§Panics

n must be larger than 0 and less than the total number of primes in this sieve (that is, self.prime_pi(self.upper_bound())).

§Example
assert_eq!(primal::StreamingSieve::nth_prime(1_000), 7919);

Trait Implementations§

source§

impl Debug for StreamingSieve

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.