#prime #numbers #iterator #prime-factors #calculations #methods #basic

elr_primes

Provides a prime number iterator and methods for basic calculations with prime numbers

3 releases

0.1.2 Jul 27, 2020
0.1.1 Jul 27, 2020
0.1.0 Jul 4, 2020

#1678 in Math

Download history 19/week @ 2024-03-21 70/week @ 2024-03-28 68/week @ 2024-04-04 15/week @ 2024-04-11 5/week @ 2024-04-18

73 downloads per month

MIT/Apache

17KB
181 lines

Prime Number Iterator and Calculations

This library provides a structure for iterating through prime numbers as well as methods for calculating prime factors and classifying numbers as prime or composite.

API Reference (docs.rs)

Using this library

Add the following to your Cargo.toml file

[dependencies]
elr_primes = "0.1.2"

Examples

Basic Usage:

use elr_primes::Primes;

// Provides an iterator for all prime numbers less than or equal to 1000
let mut p = Primes::new(1000);

Once the structure has been initiated, the primes() method provides an iterator for the prime numbers.

# use elr_primes::Primes;
let p = Primes::new(10);  // Primes less than or equal to 10
let mut prime_iter = p.primes();
let primes: Vec<usize> = prime_iter.copied().collect();
let expected: [usize; 4] = [2, 3, 5, 7];
assert_eq!(primes, expected);

Since primes() returns an iterator, you can also use it to directly find a specific prime number.

# use elr_primes::Primes;
let p = Primes::new(100); // Primes less than or equal to 100
let n = 20;

// Iterators are zero-based, so to get the 20th prime we need to search for the 19th
match p.primes().nth(n - 1) {
    Some(x) => println!("The {}th prime is: {}", n, x),
    None => println!("The {}th prime is outside the current bounds", n)
};

Methods are also available to find the prime factors of a number, and whether a number is prime or composite.

use elr_primes::{Primes, Primality};
let p = Primes::new(100);

let n = 96;
match p.factors(n) {
    Ok(factors) => println!("{:?}", factors),
    Err(_) => println!("Could not find all prime factors within the bounds"),
};

let n = 23;
match p.primality(n) {
    Primality::Prime => println!("{} is prime", n),
    Primality::Composite => println!("{} is composite", n),
    Primality::Unknown => println!("Primality of {} is undetermined", n),
};

No runtime deps