Answers for "rust prime number check"

0

rust prime number check

// 6k+-1 optimisation
fn is_prime(n: i64) -> bool {
    if n <= 3 {
        return n > 1;
    }
    if n % 2 == 0 || n % 3 == 0 {
        return false;
    }
    if n < 25 {
        return true;
    }
    let mut i: i64 = 5;
    while i.pow(2) <= n {
        if n % i == 0 || n % (i + 2) == 0 {
            return false;
        }
        i += 6;
    }
    return true;
}
Posted by: Guest on February-24-2021

Browse Popular Code Answers by Language