Skip to content

Instantly share code, notes, and snippets.

@Taneb
Created September 7, 2015 11:35
Show Gist options
  • Save Taneb/e6b50a200094db4e31cc to your computer and use it in GitHub Desktop.
Save Taneb/e6b50a200094db4e31cc to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Rust
pub fn main() {
let limit = 1000000;
let mut sieve = Vec::with_capacity(limit-2);
for _ in (2..limit) {
sieve.push(true);
}
for ix in(0..limit-2) {
if sieve[ix] {
println!("{}", ix + 2);
for i in (2..).map(|i| i * (ix + 2) - 2).take_while(|&i| i < limit - 2) {
sieve[i] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment