Skip to content

Commit

Permalink
speed up is_prime() w/ heuristics
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 27, 2024
1 parent c78e4af commit 29a1ed8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rust/src/include/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,16 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> +
pub fn is_prime<I>(x: I) -> bool
where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> + 'static
{
if x < (one::<I>() + one::<I>()) {
let two = one::<I>() + one::<I>();
if x < two {
return false;
}
return is_composite(x) == zero();
let three = two + one();
if x <= three {
return true;
}
let five = (two * two) + one();
let six = (two + one()) * two;
let check = x % six;
return (check == one() || check == five) && is_composite(x) == zero();
}

0 comments on commit 29a1ed8

Please sign in to comment.