From 83b2aa0782899ecaf3e7dfd30d8113be98335c7f Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 20 Nov 2024 12:29:15 +0000 Subject: [PATCH] solutions: 10 --- summation_of_primes.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 summation_of_primes.rs diff --git a/summation_of_primes.rs b/summation_of_primes.rs new file mode 100644 index 0000000..737b28e --- /dev/null +++ b/summation_of_primes.rs @@ -0,0 +1,30 @@ +fn is_prime(num: u64) -> bool { + let prime: bool = true; + match num { + 0..=1 => false, + 2 => true, + num if num % 2 == 0 => false, + _ => { + for i in (3..=((num as f64).sqrt() as u64)).step_by(2) { + if num % i == 0 { + return false; + } + } + prime + } + } +} + +fn main() { + let mut increment: u64 = 1; + let mut total: u64 = 0; + + while increment < 2000000 { + if is_prime(increment) { + total += increment; + } + increment += 1 + } + + println!("{:?}", total); +}