[][src]Struct elr_primes::Primes

pub struct Primes { /* fields omitted */ }

Prime Iterator

This provides an iterator over prime numbers up to a given maximum value. The inclusive upper bound for prime numbers is provided when the structure is instantiated.

Implementations

impl Primes[src]

pub fn new(bound: usize) -> Self[src]

Create a new Primes instance.

The maximum bound for prime numbers is passed as a parameter to new(). If the bound is less than 2, then the iterator will provide no primes.

The method spawns threads to generate blocks of threads concurrently.

Example

use elr_primes::Primes;

// Create an iterator for primes that are less than or equal to 1000
let p = Primes::new(1000);

pub fn factors(&self, value: usize) -> Result<Factors, (Factors, usize)>[src]

Find the prime factors for a number.

Returns a Factor type containing the factors for the number.

Errors

If the the number has prime factors that are outside of the current bounds, a tuple is returned with the first element as a Factor type containing the in-bound factors found for the number, and the second element as the remainder value that could not be factored within the current bounds.

Example

let p = Primes::new(10);

let factors = p.factors(40);
assert!(factors.is_ok());
assert_eq!(factors.ok(), Some(vec![(2, 3), (5, 1)])); // 2^3 * 5^1 = 40

let factors = p.factors(429);
assert!(factors.is_err());
assert_eq!(factors.err(), Some((vec![(3, 1)], 143))); // 3^1 is a prime factor of 429
                                                      // but the remainder 143 has no
                                                      // factors in bounds

pub fn primality(&self, value: usize) -> Primality[src]

Returns the primality of a number.

This method tries to determine the primality of a given number through trial division of the primes that are less than the square root of the given number.

Returns:

  • Primality::Composite - A prime was found that is a factor of the number
  • Primality::Prime - No prime was found that is a factor of the number, and the square root of the number is not greater than the current bounds
  • Primality::Unknown - No prime was found that is a factor of the number, but the square root fo the number is greater than the current bounds

Example

use elr_primes::{Primality, Primes};

let p = Primes::new(31);
assert_eq!(p.primality(953), Primality::Prime);
assert_eq!(p.primality(959), Primality::Composite);
assert_eq!(p.primality(967), Primality::Unknown);  // 967 is prime, but it's square root
                                                   // is greater than the current bound
                                                   // so it cannot be definitively known
                                                   // as prime through trial division

assert_eq!(p.primality(969), Primality::Composite); // The square root of 969 is also
                                                    // greater than the current bound, but
                                                    // it has a factor (3) that is within
                                                    // the bounds, so it can be classified
                                                    // as a composite number

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

Returns an iterator for the bound prime numbers.

Example

let p = Primes::new(100);

// Print all of the primes below 100
for prime_value in p.primes() {
    println!("{}", prime_value);
}

Trait Implementations

impl Debug for Primes[src]

impl Eq for Primes[src]

impl PartialEq<Primes> for Primes[src]

impl StructuralEq for Primes[src]

impl StructuralPartialEq for Primes[src]

Auto Trait Implementations

impl RefUnwindSafe for Primes

impl Send for Primes

impl Sync for Primes

impl Unpin for Primes

impl UnwindSafe for Primes

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.