Struct LinearSieve

Source
pub struct LinearSieve<const LEN: usize> { /* private fields */ }
Expand description

Generate a table of prime numbers by linear sieving.

This structure supports table generation by compile-time calculations and can also determine prime numbers at compile time.

§Limitations

The range generated is 0..LEN, NOT 0..=LEN. This is due to the limitations of the const generics.
Therefore, even if memory and computation time were infinite, this structure could not be used to determine whether usize::MAX is prime or not.

Also, implementation of types other than usize is currently not provided.
It may be possible to implement it through specialization or const_trait_impl stabilization, but no implementation method has been found so far.

§Examples

use primality_test::LinearSieve;

const S: LinearSieve<15> = LinearSieve::new();
const PRIME: bool = S.is_prime(7);
const NOT_PRIME: bool = S.is_prime(10);

assert!(PRIME);
assert!(!NOT_PRIME);

Note that [T; N] is used instead of Vec<T> to support compile-time calculations.
This may result in increased stack memory usage for runtime table generation.

use primality_test::LinearSieve;

// Data size is too big...
// At this line, stack overflow may occur.
let _s = LinearSieve::<10000000000000>::new();

Implementations§

Source§

impl<const LEN: usize> LinearSieve<LEN>

Source

pub const fn new() -> Self

Constructor.
This method supports the compile-time generation.

Source

pub const fn len(&self) -> usize

Return the length of the internal array.

The value returned by this method is always equal to LEN.

Source

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

Check if value is prime or not.

§Panics

This method panics when value >= LEN is true.

§Examples
use primality_test::LinearSieve;

const S: LinearSieve<1000> = LinearSieve::new();
assert!(S.is_prime(2));
assert!(S.is_prime(997));

assert!(!S.is_prime(0));
assert!(!S.is_prime(1));
assert!(!S.is_prime(561));
// S.is_prime(1000)     // Panic at this line
Source

pub fn factors(&self, value: usize) -> impl Iterator<Item = usize> + '_

Factorize value into prime factors.

LinearSieve keeps internally the least prime factor of each value.
By using this, the method is guaranteed to return the prime factors of value in ascending order.

1 is not contained in the returned factors.
If value is less then 2, the length of returned iterator is always 0.

§Panics

This method panics when value >= LEN is true.

§Examples
use primality_test::LinearSieve;

const S: LinearSieve<100> = LinearSieve::new();
let factors_of_120 = S.factors(60).collect::<Vec<_>>();
assert_eq!(factors_of_120, vec![2, 2, 3, 5]);

let factors_of_11 = S.factors(11).collect::<Vec<_>>();
assert_eq!(factors_of_11, vec![11]);

assert!(S.factors(1).next().is_none());

Trait Implementations§

Source§

impl<const LEN: usize> Clone for LinearSieve<LEN>

Source§

fn clone(&self) -> LinearSieve<LEN>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const LEN: usize> Debug for LinearSieve<LEN>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<const LEN: usize> Default for LinearSieve<LEN>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const LEN: usize> IntoIterator for LinearSieve<LEN>

Source§

fn into_iter(self) -> Self::IntoIter

Generate a stream of prime numbers.

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = FilterMap<Enumerate<IntoIter<usize, LEN>>, fn((usize, usize)) -> Option<usize>>

Which kind of iterator are we turning this into?
Source§

impl<const LEN: usize> Copy for LinearSieve<LEN>

Auto Trait Implementations§

§

impl<const LEN: usize> Freeze for LinearSieve<LEN>

§

impl<const LEN: usize> RefUnwindSafe for LinearSieve<LEN>

§

impl<const LEN: usize> Send for LinearSieve<LEN>

§

impl<const LEN: usize> Sync for LinearSieve<LEN>

§

impl<const LEN: usize> Unpin for LinearSieve<LEN>

§

impl<const LEN: usize> UnwindSafe for LinearSieve<LEN>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.