ok.. we'll look at the logic of the code. The primary function (the part we're interested in) is this section
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
for ( int i = 1; i<= n; i++)
for ( int b = 2; b<= sqrt((static_cast<double>(i))); b++)
{
if (i % b == 0)
{
cout<< i;
cout<< " is not prime";
cout<< "\n";
}
else
{
cout<< i;
cout<< " is prime";
cout<< "\n";
}
}
|
so, you have, for (b = 2; b <= sqrt((static_cast<double>(i))); b++)
I am no expert on this finding prime but am having a hard time figguring out why you need a square root..
if (i % b == 0) // if b devides into i evenly then it's not a prime
{...} // so we print it out
else // hrm, we havn't even bothered checking the rest of the b's yet.
{..} // so this print is pre-mature
What I might do differently is
for (b = 2; b < i; b++) // just make sure it's less than i
if (i % b == 0) // i liked this part
{ prime = false; } // just declare a bool named prime somewhere above
// and set it to true
then after looping through the b's.. if prime is still true, it's prime and print it. if not, it's not prime. reset prime to true, and move on to next iteration of the i loop.