Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm trying to solve the 10th project euler problem of finding the summation of all prime numbers below 2kk .
C++
#include <iostream>
#include <vector>

using namespace std;

struct pr
{
    long num;
    bool is_prime;
};

vector<pr> getPrime(long n)
{
    vector<pr> my_prime(n+1);
    for (long i = 0; i <= n; i++)
    {
        my_prime[i].num = i;
        my_prime[i].is_prime = true;
    }
    my_prime[0].is_prime = false;
    my_prime[1].is_prime = false;
    long p = 2;
    while ( (p*p) <= n )
    {
        for (long i = 2; i*p <= n; i++)
        {
            my_prime[i*p].is_prime = false;
        }
        for (long j = p + 1 ; j <= n; j++)
        {
            if (my_prime[j].is_prime == true)
            {
                p = my_prime[j].num;
                break;
            }
        }
    }
    return my_prime;
}

int main()
{
    vector<pr> p = getPrime(1999999);
    long counter = 0;
    for (vector<pr>::iterator itr = p.begin(); itr != p.end(); ++itr)
    {
        if ( (*itr).is_prime == true )
            counter += (*itr).num;
    }
    cout << counter;
}

But The solution is not correct, any help ?

Thanks, Sam.
Posted
Comments
PIEBALDconsult 21-Jun-15 15:26pm    
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Samuel Shokry 21-Jun-15 15:59pm    
I used it in my code
PIEBALDconsult 21-Jun-15 15:27pm    
P.S. Is _one_ prime?
Dave Kreskowiak 21-Jun-15 16:28pm    
I can tell you that my Sieve of Eratosthenes doesn't look anything like yours. For starters, my 0 and 1 values start out as Prime whereas yours don't.

Why are you using an array of structures where you have the value and a flag? Why not just have an array of flags? The value is the index into the array!

Oh, my "flag" is actually an enum that provides each value with one of 3 states, not 2.
PIEBALDconsult 22-Jun-15 0:39am    
I use a long integer to contain sixty-four flags, and those flags are only for the odd numbers, so I really pack the values together and extend my range.

1 solution

what you have is numeric overflow the logic of you code is right but your long value probably is a 32-bit integer whose max value is 2,147,483,647 unsigned as you currently have and if unsigned 4,294,967,295 both values are less than 142,913,828,922 what you need is at least a 38-bit integer but the next common integer type is 64-bit. If will use the term
C++
long long
to represent a 64-bit integer. This is common is C++ 11 complier. If
C++
long long
then uses whatever type you complier uses to represent a 64-bit integer. I will only fix your main function as everything else look right.
C++
int main()
{
    vector<pr> p = getPrime(1999999);
    long long counter = 0;
    for (vector<pr>::iterator itr = p.begin(); itr != p.end(); ++itr)
    {
        if ( (*itr).is_prime == true )
            counter += (*itr).num;
    }
    cout << counter;
}
</pr></pr>
 
Share this answer
 
Comments
Stefan_Lang 14-Jul-15 4:06am    
It's already been solved: check the comments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900