Skip to content

Commit

Permalink
sieve: improve range start in approaches (#1897)
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor authored Apr 9, 2024
1 parent 8c249f3 commit 2c18582
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion exercises/practice/sieve/.approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn primes_up_to(upper_bound: u64) -> Vec<u64> {
(2..numbers.len())
.filter_map(|i| {
let prime = numbers[i].take()? as usize;
(prime + prime..=upper_bound)
(prime * prime..=upper_bound)
.step_by(prime)
.for_each(|j| numbers[j] = None);
Some(prime as u64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn primes_up_to(upper_bound: u64) -> Vec<u64> {
(2..numbers.len())
.filter_map(|i| {
let prime = numbers[i].take()? as usize;
(prime + prime..=upper_bound)
(prime * prime..=upper_bound)
.step_by(prime)
.for_each(|j| numbers[j] = None);
Some(prime as u64)
Expand All @@ -25,7 +25,7 @@ Each number from the range is passed to the [`filter_map()`][filtermap].
The [closure][closure] (also known as a lambda) in the body of the `filter_map()` uses the [`take()`][take] method, combined with the
unwrap operator (`?`), to get the element value in the `Vec` at the index of the number passed in from the range.
If the element value is `None`, then no further processing happens in the lambda for that iteration.
If the element value is `Some` number, then an inner range is defined, starting from the element value plus itself and going through the upper bound.
If the element value is `Some` number, then an inner range is defined, starting from the element value times itself and going through the upper bound.

The [`step_by()`][stepby] method is used to traverse the range in steps the size of the element value.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(2..numbers.len())
.filter_map(|i| {
let prime = numbers[i].take()? as usize;
(prime + prime..=upper_bound)
(prime * prime..=upper_bound)
.step_by(prime)
.for_each(|j| numbers[j] = None);
Some(prime as u64)
Expand Down

0 comments on commit 2c18582

Please sign in to comment.