#prime #iterator #numbers #prime-factors #sieve #eratosthenes #factor

primes

A package for calculating primes using the Sieve of Eratosthenes, and using that to check if a number is prime and calculating factors. Includes an iterator over all primes.

15 releases

0.3.0 Mar 8, 2020
0.2.4 Dec 26, 2019
0.2.3 Aug 29, 2018
0.2.0 Apr 27, 2015
0.1.0 Nov 26, 2014

#439 in Algorithms

Download history 2547/week @ 2023-12-16 855/week @ 2023-12-23 1346/week @ 2023-12-30 1979/week @ 2024-01-06 2689/week @ 2024-01-13 2300/week @ 2024-01-20 2887/week @ 2024-01-27 2511/week @ 2024-02-03 2999/week @ 2024-02-10 2479/week @ 2024-02-17 2178/week @ 2024-02-24 2736/week @ 2024-03-02 2889/week @ 2024-03-09 2830/week @ 2024-03-16 2844/week @ 2024-03-23 2112/week @ 2024-03-30

11,155 downloads per month
Used in 11 crates (9 directly)

BSD-3-Clause

20KB
295 lines

primes

Build Status Build Status

A prime generator for Rust.

This package is available on crates.io as primes.

This package provides an iterator over all primes, generating them lazily as it goes.

The simplest usage is simply to create an Iterator:

use primes::PrimeSet;

let mut pset = PrimeSet::new();

for (ix, n) in pset.iter().enumerate().take(10) {
    println!("Prime {}: {}", ix, n);
}

For more examples, see the full documentation!


lib.rs:

A basic library for finding primes, providing a basic Iterator over all primes. It is not as fast as slow_primes, but it is meant to be easy to use!

The simplest usage is simply to create an Iterator:

use primes::{Sieve, PrimeSet};

let mut pset = Sieve::new();

for (ix, n) in pset.iter().enumerate().take(10) {
println!("Prime {}: {}", ix, n);
}

This library provides methods for generating primes, testing whether a number is prime, and factorizing numbers. Most methods generate primes lazily, so only enough primes will be generated for the given test, and primes are cached for later use.

Source

Example: Find the first prime after 1 million

use primes::{Sieve, PrimeSet};

let mut pset = Sieve::new();
let (ix, n) = pset.find(1_000_000);

println!("Prime {}: {}", ix, n);

Example: Find the first ten primes after the thousandth prime

use primes::{Sieve, PrimeSet};

let mut pset = Sieve::new();
for (ix, n) in pset.iter().enumerate().skip(1_000).take(10) {
println!("Prime {}: {}", ix, n);
}

Example: Find the first prime greater than 1000

use primes::{Sieve, PrimeSet};

let mut pset = Sieve::new();
let (ix, n) = pset.find(1_000);
println!("The first prime after 1000 is the {}th prime: {}", ix, n);

assert_eq!(pset.find(n), (ix, n));

For more info on use, see PrimeSet, a class which encapsulates most of the functionality and has multiple methods for iterating over primes.

This also provides a few functions unconnected to PrimeSet, which will be faster for the first case, but slower in the long term as they do not use any caching of primes.

No runtime deps