You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
761 B
37 lines
761 B
2 years ago
|
// 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);
|
||
|
}
|