Last active
October 27, 2021 04:16
-
-
Save tos-kamiya/65d4e1e4d7ca27c4d8a3bbf40748cf1c 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
fn simple_sieve(limit: usize) -> Vec<usize> { | |
let mut is_prime = vec![true; limit + 1]; | |
for n in 2 ..= limit { | |
if is_prime[n] { | |
for m in (n*n ..= limit).step_by(n) { | |
is_prime[m] = false; | |
} | |
} | |
} | |
(2 ..= limit).filter(|&n| is_prime[n]).collect() | |
} | |
fn main() { | |
println!("{:?}", simple_sieve(100)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment