use std::io; use std::str::FromStr; fn main() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let mut it = buf.split_whitespace().map(|n| usize::from_str(n).unwrap()); let n = it.next().unwrap(); let era = Eratosthenes::new(n as u32); let ans = (1..=n).filter(|x| era.is_prime(*x as u32)).fold(0, |acc, x| acc + x); println!("{}", ans); } struct Eratosthenes { table: Vec, } impl Eratosthenes { fn new(max: u32) -> Self { let mut table: Vec = (0u32..=max).collect(); for i in (2..=max).step_by(2) { table[i as usize] = 2; } (3u32..) .step_by(2) .take_while(|p| p * p <= max as u32) .for_each(|p| { if table[p as usize] < p { return; } for x in (p..=max as u32).step_by(p as usize) { table[x as usize] = p; } }); Self { table } } fn prime_factors(&self, n: u32) -> Vec { let mut res = Vec::new(); let mut now = n as usize; while self.table[now] != 1 { res.push(self.table[now]); now /= self.table[now] as usize; } res } fn is_prime(&self, n: u32) -> bool { if n == 1 { return false; } self.table[n as usize] == n } }