How do I speed up Vec <bool> in my Sith of Eratosthenes?
Why is this straightforward sieve implementation in Rust so slow and how can I speed it up?
use std::num::Float;
use std::iter::range_step;
fn eratosthenes(n: uint) -> Vec<bool> {
let mut sieve = Vec::from_elem(n+1,true);
sieve[0] = false;
sieve[1] = false;
let m = ((n as f64).sqrt()) as uint;
for i in range(0u, m+1) {
if sieve[i] {
for j in range_step(i*i, n+1, i) {
sieve[j] = false;
}
}
}
sieve
}
fn main() {
eratosthenes(100_000_000);
/*
let mut found = Vec::new();
for (i,&sieve) in sieve.iter().enumerate() {
if sieve {
found.push(i);
}
}
println!("Primes found: {}",found.len());
*/
}
PS: I'm not asking about the best algorithm, but how to use Vec (Slice?) Correctly to get it done decently fast!
+3
source to share
No one has answered this question yet
Check out similar questions: