From 5c8c7baa75118524c122b42ca966f2d0746ce590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 4 Mar 2022 15:48:00 +0100 Subject: [PATCH] Parallelize prime testing. --- src/test.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/test.rs b/src/test.rs index fedd886..a26e964 100644 --- a/src/test.rs +++ b/src/test.rs @@ -17,6 +17,7 @@ */ use std::collections::VecDeque; +use rayon::prelude::*; pub fn is_prime_f(n:u64, b:u64) -> bool { @@ -47,14 +48,9 @@ pub fn is_prime_f(n:u64, b:u64) -> bool } let limit = (n as f64).sqrt() as u64 + 1; - let mut res = true; - (start..limit).step_by(2).for_each(|x| { - if n % x == 0 - { - res = false; - } - }); - return res; + let compound = (start..limit).step_by(2).collect::>() + .par_iter().any(|x| n % x == 0); + return !compound; } pub fn is_prime(n:u64) -> bool @@ -65,12 +61,8 @@ pub fn is_prime(n:u64) -> bool pub fn is_prime_mem(n:u64, primes:&VecDeque) -> bool { let limit = (n as f64).sqrt() as u64; - let mut res = true; - primes.iter().take_while(|x| **x <= limit).for_each(|x| { - if n % *x == 0 - { - res = false; - } - }); - return res; + let pp = primes.partition_point(|x| *x < limit); + //let compound = primes.par_iter().take(pp).any(|x| n % x == 0); + let compound = primes.iter().take(pp).any(|x| n % x == 0); + return !compound; }