Prime Number Sieve* What's wrong?

I am working on the typical prime number problem, and have decided to tackle it using the sieve method. I came up tih this code and believe it should work, at the moment there are no Syntax problems, but does not quite work as I envisioned it to work. Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
/*Generation of a prime number 
by use of the Sieve of Eratosthenes */

int main()
{
    int int_list, x;
    for(int_list=2; int_list<=100; int_list++)//Print list of intergers between 2 and 100.
    cout << int_list << "\n";
    cin.ignore();
    system("cls");//Restart after displaying integers.
    /* I am running the loop a second time, and pairing it with a second loop,
     this is with the hope that I can have the program go through each interger 
     on the list, and then display any integer on that list that is not a multiple
     of 2, 3, 5, or 7. I am using the x loop to do (2,3,5,and 7 * 2,3,4,5,6,etc..) */
    int_list=2;
    for(int_list=2; int_list<=100; int_list++)//loop 1
    for(x=2;x<=50;x++)//loop 2
    if (int_list != (2*x) || (3* x) || (5*x) || (7*x));
    cout << int_list;
    cin.ignore();//wait for key press to close program
    


Yes, I do know that using system() is somewhat of a bad practice, but I know full well what the system() actually means, and that it is OS specific. But currently for me it is the easiest way to get the job done.

Anyway... any comments? Is there anyone that understands what I'm trying to do and sees what is wrong?
EDIT: I believe the problem falls in place somewhere between lines 18 and 24. I think I know kinda what the problem is, but not sure how to fix it. I believe that with every run on loop 1, it overwrites the output of the previous, thus giving me only the final answer. Anyone know how to fix this?
Last edited on
if (int_list != (2*x) || (3* x) || (5*x) || (7*x));
You shouldn't have the semicolon
|| operator doesn't work that way ( you should have int_list != 2*x || int_list != 3*x ... )
The logic isn't fully correct, you don't need to check whether int_list == p*x but whether p divides int_list ( using % operator )
The logic isn't fully correct, you don't need to check whether int_list == p*x but whether p divides int_list ( using % operator )
Don't really understand what you mean there. My program is set up to find the multiples of 2,3,5,and 7 up to (2,3,5,7)*50, and check to see if int_list is equal. if int_list is equal, then int_list is not prime, therefore int_list !=... is prime.
Like Bazzy sayd if (int_list != (2*x) || (3* x) || (5*x) || (7*x)) is not the correct logic.

Between each or || and each and && should be an extra logical request.
So you have to write your idea this way:

if (int_list != (2*x) || int_list !=(3* x) || int_list !=(5*x) || int_list !=(7*x))

But it's still not correct.
Because if one of this requests is true, the whole term will be true. For example with number 35:
(true || true || false || false) => true
So you should better use && instead of ||.

But also with this change it whould not work correctly ;)
Because with our example (35) it whould make an output for each small number which is no factor of 35 like 2.
2*2 != 35, 3*2!=35, 5*2!=35 and 7*2!=35 => true => output.

So you should try something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int_list=2;
for(int_list=2; int_list<=100; int_list++)//loop 1
{
  bool isPrime = true;
  for(x=2;x<=50;x++)//loop 2
  {
    if (int_list == (2*x) || int_list ==(3* x) || int_list ==(5*x) || int_list ==(7*x))
    {
      isPrime=false;
      break; // added a break to leave the loop on first match and make it faster
    }
  }
  if(isPrime) cout << int_list;
}
Last edited on
1
2
3
4
  for(x=2;x<=50;x++)//loop 2
  {
    if (int_list == (2*x) || int_list ==(3* x) || int_list ==(5*x) || int_list ==(7*x))
    {

is equivalent to
if ( int_list % 2 || int_list % 3 ... )
Notice that the second version doesn't need a loop
I'm not so sure you posted that correctly Baz

your saying this is fine...

1
2
3
4

if ( 67 % 4 ) 
   blowUpThePC();


mind you i didn't read much of the other posts...

possibly should be int_list % 2 == 0 || int_list %3 == 0 || ... ??
Last edited on
Yeah, you're right.
Topic archived. No new replies allowed.