Hey All - i am currently working on an assignment for a computer science programming class. I have to find and print all the prime numbers from 2 to N (N being a number read in from file).
I have some code but i do can not get my mind around one of the main concepts. I have to use the sieve of eratosthenes. This, from what i have read elsewhere online and mentioned in my assignment notes goes something like this:
you take an array of numbers, say 1 - 100 and then do,
2 * 2 = 4, 4 *2 = 8, 8 * 2 = 16 ... etc etc... while doing this, you delete the value from the array ( set it to zero - for error checking later while printing). But then you take... say 3 and do 3 * 2 = 6, 6 * 2 = 12, .... etc ...
Is this correct? Or do i have the idea of it completely incorrect... ?
As always i have done some minor work on this so far, but without a correct understanding of the sieve algorithm im not going to get very far. I will post my attempt below, i ask not that you give me code by any means, but just a hand with understanding the concept the of the sieve algo.
Thanks to everyone in advance, here is my feeble attempt w/o knowing what i truly have to do:
void sieve(set<int>& intSet, int n)
{
ostream_iterator<int> screen(cout, " ");
int i = n;
int j = 2;
// K is the value that will be multiplied
for (int k = 2; k < n; k = k * j)
{
cout << "Will erase value: " << k << endl;
intSet.erase(k);
j++;
k++;
}
for (int h = 2; h < i;h++)
{
cout << "value of h = ";
copy(intSet.begin(), intSet.end(), screen);
cout << endl;
}
}
Using an input value of 10, give me the following output:
10
Will erase value: 2
Will erase value: 9
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
value of h: 3 4 5 6 7 8
Hope i can get a hand with understand this concept better...