Modulo 6

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.

Here is my code, please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
    unsigned long long 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;
}

OUTPUT:
SUM: 166337
Alternation of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    printf("2 3 ");
    int m5=25, m7=49, i=5, d=2,sum = 5;
    for( ; i<100; i+=d, d=6-d )
    {
        if( i!=m5 && i!=m7)
        {
            printf("%d ", i);
            sum += i;
        }
        if( m5 <= i ) m5+=10;
        if( m7 <= i ) m7+=14;
    }
    cout << endl << "Sum: " << sum << endl;
    return 0;
}
@greenleaf800073: Could you please kindly explain the logic behind your code?
um, compressed prime finder...
Assuming the converse! While it is true that
Neil010 wrote:
... 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...
Topic archived. No new replies allowed.