primes/primes.rs

28 lines
666 B
Rust
Raw Normal View History

use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("Usage: ./primes $MAX");
return;
}
let max: u64 = args[1].parse().expect("MAX must be an integer value");
let mut i: u64 = 3;
while i <= max {
let loop_max: u64 = (i as f64).sqrt() as u64;
let mut x: u64 = 2;
let mut prime = true;
while x <= loop_max {
if i % x == 0 {
prime = false;
break;
}
x += 1;
}
if prime {
println!("{}", i)
}
i += 2;
}
}