Skip to content

Commit

Permalink
Removed the multithreading helper of is_small_prime_small_test. (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfpf authored May 4, 2024
1 parent 0e3efe3 commit 39d8d32
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,15 @@ mod tests {
reader.lines().map(|line| line.unwrap())
}

/// Convenience to count the number of prime numbers in a given range using
/// multiple threads.
///
/// * `lower` - First number of the range (inclusive).
/// * `upper` - Last number of the range (exclusive).
/// * `pieces` - Number of threads to use.
///
/// -> Number of primes numbers in the range.
fn primes(lower: i64, upper: i64, pieces: i64) -> usize {
#[cfg(target_pointer_width = "64")]
#[test]
fn is_prime_small_test() {
// Break the range into equal sub-ranges, and count the number of
// primes in each sub-range in a separate thread.
let (lower, upper, pieces) = (0, 3i64.pow(20), 3);
let search_space = upper - lower;
let search_space = search_space / pieces + if search_space % pieces == 0 { 0 } else { 1 };
(lower..upper)
let num_of_primes = (lower..upper)
.step_by(search_space as usize)
.map(|lower| {
let upper = std::cmp::min(lower + search_space, upper);
Expand All @@ -282,13 +279,7 @@ mod tests {
.collect::<Vec<_>>()
.into_iter()
.map(|worker| worker.join().unwrap())
.sum()
}

#[cfg(target_pointer_width = "64")]
#[test]
fn is_prime_small_test() {
let num_of_primes = primes(0, 3i64.pow(20), 3);
.sum::<usize>();
assert_eq!(num_of_primes, 166677978);
}

Expand Down

0 comments on commit 39d8d32

Please sign in to comment.