How to find the sum of all the primes below 10000 (ten thousand) in Rust

1 Answer

0 votes
fn is_prime(n: u32) -> bool {
    if n < 2 || (n % 2 == 0 && n != 2) {
        return false;
    }

    let count = (n as f64).sqrt().floor() as u32;
    for i in (3..=count).step_by(2) {
        if n % i == 0 {
            return false;
        }
    }

    true
}

fn main() {
    let num = 10_000;
    let mut sum = 0;

    for i in 2..num {
        if is_prime(i) {
            sum += i;
        }
    }

    println!("sum = {}", sum);
}


    
/*
run:

sum = 5736396
   
*/

 



answered Jul 26 by avibootz
...