Hello everybody, I have to find the sum of primes below limit.
My program works fine when I try to find primes less than 10. However, when I run it for the sum of primes less than 100, I get 166337 when I am supposed to get 1060. My program works on modular arithmetic, where any prime greater than 3 can be expressed as 1 or 5 mod 6.
#include <iostream>
usingnamespace std;
int main()
{
unsignedlonglong prime, sum;
int limit = 100;
int r = 0;
prime = 5; // Prime has to start from 5 because the logic works for primes greater than 3
sum = 5; //I let the sum be 5 because 2+3 = 5
do{
r = prime % 6;
if ((r == 1)||(r==5))
{
sum = sum + prime;
}
prime = prime + 2;
}while (prime<limit);
cout<<"SUM: "<<sum;
return 0;
}
... any prime greater than 3 can be expressed as 1 or 5 mod 6.
That does not imply (unfortunately) the converse, that "any number greater then 3 that can be expressed as 1 or 5 (mod 6), is prime." Counter-example: 35 = 5 (mod 6), but 35 is NOT prime...