Skip to content

Instantly share code, notes, and snippets.

@valep27
Created July 12, 2015 19:55
Show Gist options
  • Save valep27/ac3ec2c62cfc525f3b92 to your computer and use it in GitHub Desktop.
Save valep27/ac3ec2c62cfc525f3b92 to your computer and use it in GitHub Desktop.
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