Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/algorithms/primes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -51,4 +53,4 @@ Primes::PrimeFactors(int n) {
}
}
return factors;
}
}