summaryrefslogtreecommitdiff
path: root/p357/src/main.rs
blob: 14a5d02e451d8acd9327739fdd2d2cf5e70fc5bd (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const MAX_PRIMES: usize = 200_000_000;
const MAX_DIV_PRIMES: usize = 100_000_000;
const SQRT_MAX_PRIMES: usize = 14_142;

fn main() {
    let mut primes = vec![true;MAX_PRIMES];

    primes[0] = false;
    primes[1] = false;

    for i in 2..SQRT_MAX_PRIMES {
        if primes[i] {
            let mut j = i*i;
            while j < MAX_PRIMES {
                primes[j] = false;
                j += i;
            }
        }
    }

    let mut cnt = 0;
    for i in &primes {
        if *i {
            cnt += 1;
        }
    }
    println!("Count is {}", cnt);

    let mut sum: u64 = 0;
    for i in 1..MAX_DIV_PRIMES {
        if primes[i+1] {
            let mut ok = true;

            let mut j = 1;
            while j*j <= i {
                if i % j == 0 {
                    if !primes[j + i/j] {
                        ok = false;
                        break;
                    }
                }
                j += 1;
            }
            if ok {
                sum += i as u64;
             }
        }
    }
    println!("Sum is {}", sum);


}