Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primes_geq and primes_lt generation functions, as well as sieve_geq and sieve_lt. #19

Merged
merged 213 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
213 commits
Select commit Hold shift + click to select a range
36792b5
Add function largest_primes_below
JSorngard Oct 23, 2023
10c4744
Add unit test to largest_primes_below
JSorngard Oct 23, 2023
c09b143
Add early panic in largest_primes_below
JSorngard Oct 23, 2023
e3d8749
Clarify sieve_segment docstring
JSorngard Oct 23, 2023
d57e5bd
Discovered failing test
JSorngard Oct 23, 2023
2311f8a
Fix longrunning test
JSorngard Oct 24, 2023
3ac94ac
Add more examples to docstring of largest_primes_below
JSorngard Oct 24, 2023
f174944
Remove N=1 special case, as that can not succeed. Add test for smalle…
JSorngard Oct 24, 2023
7a5f293
Increase upper_limit in primes_larger_than docstring
JSorngard Oct 24, 2023
cea6fa8
Add note about compile error in const contexts to largest_primes_below
JSorngard Oct 24, 2023
3275ca2
use : before code examples
JSorngard Oct 24, 2023
e1e0196
Add some comments
JSorngard Oct 24, 2023
190a405
Grouped into submodules
JSorngard Oct 25, 2023
07a592d
Begin unifying names, complete `primes_greater_than_or_equal_to`
JSorngard Oct 25, 2023
2670aa5
limit sieve_segment to the crate
JSorngard Oct 25, 2023
727fc55
Finalized the naming convention for specifying output range
JSorngard Oct 25, 2023
ad8de41
rename generation module to generate
JSorngard Oct 25, 2023
9cf3f12
Add sieve_numbers_greater_than_or_equal_to
JSorngard Oct 25, 2023
e98b849
Remove the moebius function as it is out of scope for the crate
JSorngard Oct 26, 2023
2f98d59
Remove panics section that is not needed
JSorngard Oct 27, 2023
9765451
Remove '_numbers' from the names of sieving functions
JSorngard Oct 27, 2023
5ee65ba
Add test for sieve_geq
JSorngard Oct 27, 2023
22ce04b
Make some tests use the const eval
JSorngard Oct 27, 2023
ba516ec
Add example of sieve_geq to crate docstring
JSorngard Oct 27, 2023
64b41c0
Add example of sieve_geq to README
JSorngard Oct 27, 2023
fd8567e
Add benchmark to all prime generation functions
JSorngard Oct 27, 2023
ae2c7bf
Add benchmark to all sieve functions
JSorngard Oct 27, 2023
011aa7b
Name benchmark groups programmatically
JSorngard Oct 27, 2023
9023e5d
Add runtime tests to some functions so that codecov can see it
JSorngard Oct 27, 2023
821475d
Add test of None path to smallest_prime_geq
JSorngard Oct 27, 2023
5e335dd
cargo fmt
JSorngard Oct 27, 2023
6dfe3a3
Correct sieve_segment behaviour when the requested segment starts at …
JSorngard Oct 27, 2023
015a3a8
make next_prime functions always jump ahead
JSorngard Oct 27, 2023
8bfad4c
Improve docstrings of next_prime functions
JSorngard Oct 27, 2023
5b5f9eb
Shorten names
JSorngard Oct 27, 2023
6f69dda
Add panics section to primes_geq docstring
JSorngard Oct 27, 2023
f591acb
Clean up panics section of primes_lt
JSorngard Oct 27, 2023
64e5819
Add comments to compile_fail exampels
JSorngard Oct 27, 2023
92a92f9
Add primality verification to primes_geq test
JSorngard Oct 27, 2023
41c3a7d
Ensured that potential false positives are ignored
JSorngard Oct 27, 2023
52cbd6d
Motivate early exit
JSorngard Oct 27, 2023
d939966
Remove now redundant check
JSorngard Oct 27, 2023
737aa02
grammar
JSorngard Oct 27, 2023
76c9480
move backticks in panic massage of sieve_lt
JSorngard Oct 27, 2023
24025e2
Add note about assumtions in sieve_segment
JSorngard Oct 27, 2023
13a0371
Remove unused imports in crate docstring examples
JSorngard Oct 27, 2023
2e8272c
Add #[must_use]
JSorngard Oct 27, 2023
9e715e1
rename next_prime module to other_prime
JSorngard Oct 27, 2023
1a4d8ef
Update documentation string of crate and README
JSorngard Oct 27, 2023
e4f7e43
Add array_section module
JSorngard Oct 29, 2023
53c5e81
Use a newtype for RestrictedArray IntoIter
JSorngard Oct 29, 2023
8b1add3
Simplify docstring
JSorngard Oct 29, 2023
86c840b
grammar
JSorngard Oct 29, 2023
b6b6be7
Add conditional impl of Eq
JSorngard Oct 29, 2023
1a70333
rename array_section to restricted_array
JSorngard Oct 29, 2023
823f7d2
Add restricted_array to the crate as a module
JSorngard Oct 29, 2023
48f7986
cargo fmt
JSorngard Oct 29, 2023
94a1c2e
Change order of generic paramters to match array
JSorngard Oct 29, 2023
cd29f38
PARTIAL COMMIT, primes_geq fails some tests
JSorngard Oct 29, 2023
5a13163
Shorten docstring of enum variants
JSorngard Oct 30, 2023
f41eeea
Talk about RestrictedArray in terms of visiblillity
JSorngard Oct 30, 2023
c5e5daf
Improve docstring for RestrictedArrayIntoIter
JSorngard Oct 30, 2023
1e38b89
Add note about comparisons to RestirctedArray docstring
JSorngard Oct 30, 2023
9445e49
Refactor result
JSorngard Nov 2, 2023
05a68b6
smallest_prime_geq --> next_prime
JSorngard Nov 2, 2023
7d843a9
rename largest_prime_leq to previous_prime
JSorngard Nov 2, 2023
52f4f2b
Improve some docstrings
JSorngard Nov 3, 2023
cfbc1ac
Add std feature
JSorngard Nov 3, 2023
a2e7086
rename std --> alloc
JSorngard Nov 3, 2023
642a535
Remove alloc feature
JSorngard Nov 3, 2023
22f7d10
Add keywords field
JSorngard Nov 20, 2023
baa6e10
change keyword 'prime' to 'primes'
JSorngard Dec 5, 2023
47719ce
move clone and copy into derive
JSorngard Dec 27, 2023
df305d9
Move Eq impl to derive
JSorngard Dec 27, 2023
c9cf0c8
impl IntoIterator for &RestrictedArray
JSorngard Feb 19, 2024
1f402c4
impl IndexMut for RestrictedArray
JSorngard Feb 19, 2024
1f0e96b
Add as_slice_mut
JSorngard Feb 19, 2024
c9628b9
implement Intex(Mut) in terms of as_slice(_mut)
JSorngard Feb 19, 2024
83d1862
Add where clause for signature looks
JSorngard Feb 19, 2024
d391e9e
implement size_hint in terms of len
JSorngard Feb 19, 2024
5edb8a2
Remove mutable access
JSorngard Feb 19, 2024
8a2565f
Add manual nth impl
JSorngard Feb 22, 2024
a54ad94
Inline into_iter
JSorngard Feb 22, 2024
de89b3a
Rename RestrictedArray, impl Index<Ranges>
JSorngard Apr 26, 2024
b476af2
minor doc and name tweaks
JSorngard Apr 26, 2024
b220964
Be explicit in impl of index<usize>
JSorngard Apr 26, 2024
86e41cb
Use arraysectioniter also for into_iter(&self)
JSorngard Apr 26, 2024
824d16d
Simplify impl of &self.into_iter()
JSorngard Apr 26, 2024
db284df
Add newline before headings in docstrings
JSorngard Apr 26, 2024
08f6f80
Statically assert N>0 using inline const
JSorngard Apr 26, 2024
80e284f
Add info about new panic behavior to type docstring
JSorngard Apr 26, 2024
60b0865
Add space before header in docstring
JSorngard Apr 26, 2024
066f074
Add newline around headings in docstrings
JSorngard Apr 26, 2024
c89e125
Add newline around headings in docstrings
JSorngard Apr 26, 2024
da8e144
Fail to compile in primes if N = 0
JSorngard Apr 26, 2024
73a6fb5
Compile fail in all sieve functions if N == 0
JSorngard Apr 26, 2024
35d086a
Document compile fail when N=0
JSorngard Apr 26, 2024
7eb8d64
Document compile fail in generation functions
JSorngard Apr 26, 2024
60c9e1c
Fix compile errors of tests
JSorngard Apr 26, 2024
57e5511
impl Default when N>0
JSorngard Apr 26, 2024
e73fd43
Make primes_geq set lower_limit to 2 if it was lower
JSorngard Apr 26, 2024
43089c9
primes_lt return error if upper_limit <= 2
JSorngard Apr 26, 2024
5bbe7a9
Specify type link for partial_ok
JSorngard Apr 26, 2024
254f345
impl Hash for ArraySection and SegmentedGenerationError
JSorngard Apr 26, 2024
0b3c719
Add start and end to ArraySection
JSorngard Apr 26, 2024
ef2e881
Correct left to right in docstring of end
JSorngard Apr 26, 2024
f5e4410
Link between start and end
JSorngard Apr 26, 2024
109fe61
Shorten core::ops::Index with use statement
JSorngard Apr 26, 2024
37625d9
Specify full path of ArraySection in macro
JSorngard Apr 26, 2024
4d3ce43
Simplify Index<usize> imple
JSorngard Apr 26, 2024
4c83e56
Fixed final test, on to doctests
JSorngard Apr 28, 2024
1d4d07e
Add const assert to prime_counts
JSorngard Apr 28, 2024
21bfd9a
Fixed some doc tests
JSorngard Apr 28, 2024
44d0cbd
Rename SegmentedGeneration* to *
JSorngard Apr 28, 2024
8c20088
Impl PartialOrd and Ord for ArraySection
JSorngard Apr 30, 2024
def01ae
Implement non-marker traits manually for ArraySection
JSorngard Apr 30, 2024
1e5ae57
Import Hash and Hasher, reorder PartialOrd and Ord impl blocks
JSorngard Apr 30, 2024
f61bd5e
#[inline all the things]
JSorngard Apr 30, 2024
0985872
Oneliner :3
JSorngard Apr 30, 2024
866158b
impl is_empty with slice::is_empty
JSorngard Apr 30, 2024
14de1d4
impl AsRef<[T]> for ArraySection
JSorngard Apr 30, 2024
dbdbe3a
Remove duplicate impl blocks
JSorngard Apr 30, 2024
aa8e06f
Add as_full_array_mut
JSorngard Apr 30, 2024
2382ade
impl PartialOrd and PartialEq for different Ns
JSorngard Apr 30, 2024
4d0200f
Add examples to ArraySection
JSorngard Apr 30, 2024
fd6c373
Fix rustdoc error messages
JSorngard Apr 30, 2024
b535d9a
only do log2(n) once
JSorngard Apr 30, 2024
5e559d4
% -> mod in math
JSorngard Apr 30, 2024
05408b0
Almost completely impl PrimesArray
JSorngard Apr 30, 2024
8bc620c
impl PartialEq with slices for PrimesArray
JSorngard Apr 30, 2024
866d9d9
Just stack overflows left
JSorngard Apr 30, 2024
8aeb16a
Add TODO
JSorngard Apr 30, 2024
281882c
Fix primes_lt
JSorngard May 2, 2024
c23dfd5
TODO: move only large enough primes into the array
JSorngard May 2, 2024
26cface
Fix primes_geq
JSorngard May 2, 2024
1686ba8
Comment formatting
JSorngard May 2, 2024
b26f79c
Rename TooLargeN to MEMSquaredOverflow
JSorngard May 2, 2024
b36af3f
Correct display impl of Error for the TooLargeLimit variant
JSorngard May 2, 2024
1f4e9be
Remove ominous no guarantee text
JSorngard May 2, 2024
76fa4d3
Remove ability to edit ArraySection, the point is invariant preervation
JSorngard May 2, 2024
b877131
Fix broken doc links
JSorngard May 2, 2024
7d4bed8
Add iter function and impl IntoIterator for PrimesArray
JSorngard May 2, 2024
25f486f
Remove broken const annotation
JSorngard May 2, 2024
f15d2d7
Override default size_hint for PrimesArrayIntoIter
JSorngard May 2, 2024
69e47a1
Define iterator impl region
JSorngard May 2, 2024
12e1886
inline new PrimesArrayIter
JSorngard May 2, 2024
a254287
Make PartialEq<[T; N]> for PrimesArray symmetric
JSorngard May 2, 2024
3581ea0
computed --> populated in doctext
JSorngard May 2, 2024
bd61c12
Clarify ArraySection docstring
JSorngard May 2, 2024
1f99b77
Add note to Hash impl
JSorngard May 2, 2024
73981dc
Add fallible conversions into array for ArraySection
JSorngard May 2, 2024
98c04d7
Add note about sections to Ord impl
JSorngard May 2, 2024
e974051
Inline TryFrom stuffs
JSorngard May 2, 2024
45280b3
Reshape return type of suffixed generation functions
JSorngard May 3, 2024
25bf22d
Improve documentation of primes_geq
JSorngard May 3, 2024
881e2ab
Better math notation
JSorngard May 3, 2024
4b1fb6a
Show usage of primes_geq
JSorngard May 3, 2024
805c05a
Fix impl of Error for generation::Error
JSorngard May 3, 2024
24448aa
Simplify docstring
JSorngard May 3, 2024
da22f4e
close paren
JSorngard May 3, 2024
62e2e65
Better format of primes_geq demo
JSorngard May 3, 2024
542753b
Solve mem problem with macro
May 3, 2024
a14ca68
Add primes() to const_primes!()
JSorngard May 4, 2024
ed92250
Only compute MEM^2 once
JSorngard May 5, 2024
6d71375
rustfmt let else
JSorngard May 5, 2024
0587e03
call index in index
JSorngard May 5, 2024
f96035c
Impl all indexing operations with one generic impl instead of many ex…
JSorngard May 5, 2024
8bce044
use statement, and remove where
JSorngard May 5, 2024
4b3f21b
inline more trivial things
JSorngard May 5, 2024
4d99fc0
Clearer variable name in example
JSorngard May 5, 2024
250fe92
impl From<TryFromArraySectionError> for ArraySection
JSorngard May 5, 2024
8e9c2ce
it must be pub
JSorngard May 5, 2024
345f637
Add docstring to array_section
JSorngard May 5, 2024
4ef0691
clarity in docstring
JSorngard May 5, 2024
97ea444
Remove double doc-links
JSorngard May 5, 2024
0064cdb
impl into_full_array_const for ArraySection
JSorngard May 5, 2024
6f6aad5
rename ArraySection::new range argument
JSorngard May 5, 2024
ad52430
Simplify doctest
JSorngard May 5, 2024
0ee8c80
Clarify what the result type is in doctests
JSorngard May 5, 2024
bfcd802
remove i
JSorngard May 5, 2024
48ab231
31 -> 63
JSorngard May 5, 2024
1b34058
Simplify module structure
JSorngard May 5, 2024
deebe76
Remove PrimesArray
JSorngard May 5, 2024
cf13908
more docstrings
JSorngard May 5, 2024
43c8eae
Split ArraySection into its own crate
JSorngard May 5, 2024
61fd56a
Docstring formatting
JSorngard May 5, 2024
d64b58c
Add note of runtime use to const_primes!
JSorngard May 6, 2024
96973ab
improve docstring
JSorngard May 6, 2024
8307023
Merge branch 'main' into largest_primes_below
JSorngard May 6, 2024
c6a2eef
Add categories 'Mathematics' and 'No standard library::no dynamic all…
JSorngard May 6, 2024
c3177f5
Add alloc features
JSorngard May 6, 2024
eb94512
Update README.md
JSorngard May 6, 2024
4af5454
Add features to README
JSorngard May 6, 2024
0d7dabc
newline
JSorngard May 6, 2024
de8dd9e
The readme can't doclink
JSorngard May 6, 2024
7b55a59
Ignore rust-toolchain.toml, I just want my vscode to stop yelling at me
JSorngard May 6, 2024
d189cb6
fix benchmark code
JSorngard May 6, 2024
472ee92
Simplify API
JSorngard May 7, 2024
11a52e9
Use heuristic to estimate geq memory requirements
JSorngard May 7, 2024
2f63369
const_primes! -> primes_segment!
JSorngard May 7, 2024
eb0b14e
Add test to primes_segment
JSorngard May 7, 2024
8be89a4
Simplify test code
JSorngard May 7, 2024
b4599d9
Add note about overestimating sieve size
JSorngard May 7, 2024
e13fdae
Clarify docstirng
JSorngard May 7, 2024
4c557e7
move 3 into const N
JSorngard May 7, 2024
4ddcadd
use isqrt in primes_segment!
JSorngard May 7, 2024
3c5c687
and -> or
JSorngard May 7, 2024
2185d55
Clearer error in docstring of primes_geq
JSorngard May 7, 2024
9f69a8f
docstring tweaks of primes_lt
JSorngard May 7, 2024
a7f8aaa
Force nightly for now
JSorngard May 7, 2024
1d7b69c
All hail clippy
JSorngard May 7, 2024
64ba7c9
Remove commented line
JSorngard May 7, 2024
f820bba
primes_geq docstring tweaks
JSorngard May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
/Cargo.lock
/Cargo.lock
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ authors = ["Johanna Sörngård (jsorngard@gmail.com)"]
version = "0.4.8"
edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["primes", "const"]
categories = ["mathematics", "no-std::no-alloc"]
description = "Generate and work with prime numbers in const contexts"
repository = "https://github.com/JSorngard/const-primes"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[dev-dependencies]
criterion = {version = "0.5", features = ["html_reports"]}
criterion = { version = "0.5", features = ["html_reports"] }
rand = "0.8"

[features]
std = [ "alloc"]
alloc = []

[[bench]]
name = "prime_benches"
harness = false
harness = false
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ A crate for generating and working with prime numbers in const contexts.
`#![no_std]` compatible.

## Examples
Generate arrays of prime numbers with the function `primes` which uses a [segmented sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Segmented_sieve).
Generate arrays of prime numbers with the function `primes` which uses a [segmented sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Segmented_sieve):
```rust
const PRIMES: [u32; 10] = primes();
assert_eq!(PRIMES[5], 13);
assert_eq!(PRIMES, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
```
or with the type `Primes` which ensures that a non-zero number of primes are generated:
or with the wrapping type [`Primes`]:
```rust
const PRIMES: Primes<10> = Primes::new();
assert_eq!(PRIMES[5], 13);
Expand All @@ -40,30 +40,54 @@ assert_eq!(PRIMES_LEQ_100, Some(25));
assert!(CACHE.is_prime(1000).is_none());
assert!(CACHE.count_primes_leq(1000).is_none());
```
Creating a `Primes<0>` is a compile fail in const contexts and a panic otherwise.

### Other functionality
Use `is_prime` to test whether a given number is prime
```rust
const CHECK: bool = is_prime(18_446_744_073_709_551_557);
assert!(CHECK);
```
or `are_prime` to compute the prime status of the `N` first integers,
Sieve a range of numbers for their prime status with `sieve`:
```rust
const N: usize = 10;
const PRIME_STATUS: [bool; N] = are_prime();
const PRIME_STATUS: [bool; N] = sieve();
// 0 1 2 3 4 5 6 7 8 9
assert_eq!(PRIME_STATUS, [false, false, true, true, false, true, false, true, false, false]);
```

## Arbitrary ranges
The crate provides prime generation and sieving functions with suffixes, e.g. `primes_geq` and `sieve_lt`
that can be used to work with ranges that don't start at zero. They take two generics: the number of elements
to store in the binary and the size of the sieve used during evaluation. The sieve size must be the cieling
of the square root of the largest encountered value:
```rust
// ceil(sqrt(5_000_000_063)) = 70_711
const PRIMES_GEQ: const_primes::Result<3> = primes_geq::<3, 70_711>(5_000_000_031);
assert_eq!(PRIMES_GEQ?, [5_000_000_039, 5_000_000_059, 5_000_000_063]);
```
or `are_prime_below` to compute the prime status of the `N` largest integers below a given value,
```rust
const N: usize = 70711;
const BIG_PRIME_STATUS: [bool; N] = are_prime_below(5_000_000_031);
// 5_000_000_028 5_000_000_029 5_000_000_030
assert_eq!(BIG_PRIME_STATUS[N - 3..], [false, true, false]);
const PRIME_STATUS_LT: [bool; N] = sieve_lt(5_000_000_031);
// 5_000_000_028 5_000_000_029 5_000_000_030
assert_eq!(PRIME_STATUS_LT[N - 3..], [false, true, false]);
```
The sieving functions have yet to be modified for two generics, and must save the entire sieve in the binary.
## Other functionality
Use `is_prime` to test whether a given number is prime:
```rust
const CHECK: bool = is_prime(18_446_744_073_709_551_557);
assert!(CHECK);
```
Find the next or previous prime numbers with `next_prime` and `previous_prime` if they exist:
```rust
const NEXT: Option<u64> = next_prime(25);
const PREV: Option<u64> = previous_prime(25);
const NOSUCH: Option<u64> = previous_prime(2);

assert_eq!(NEXT, Some(29));
assert_eq!(PREV, Some(23));
assert_eq!(NOSUCH, None);
```
and more!

## Features

`std`: derives the `Error` trait for the error types.
`alloc`: enables conversion of the type returned by `primes_geq` and `primes_lt` into `Vec`s and `Box`ed slices.

## License

Licensed under either of
Expand Down
73 changes: 45 additions & 28 deletions benches/prime_benches.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
use const_primes::{are_prime, is_prime, moebius, primes};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use const_primes::{is_prime, primes, primes_geq, primes_lt, sieve, sieve_geq, sieve_lt};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use rand::prelude::*;
use std::hint::black_box;

fn benchmarks(c: &mut Criterion) {
c.bench_function("generate 10000 primes", |b| {
b.iter(|| black_box(primes::<10_000>()))
});
{
const N: usize = 10_000;
let mut prime_generation = c.benchmark_group("prime generation");
prime_generation.bench_function(format!("first {N} primes"), |b| {
b.iter(|| black_box(primes::<N>()))
});
prime_generation.bench_function(format!("{N} primes < 100000000"), |b| {
b.iter(|| black_box(primes_lt::<N, N>(100000000)))
});
prime_generation.bench_function(format!("{N} primes >= 99990000"), |b| {
b.iter(|| black_box(primes_geq::<N, N>(99990000)))
});
}

c.bench_function("is_prime on random numbers", |b| {
b.iter_batched(
|| (0..10_000).map(|_| random()).collect::<Vec<u64>>(),
|data| {
for number in data.iter() {
black_box(is_prime(*number));
}
},
BatchSize::SmallInput,
)
});
{
const N: u64 = 10_000;
let mut rng = StdRng::seed_from_u64(1234567890);
let mut primality_testing = c.benchmark_group("primality testing");
primality_testing.throughput(Throughput::Elements(N));
primality_testing.bench_function(format!("is_prime on {N} random numbers"), |b| {
b.iter_batched(
|| (0..N).map(|_| rng.gen()).collect::<Vec<u64>>(),
|data| {
for number in data.iter() {
black_box(is_prime(*number));
}
},
BatchSize::SmallInput,
)
});
}

c.bench_function("sieve 10000 integers", |b| {
b.iter(|| black_box(are_prime::<10_000>()))
});

let ints: Vec<_> = (1..1_000_000).map(|n| n).collect();
c.bench_function("möbius of first 1e6 integers", |b| {
b.iter(|| {
for &i in &ints {
black_box(moebius(i));
}
})
});
{
const N: usize = 10_000;
let mut sieving = c.benchmark_group("prime sieving");
sieving.bench_function(format!("first {N} integers"), |b| {
b.iter(|| black_box(sieve::<N>()))
});
sieving.bench_function(format!("{N} integers < 100000000"), |b| {
b.iter(|| black_box(sieve_lt::<N>(100000000)))
});
sieving.bench_function(format!("{N} integers >= 99990000"), |b| {
b.iter(|| black_box(sieve_geq::<N>(99990000)))
});
}
}

criterion_group!(benches, benchmarks);
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
Loading
Loading