Skip to content

Commit

Permalink
Implement sieve_lt, sieve_geq and sieve_segment, as well as the…
Browse files Browse the repository at this point in the history
…ir supporting types (#21)

* sieve_geq::<N, MEM>(limit)

* Remove redundant doclink

* Add docstring to SieveError

* Clearer comments and document MEM >= N

* sieve_lt and sieve_geq have no currently known bugs

* fix bugs in benchmarks

* Fix potential bugs in primes_segment!

* Formatting

* Improve docstring examples for sieve_geq

* Clarify usage of size_lt and sieve_geq

* Return the right error variant

* Add example that shows SieveError::TotalDoesntFitU64

* plural examples

* Remove state from GenerationError

* Clarify SieveOverrun in docstring

* Correct error in SieveOverun docstring

* Correct error in TooLargeLimit docstring

* Clarify estimation of sieve size in primes_lt and primes_geq

* Clarify MEM computation in sieve_lt

* Clarify MEM computation in lib.rs docstring

* Remove disclaimer for now fixed feature!

* Implement sieve_segment
  • Loading branch information
JSorngard authored May 7, 2024
1 parent 29124b8 commit 98986b1
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 205 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ to store in the binary and the size of the sieve used during evaluation. The sie
of the square root of the largest encountered value:
```rust
// ceil(sqrt(5_000_000_063)) = 70_711
const PRIMES_GEQ: Result<[u64; 3], const_primes::Error> = primes_geq::<3, 70_711>(5_000_000_031);
const PRIMES_GEQ: Result<[u64; 3], GenerationError> = primes_geq::<3, 70_711>(5_000_000_031);
assert_eq!(PRIMES_GEQ, Ok([5_000_000_039, 5_000_000_059, 5_000_000_063]));
```
```rust
const N: usize = 70711;
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]);
const PRIME_STATUS_LT: Result<[bool; N], SieveError> = sieve_lt::<3, 70_711>(5_000_000_031);
// 5_000_000_028 5_000_000_029 5_000_000_030
assert_eq!(PRIME_STATUS_LT, Ok([false, true, false]));
```
The sieving functions have yet to be modified for two generics, and must save the entire sieve in the binary.
The sieve size can be computed by the crate by using the macro `primes_segment!` and `sieve_segment!`.
## Other functionality
Use `is_prime` to test whether a given number is prime:
```rust
Expand Down
4 changes: 2 additions & 2 deletions benches/prime_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ fn benchmarks(c: &mut Criterion) {
b.iter(|| black_box(sieve::<N>()))
});
sieving.bench_function(format!("{N} integers < 100000000"), |b| {
b.iter(|| black_box(sieve_lt::<N>(100000000)))
b.iter(|| black_box(sieve_lt::<N, N>(100000000)))
});
sieving.bench_function(format!("{N} integers >= 99990000"), |b| {
b.iter(|| black_box(sieve_geq::<N>(99990000)))
b.iter(|| black_box(sieve_geq::<N, N>(99990000)))
});
}
}
Expand Down
40 changes: 0 additions & 40 deletions src/error.rs

This file was deleted.

Loading

0 comments on commit 98986b1

Please sign in to comment.