#prime #factor #prime-factors #eratosthenes

prime_tools

Generate primes, get prime factors, check primality, and other useful prime-related utilities

8 releases

0.3.4 Oct 6, 2020
0.3.3 Oct 6, 2020
0.2.2 Oct 19, 2019
0.1.3 Oct 15, 2019

#1975 in Math

Download history 46/week @ 2024-08-31 38/week @ 2024-09-07 29/week @ 2024-09-14 65/week @ 2024-09-21 52/week @ 2024-09-28 11/week @ 2024-10-05 16/week @ 2024-10-12 17/week @ 2024-10-19 28/week @ 2024-10-26 36/week @ 2024-11-02 12/week @ 2024-11-09 14/week @ 2024-11-16 37/week @ 2024-11-23 37/week @ 2024-11-30 69/week @ 2024-12-07 55/week @ 2024-12-14

201 downloads per month
Used in 5 crates (2 directly)

MIT license

15KB
280 lines

prime_tools Crate Build Status

This util provides a few tools for working with prime numbers.

Mostly for personal use with project euler problems. :)

fn get_primes_less_than_x(x: u32) -> Vec<u32>

Generates an ordered list of prime numbers from 2 up to x (exclusive).

Uses the sieve of Eratosthenes under the covers.

fn get_prime_factors_with_counts(x: u32, primes: &Vec<u32>) -> HashMap<u32, u32>

To be used in conjunction with get_primes_less_than_x. Be sure to pass in primes at least up to sqrt(x).

fn is_u32_prime(x: u32) -> bool

Figures out if x is prime. This is fast! I've benchmarked it at 2.7 seconds to process 1 million random u32s.

fn is_u64_prime(x: u64) -> bool

Figures out if x is prime. This is pretty slow: I've benchmarked it at 26 seconds to process only 200 random u64s. :(

fn get_primes_between(min: u64, max: u64) -> Vec<u64>

Generates primes between min (inclusive) and max (exclusive). Uses a modified sieve of eratosthenes.

WARNING #1: This can be very slow if the max is greater than 10^17 ish, or with too large a range.

WARNING #2: This will break if the max is too much higher than 10^19 ish

Dependencies

~470–700KB
~10K SLoC