diff options
Diffstat (limited to 'p357/src/main.rs')
-rw-r--r-- | p357/src/main.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/p357/src/main.rs b/p357/src/main.rs new file mode 100644 index 0000000..14a5d02 --- /dev/null +++ b/p357/src/main.rs @@ -0,0 +1,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); + + +} |