Basic prime number calculation.

This commit is contained in:
Nicolás Ortega Froysa 2021-02-26 18:22:20 +01:00
parent 01f3f6ddf6
commit eb4405e1c3
2 changed files with 38 additions and 0 deletions

5
Cargo.lock generated Normal file
View File

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "indivisible"
version = "0.1.0"

View File

@ -1,2 +1,35 @@
use std::env;
fn main() { fn main() {
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
let mut prime_count = 1;
let mut candidate = 3;
while prime_count < n
{
let mut aux = 3;
let mut is_prime = true;
while aux < candidate / 2
{
if candidate % aux == 0
{
is_prime = false;
break;
}
aux += 2;
}
if is_prime
{
println!("{}", candidate);
prime_count += 1;
}
candidate += 2;
}
} }