Coin de Gamma
2 years ago
1 changed files with 36 additions and 0 deletions
@ -0,0 +1,36 @@
|
||||
// based on ../007/main.rs
|
||||
|
||||
const SIZE: usize = 2000000; // 10 as test should be =17
|
||||
type Rs = [bool; SIZE + 1]; |
||||
|
||||
fn remove_divs(m: usize, xs: &mut Rs) { |
||||
let mut i = m + m; |
||||
while i <= SIZE { |
||||
xs[i] = false; |
||||
i += m; |
||||
} |
||||
} |
||||
|
||||
fn get_next_p(m_from: usize, xs: &Rs) -> Option<usize> { |
||||
for k in (m_from + 1)..=SIZE { |
||||
if xs[k] { return Some(k); }
|
||||
} |
||||
return None; |
||||
} |
||||
|
||||
fn main() { |
||||
let mut xs: Rs = [true; SIZE + 1]; |
||||
let mut sum = 0usize; |
||||
let mut last_p = 1; |
||||
loop { |
||||
match get_next_p(last_p, &xs) { |
||||
Some(p) => { |
||||
sum += p; |
||||
last_p = p; |
||||
}, |
||||
None => break
|
||||
} |
||||
remove_divs(last_p, &mut xs); |
||||
} |
||||
println!("sum: {}", sum); |
||||
} |
Loading…
Reference in new issue