diff --git a/rust/src/include/primes.rs b/rust/src/include/primes.rs index 03b1ff6d..52fb7c5f 100644 --- a/rust/src/include/primes.rs +++ b/rust/src/include/primes.rs @@ -126,8 +126,16 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div + Rem + pub fn is_prime(x: I) -> bool where I: Hash + Zero + One + Add + Ord + Copy + Div + Rem + 'static { - if x < (one::() + one::()) { + let two = one::() + one::(); + 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(); }