1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pub fn eratosthenes_sieve(n: usize) -> Vec<bool> {
    let mut s = vec![true; n];
    s[0] = false;
    s[1] = false;

    for i in 2..n {
        if i * i > n {
            break;
        }
        if s[i] {
            for k in 2..(n + i - 1) / i {
                s[k * i] = false
            }
        }
    }

    s
}

#[cfg(test)]
mod test_eratosthenes_sieve {
    use crate::math::eratosthenes::eratosthenes_sieve;

    #[test]
    fn it_works() {
        let primes = eratosthenes_sieve(10);

        assert!(!primes[1]);
        assert!(primes[2]);
        assert!(primes[3]);
        assert!(!primes[4]);
        assert!(primes[5]);
        assert!(!primes[6]);
        assert!(primes[7]);
        assert!(!primes[8]);
        assert!(!primes[9]);
    }
}