From f9945ad88c3a4dc2b35145f7259a14f2df94a2a3 Mon Sep 17 00:00:00 2001 From: TurinTech Bot Date: Fri, 23 Aug 2024 09:22:20 +0000 Subject: [PATCH] Artemis Changes --- src/algorithms/primes.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/algorithms/primes.cc b/src/algorithms/primes.cc index d4250ab..6622fbe 100644 --- a/src/algorithms/primes.cc +++ b/src/algorithms/primes.cc @@ -22,12 +22,14 @@ Primes::IsPrime(int n) { * * @param n Upper bound (non-inclusive) of the sum * @return The sum of all prime numbers from 0 to n - */ + */ int Primes::SumPrimes(int n) { - int sum = 0; + int sum = 2; // 2 is the first prime number + + if (n <= 2) return 0; // No primes below 2 - for (int i = 0; i < n; i += 1) { + for (int i = 3; i < n; i += 2) { // Start from 3 and check only odd numbers if (IsPrime(i)) { sum += i; } @@ -51,4 +53,4 @@ Primes::PrimeFactors(int n) { } } return factors; -} +} \ No newline at end of file