36 lines
743 B
Rust
Raw Normal View History

2021-02-26 18:22:20 +01:00
use std::env;
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
let n:u64 = args[1].parse().unwrap();
// first prime
println!("{}", 2);
// already have first prime
2021-02-26 18:27:16 +01:00
let mut prime_count:u64 = 1;
let mut candidate:u64 = 3;
2021-02-26 18:22:20 +01:00
while prime_count < n
{
2021-02-26 18:27:16 +01:00
let mut aux:u64 = 3;
2021-02-26 18:22:20 +01:00
let mut is_prime = true;
2021-02-26 18:27:16 +01:00
while aux < (candidate as f64).sqrt() as u64
2021-02-26 18:22:20 +01:00
{
if candidate % aux == 0
{
is_prime = false;
break;
}
aux += 2;
}
if is_prime
{
println!("{}", candidate);
prime_count += 1;
}
candidate += 2;
}
2021-02-26 17:58:58 +01:00
}