Skip to content

Instantly share code, notes, and snippets.

@Dhole
Last active May 18, 2016 14:13
Show Gist options
  • Save Dhole/7e35e1279920984cf4638e86d591b6b4 to your computer and use it in GitHub Desktop.
Save Dhole/7e35e1279920984cf4638e86d591b6b4 to your computer and use it in GitHub Desktop.
Rust sieve
fn main() {
println!("{:?}", sieve1(30));
}
pub fn sieve1(n: u32) -> Vec<u32> {
let mut nums = (0..n).map(|x| Some(x)).collect::<Vec<Option<u32>>>();
let nums_len = nums.len() as u32;
for i in 2..nums.len() {
if let Some(y) = nums[i] {
for j in (2..).map(|x| x * y).take_while(|&j| j < nums_len) {
nums[j as usize] = None;
}
}
}
nums.iter().skip(2).filter(|&x| x.is_some()).map(|x| x.unwrap()).collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment