Created
July 12, 2015 19:55
-
-
Save valep27/ac3ec2c62cfc525f3b92 to your computer and use it in GitHub Desktop.
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() { | |
let n = 10000000; | |
println!("Let's print prime numbers up to {}", n); | |
let primes = sieve(n); | |
for i in primes { | |
print!("{} ", i); | |
} | |
} | |
fn sieve(limit : u64) -> Vec<u64> { | |
// init the sieve | |
let mut prime = vec![true;limit as usize]; | |
// set to false all multiples of prime numbers | |
for i in 2..limit { | |
if prime[i as usize] { | |
let mut j = i*i; | |
while j < limit { | |
prime[j as usize] = false; | |
j += i; | |
} | |
} | |
} | |
let mut result : Vec<u64> = Vec::new(); | |
for i in 2..limit { | |
if prime[i as usize] { | |
result.push(i); | |
} | |
} | |
result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment