Printing Prime Numbers

Hi,

I'm learning about structuring my program to produce prime numbers. I know there can be several various ways to do this, and I've looked at some, but having difficulty getting my code to work.

I know that a prime is only dividable by 1 and itself. So I'm working on the logic behind what to filter out. Here's the logic I have written down, which I'm translating into code:

- test_number cannot have an integer sqrt
- test_number % random_number cannot have a remainder, as long as test_number is not "1" or "test_number" itself

I think I can do these 2, but the last one is troublesome since 6 % 3 = 0. So I'm thinking about saying:

if (test_number % another_number == 0) && (test_number / another_number > 0)
then exclude this. But that's not working. It seems to leave out the last part. In fact, the code returns all of the numbers I don't want, and excludes the ones I do.

Here's what I got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;

int i,j;


int main()
{
	
	for (i = 1; i < 10; i++)
	{
		
		for (j = 2; j < i; j++)
         {
         			
			 if (((i > j) && ((i % j) > 0)) && ((i / j) > 0))   // both the quotient & the remainder have to be satisfied
					
			 {
				  // do nothing 
			 }

			  else
			  {  
				  // display "i" values which don't meet the "if" criteria, and show the "%" and "/" of the resuls
				  cout << i << ", " << j << "     " << "i % j = " << i % j <<  "         i / j = " << i / j << endl;
			  }
			
		} 
	 }     

cin.ignore();
return 0;
}
In fact, the code returns all of the numbers I don't want, and excludes the ones I do.

Perhaps you should then reverse something.

Consider these:
7 % 2 == 1
7 % 3 == 1
7 % 4 == 3
7 % 5 == 2
7 % 6 == 1
Is seven a prime or not?

8 % 2 = 0
8 % 3 = 2
8 % 4 = 0
8 % 5 = 3
8 % 6 = 2
8 % 7 = 1
Is eight a prime or not?


Note also that you cannot know whether a number is a prime after testing with first divisor (2) like your if..else implies. We know that 7 is a prime only after we have tested with all reasonable divisors. One can "fail" early, not not "succeed". How would you use a bool flag to convey information?
Thanks for the sound reasoning. Made my mistake pretty easy to catch, and cleaned up my code nicely.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
	
cout << "Prime numbers from 1 - 10 are: ";

for (i=2; i < 10; i++)
{
prime = true;  // reset each number of increasing 'i', this assumes 'i' is prime unless proven otherwise

		for (j = 2; j < i; j++)
		{
		  if (i % j == 0)
		  {
			prime = false; // number isn't prime, go to next 'i' / trigger loop to end)
			j = i; // this will exit current for loop

		  }

		}

		if (prime == true)
		{
		  // print 'i'
          cout << i << " "; 
		}
		
// return to top of for loop for 'i'
}

cin.ignore();
return 0;
}
Topic archived. No new replies allowed.