41 lines
923 B
Rust
Raw Normal View History

2021-02-26 18:22:20 +01:00
use std::env;
2021-03-01 16:12:23 +01:00
use std::collections::VecDeque;
2021-02-26 18:22:20 +01:00
2021-02-26 17:58:58 +01:00
fn main() {
2021-02-26 18:22:20 +01:00
let args:Vec<String> = env::args().collect();
// get the first `n` primes
2021-03-01 16:12:23 +01:00
let n:usize = args[1].parse().unwrap();
let mut primes:VecDeque<u64> = VecDeque::with_capacity(n);
2021-02-26 18:22:20 +01:00
// first prime
println!("{}", 2);
2021-03-01 16:12:23 +01:00
primes.push_back(2);
2021-02-26 18:27:16 +01:00
let mut candidate:u64 = 3;
2021-02-26 18:22:20 +01:00
2021-03-01 16:12:23 +01:00
while primes.len() < n
2021-02-26 18:22:20 +01:00
{
2021-03-01 16:12:23 +01:00
let mut i = 0;
let mut test_prime = primes.get(i).unwrap();
2021-02-26 18:22:20 +01:00
let mut is_prime = true;
2021-03-01 16:12:23 +01:00
while *test_prime < (candidate as f64).sqrt() as u64
2021-02-26 18:22:20 +01:00
{
2021-03-01 16:12:23 +01:00
if candidate % test_prime == 0
2021-02-26 18:22:20 +01:00
{
is_prime = false;
break;
}
2021-03-01 16:12:23 +01:00
i += 1;
test_prime = primes.get(i).unwrap();
2021-02-26 18:22:20 +01:00
}
if is_prime
{
println!("{}", candidate);
2021-03-01 16:12:23 +01:00
primes.push_back(candidate);
2021-02-26 18:22:20 +01:00
}
candidate += 2;
}
2021-02-26 17:58:58 +01:00
}