// Copyright (C) 2015 by Alexandru Cojocaru // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . use std::collections::BitSet; use std::f64; // use std::iter::range_step; fn to_index(n: u64) -> usize { ((n-3)/2) as usize } fn from_index(i: usize) -> u64 { (i*2 + 3) as u64 } fn primes(n: u64) -> Vec { let mut p = BitVec::from_elem(to_index(n) + 1, false); for i in (3..f64::sqrt(n as f64) as u64 + 1).step_by(2) { for j in (i*i..n+1).step_by(i*2) { p.set(to_index(j), true); } } let mut v = Vec::::new(); v.push(2); for i in 0..p.len() { if p[i] == false { v.push(from_index(i)) } } v } fn main() { println!("{}", primes(15485863).len()); }