Basic prime number calculation.
This commit is contained in:
parent
01f3f6ddf6
commit
eb4405e1c3
5
Cargo.lock
generated
Normal file
5
Cargo.lock
generated
Normal 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"
|
33
src/main.rs
33
src/main.rs
@ -1,2 +1,35 @@
|
||||
use std::env;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user