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;
}
|