My solution seems quite different from the one in the 'Solution Guide'.
Will mine detect all primes or will it break? It appears to work but
I don't know enough to be sure. Thanks for any insight.
b52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
int i,j;
for (i = 2; i < 1000; i++)
{
for (j = 2; j < i; j++)
if (i % j * j == 0) break;
if (j < i)
continue;
else
cout << i << endl;
}
return 0;
}
I see what you mean. When I googled prime formula I must have misunderstood the information that I read. Thanks, I would never have noticed that - even when I debugged the code.
PROTIP: It's not necessary to test something to demonstrate that it's incorrect, and just because something passes a test, it doesn't make it correct. Testing can only prove the presence of bugs, not their absence.
Agreed that 2nd method won't work. Skillless needed a higher # for his counter example as 21%3==0 is tested for. Method fails if lowest prime factor is > 5 so lowest "false positive" comes for 7*7 = 49.