Primes

Struct Primes 

Source
pub struct Primes { /* private fields */ }
Expand description

Collection of generated prime numbers.

Implementations§

Source§

impl Primes

Source

pub fn new() -> Self

Constructs a new Primes that contains numbers generated up to 3 (inclusive).

Source

pub fn generate_to(&mut self, n: usize)

Generates primes up to at least n.

§Example
use primter::Primes;

fn main() {
    let mut primes = Primes::new();
    primes.generate_to(100);

    for n in 1..=100 {
        if primes.is_prime(n) {
            println!("{} is prime", n);
        } else {
            println!("{} is not prime", n);
        }
    }
}
Source

pub fn generate_amount(&mut self, amount: usize)

Generates primes so that the total amount is at least amount.

§Example
use primter::Primes;

fn main() {
    let mut primes = Primes::new();
    primes.generate_amount(100);

    for prime in primes.into_iter().take(100) {
        println!("{}", prime);
    }
}
Source

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

Checks whether a number is prime.

This method works faster the more primes are generated.

Source

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

Checks whether a number is prime.

This method generates all the primes up to at least n in order to check if it’s prime.

Source

pub fn sieve(&self) -> &[bool]

Returns an immutable reference to the underlying sieve of Eratosthenes.

To check if number is in the sieve, simply use it as the index.

§Example
use primter::Primes;

fn main() {
    let mut primes = Primes::new();
    primes.generate_to(10);
    assert!(primes.sieve()[10]); // 10 is not prime
}
Source

pub fn primes(&self) -> &[usize]

Returns an immutable reference to the underlying Vec of generated primes.

Source

pub fn iter(&mut self) -> Iter<'_>

Constructs a borrowed iterator

Trait Implementations§

Source§

impl<'a> IntoIterator for &'a mut Primes

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Primes

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. 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>,

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.