Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

find prime numbers with the sieve of Eratosthenes

fn sieve(in_n: u32) -> Vec<u32> {
    let n = in_n as usize;
    let mut a = Vec::new();
    a.resize(n, true);
    for i in 2..(f64::sqrt(n as f64) as usize + 1) {
        if a[i] {
            for j in (i*i..n).step_by(i) {
                a[j] = false;
            }
        }
    }
    return a.iter().enumerate().filter_map(|(i, &t)| {
      if t {Some(i as u32)} else {None}   
    }).collect();
}

fn main() {
    let n = 12;
    println!("List of primes less than {} = {:?} ", n, sieve(n));
}
Source by en.wikipedia.org #
 
PREVIOUS NEXT
Tagged: #find #prime #numbers #sieve #Eratosthenes
ADD COMMENT
Topic
Name
5+2 =