Throw away all code that someone else gave you. You can see why is_prime() fails here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int is_prime(int number) //the problem is in this function
{
int num = number;
int factor=0;
do{
num++;
for(int i=1;i<(num+1);i++){
cout << "\n(num, i) = (" << num << ", " << i << ")\n" << flush;
if(num % i == 0)
factor++;
cout << "(factor = " << factor << ")\n" << flush;
}
}while(factor!=2);
return num;
}
|
I suspect that someone gave you that code, though. And yes, it is broken.
You would have a much easier time doing this straight yourself. You will need:
• The
potentially prime number. (You call this
prime.)
• A divisor/counter variable to loop from 2 to (
prime - 1).
That second part is where you are having trouble. Remember, to check to see if 7 is prime (for example), start with
•
prime = 7
•
divisor = 2 (all numbers are evenly divisible by one, so we start at two)
If 7 mod 2 is 0 then we know the number is not prime.
But since 7 mod 2 is 1 we cannot yet say that the number is not prime. (It might be, but it might not -- we don't yet know.)
•
divisor++
Again, if 7 mod 3 is 0 then we know the number is not prime.
But since 7 mod 3 is 1 we still cannot say we know anything about the primality of 7. If we keep going, we also learn that:
•
7 mod 4 ≠ 0
•
7 mod 5 ≠ 0
•
7 mod 6 ≠ 0
At this point,
divisor ==
prime. We did not find any numbers that evenly divide into
prime (except for 1 and
prime, which we know do evenly divide it), so we can safely conclude that the number is actually prime.
If we start with, say:
•
prime = 55
Then:
•
55 mod 2 ≠ 0
•
55 mod 3 ≠ 0
•
55 mod 4 ≠ 0
•
55 mod 5 = 0 (ah ha!)
Since 55 is evenly divisible by 5, and 5≠1 and 5≠55, we know that 55 cannot be a prime number. We'll just
return out of our loop.
Now that you've thought this through, put it all in a function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
bool is_prime(int prime)
{
int divisor = 2;
/* see note below */
while (divisor < prime)
{
if ( /* what is my test for not-a-prime? */ )
{
return false;
}
/* what do I do to divisor here? */
}
return true;
}
|
Here is the note: make sure to check that the value of
prime is greater than one before entering the loop, otherwise you will be stuck in a really long loop while we wait for 2++ to become greater than or equal to -7, for example.
Now, to use the function, just call it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int main()
{
int n;
cout << "What number would you like to test for prime? " << flush;
cin >> n;
if (is_prime(n))
{
cout << "prime\n";
}
else
{
cout << "not prime\n";
}
system("pause");
return 0;
}
|
Additional Notes
You need to pay special attention to indentation. Only indent lines that are somehow directly dependent on the less indented line above it. For example, you have lines 29 and 30 indented underneath line 28. But removing line 28 does not change how lines 29 and 30 work. 29 should be only indented as far as 28.
Line 30, however, is dependent on line 29. Line 30 should be indented further than 29.
28 29 30
|
cout<<counter<<endl<<endl;
if(prime % divisor == 0) //condition 1
prime /= divisor; /* <-- what is this supposed to do? (What did you learn from line 29?) */
|
Hope this helps.