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.
Using this library
Add the following to your Cargo.toml
file
[]
= "0.1.2"
Examples
Basic Usage:
use Primes;
// Provides an iterator for all prime numbers less than or equal to 1000
let mut p = new;
Once the structure has been initiated, the primes()
method provides an iterator
for the prime numbers.
# use Primes;
let p = new; // Primes less than or equal to 10
let mut prime_iter = p.primes;
let primes: = prime_iter.copied.collect;
let expected: = ;
assert_eq!;
Since primes()
returns an iterator, you can also use it to directly find a specific
prime number.
# use Primes;
let p = new; // 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 ;
Methods are also available to find the prime factors of a number, and whether a number is prime or composite.
use ;
let p = new;
let n = 96;
match p.factors ;
let n = 23;
match p.primality ;