Last active
May 18, 2016 14:13
-
-
Save Dhole/7e35e1279920984cf4638e86d591b6b4 to your computer and use it in GitHub Desktop.
Rust sieve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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