Struct primal_slowsieve::Primes

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

Stores information about primes up to some limit.

This uses at least limit / 16 + O(1) bytes of storage.

Implementations§

source§

impl Primes

source

pub fn sieve(limit: usize) -> Primes

Construct a Primes via a sieve up to at least limit.

This stores all primes less than limit (and possibly some more), allowing for very efficient iteration and primality testing below this, and guarantees that all numbers up to limit^2 can be factorised.

source

pub fn upper_bound(&self) -> usize

The largest number stored.

source

pub fn is_prime(&self, n: usize) -> bool

Check if n is prime, possibly failing if n is larger than the upper bound of this Primes instance.

source

pub fn primes(&self) -> PrimeIterator<'_>

Iterator over the primes stored in this map.

source

pub fn factor( &self, n: usize, ) -> Result<Vec<(usize, usize)>, (usize, Vec<(usize, usize)>)>

Factorise n into (prime, exponent) pairs.

Returns Err((leftover, partial factorisation)) if n cannot be fully factored, or if n is zero (leftover == 0). A number can not be completely factored if and only if the prime factors of n are too large for this sieve, that is, if there is

  • a prime factor larger than U^2, or
  • more than one prime factor between U and U^2

where U is the upper bound of the primes stored in this sieve.

Notably, any number between U and U^2 can always be fully factored, since these numbers are guaranteed to only have zero or one prime factors larger than U.

source

pub fn count_upto(&self, n: usize) -> usize

Count the primes upto and including n.

§Panics

count_upto panics if n > self.upper_bound().

Trait Implementations§

source§

impl Debug for Primes

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Primes

§

impl RefUnwindSafe for Primes

§

impl Send for Primes

§

impl Sync for Primes

§

impl Unpin for Primes

§

impl UnwindSafe for Primes

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.