Created
October 14, 2014 14:53
-
-
Save scturtle/38f612438f0db968fb5c to your computer and use it in GitHub Desktop.
hello 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
use std::iter; | |
fn primes(maxn: uint) -> (Vec<bool>, Vec<uint>) { | |
let mut primes : Vec<uint> = Vec::new(); | |
let mut is_prime : Vec<bool> = Vec::from_elem(maxn + 1, true); | |
*is_prime.get_mut(0) = false; | |
*is_prime.get_mut(1) = false; | |
primes.push(2); | |
for i in iter::range_step_inclusive(4, maxn, 2) { | |
*is_prime.get_mut(i) = false; | |
} | |
let maxn_sqrt = (maxn as f64).sqrt() as uint; | |
for i in iter::range_step_inclusive(3, maxn_sqrt, 2) { | |
if is_prime[i] { | |
primes.push(i); | |
for i in iter::range_step_inclusive(i * i, maxn, i) { | |
*is_prime.get_mut(i) = false; | |
} | |
} | |
} | |
for i in iter::range_step_inclusive(maxn_sqrt + 1, maxn, 2) { | |
if is_prime[i] { primes.push(i); } | |
} | |
return (is_prime, primes); | |
} | |
fn main() { | |
let maxn = 100000000u; | |
let (is_prime, primes) = primes(maxn); | |
println!("{} is prime: {}", maxn, is_prime[maxn]); | |
println!("number of primes: {}", primes.len()); | |
} |
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
$ cargo build --release | |
$ time cargo run --release | |
Running `target/release/euler` | |
100000000 is prime: false | |
number of primes: 5761455 | |
real 0m1.522s | |
user 0m1.414s | |
sys 0m0.103s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment