Created
September 7, 2015 11:35
-
-
Save Taneb/e6b50a200094db4e31cc to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Rust
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
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