prime_data/
lib.rs

1#![deny(missing_docs)]
2/*!
3# Prime Data
4
5Welcome! This module is a personal project of mine to generate and store prime numbers in a way that's
6both memory efficient and time efficient.
7
8## What's this?
9
10This crate has a user guide, which is quite recommended if you want to learn more about keeping the
11efficiency of the Sieve of Eratosthenes, but increasing the memory efficiency of storing and generating
12primes. Click [here](crate::guide) if you wish to read it.
13
14However, if you just want to know how to utilize this crate, read below:
15
16# TL;DR / How to use this library
17
18When you import this crate into your project, you will have, by default, access to the following tools:
19
20Let's say you want to know what are all the prime numbers between 100 and 200:
21
22```
23let primes = prime_data::PrimeData::generate(100..=200);
24```
25
26You can easily generate that data with the [generate](crate::PrimeData::generate) method. By design,
27every method or function that requires some range will be an [inclusive range](std::ops::RangeInclusive).
28This is just because of a few conventions involving prime numbers. For example, in mathematics, when we
29want to count "the amount of prime numbers from 1 to x", we usually want to include x, if it is prime.
30
31With that, you can do some cool things with it:
32
33```
34# let primes = prime_data::PrimeData::generate(100..=200);
35// You can iterate over all those prime numbers
36for prime in primes.iter_all() {
37    println!("{} is a prime number!", prime);
38}
39
40// Therefore, you can collect them into a vector
41println!("{:?}", primes.iter_all().collect::<Vec<_>>());
42
43// You don't have to iterate through all of them. Maybe you just want primes within some range
44// Naturally, you can collect those into a vector too
45for prime in primes.iter(161..=179) {
46    println!("{p} is a prime number and 161 <= {p} <= 179", p = prime);
47}
48
49// If you just want to know how many there are
50println!("There are {} primes between 100 and 200.", primes.count_primes());
51
52// ...within a range
53println!("There are {} primes between 161 and 179.", primes.count_primes_in_range(161..=179));
54```
55
56However, there are some handy public methods, that abstract you from the need of generating any data:
57
58```
59// You can verify if a number is prime
60println!("2027 is {}", if prime_data::is_prime(2_027) { "prime!" } else { "not prime!" });
61
62// You can count the amount of primes from 1 to some bound
63println!("There are {} primes from 1 to 1024", prime_data::count_primes(1024));
64```
65
66# Features
67
68You'll get more functionality by including features in the dependency:
69
70## `"factors"`
71
72The **factors** feature includes a struct and a public method for factorizing numbers.
73
74```
75# #[cfg(feature = "factors")] {
76// from some prime data...
77let data = prime_data::PrimeData::generate(0..=12);
78
79// you can factorize numbers
80let factors = data.factorize(120);
81
82// from that, you can retrieve the original number
83println!("The prime factorization of {} is:", factors.as_u64());
84// retrieve the prime factors as tuples, where each tuple is of the form
85// (prime, amount) meaning prime.pow(amount)
86println!("{:?}", factors.as_tuples());
87
88// or alternatively, you can retrieve a list of all factors
89let all_factors = factors.all_factors();
90println!("All factors are: {:?}", all_factors);
91println!("120 has {} factors in total.", all_factors.len());
92
93// you can also convert a u64 into its factorization
94let factorized_44 = prime_data::Factorization::from(44);
95println!("The factors of 44 are {:?}", factorized_44.all_factors());
96
97// finally, if you only need to list a number's factors once:
98println!("The factors of 490 are {:?}", prime_data::all_factors_of(490));
99# }
100
101*/
102
103pub use data::*;
104mod data;
105
106pub mod guide;