Skip to content

Instantly share code, notes, and snippets.

@scratchyone
Created February 2, 2020 02:48
Show Gist options
  • Save scratchyone/3bb7cea5e76d96cafd32e3f0d1e390b5 to your computer and use it in GitHub Desktop.
Save scratchyone/3bb7cea5e76d96cafd32e3f0d1e390b5 to your computer and use it in GitHub Desktop.
pub fn prime(amount: i32) -> Vec<i64> {
let mut out: Vec<i64> = Vec::with_capacity(amount as usize / 3);
let mut allowed: Vec<bool> = vec![false; amount as usize];
for i in
(0_usize..amount as usize).filter(|n| n % 2 == 1 && n % 5 != 0 && n % 3 != 0 && n % 7 != 0)
{
allowed[i] = true;
}
out.push(2_i64);
out.push(3_i64);
out.push(5_i64);
out.push(7_i64);
for i in (4..amount as usize).filter(|n| n % 2 == 1 && n % 5 != 0 && n % 3 != 0 && n % 7 != 0) {
let mut div = false;
for x in (2..((i / 3) + 1)).filter(|n| allowed[*n]) {
if (i as f64 % x as f64) == 0.0 {
div = true;
}
}
if div == false {
out.push(i as i64);
}
//print!("\r{}% done", ((i as f32 / amount as f32) * 100.0) as i32);
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment