pub struct Primes { /* private fields */ }Expand description
Collection of generated prime numbers.
Implementations§
Source§impl Primes
impl Primes
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new Primes that contains numbers generated up to 3
(inclusive).
Sourcepub fn generate_to(&mut self, n: usize)
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);
}
}
}Sourcepub fn generate_amount(&mut self, amount: usize)
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);
}
}Sourcepub fn is_prime(&self, n: usize) -> bool
pub fn is_prime(&self, n: usize) -> bool
Checks whether a number is prime.
This method works faster the more primes are generated.
Sourcepub fn is_prime_mut(&mut self, n: usize) -> bool
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.
Sourcepub fn sieve(&self) -> &[bool]
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
}Trait Implementations§
Source§impl<'a> IntoIterator for &'a mut Primes
impl<'a> IntoIterator for &'a mut Primes
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more