why isn't my loop working properly?

• 09-07-2005
orion-
why isn't my loop working properly?
im trying to make a simple program that generates 10 random prime numbers and displays them onto the screen. heres the code...

Code:

```#include <iostream> #include <cstdlib> #include <time.h> using namespace std; bool isprime(int num); int main() {     int randP = 0,i = 22;     srand( (unsigned)time( NULL ) );     for (int x = 0; x<10; x++)     {         randP = rand() % i; // generate a random number between 0 and the variable 'i'         if (isprime(randP) == true) // if the random number is a prime then display it         cout << randP << endl;                }     cout << endl;     system("Pause");     return 0; }       bool isprime(int num) {     bool ya = false;     if (num % 2 == 0)     ya = true;     else     ya = false;     return ya; }```
im not asking 'how do i generate 10 random prime numbers', i just simply want to know why my loop doesn't repeat 10 times like i want it to! to get an idea of what im talking about you have to try the code out for yourself. please help.
• 09-07-2005
loko
You need to decrement x whenever isprime == false;
• 09-07-2005
orion-
wow thanks! that worked perfectly! could you explain why i needed to decrement x whenever isprime == false?
• 09-07-2005
Daved
You should have been incrementing x only when the random number you got was actually prime. Since the for loop was incrementing x automatically whether or not you had a prime, decrementing x when the number wasn't prime counteracted that so that x would stay the same until the next prime number was found.

Another way to do it would have been to use a while loop and only increment x if the random number is prime.
• 09-07-2005
Narf
Code:

```bool isprime(int num) {     bool ya = false;     if (num % 2 == 0)     ya = true;     else     ya = false;     return ya; }```
Your code is overly verbose. It's also misleading because you're not testing for primality, but whether the number is even or not. That's only a first step for a primality test, and you can make it shorter like this:
Code:

```bool is_even(int num) {   return num % 2 == 0; }```
Testing for a prime number is harder. Especially if you make it fast. :)
• 09-07-2005
orion-
Quote:

Originally Posted by Daved
Another way to do it would have been to use a while loop and only increment x if the random number is prime.

something like this.....???

Code:

```int x = 0; while (isprime(randP) == true) && x<10) {       x++;       cout << randP << endl; }```
Quote:

Originally Posted by Narf

Your code is overly verbose. It's also misleading because you're not testing for primality, but whether the number is even or not. That's only a first step for a primality test, and you can make it shorter like this:
Code:

```bool is_even(int num) {   return num % 2 == 0; }```
Testing for a prime number is harder. Especially if you make it fast. :)

yeah i know my isprime function is pretty.....n00bish. do you know if there is a more efficient function in the C++ STL to test whether a number is a prime or not? because im pretty sure my isprime function wouldn't be able to calculate large numbers like 1.92749293503
• 09-07-2005
Narf
Quote:

because im pretty sure my isprime function wouldn't be able to calculate large numbers like 1.92749293503
I'm very sure that your isprime function won't be able to calculate primes at all. Unless you're confused as to what a prime is. The primes lower than 10 are 2, 3, 5, and 7. Let's run the equivalent of your code through all positive numbers less than 10:
Code:

```#include <iostream> using namespace std; bool isprime(int num) {   return num % 2 == 0; } int main() {   for (int x = 1; x < 10; x++) {     if (isprime(x))       cout << x << endl;              }   return 0; }```
isprime() failed to find all but one of the primes because isprime() doesn't test for primality, it tests for divisibility by 2. A prime number is a number that is only evenly divisible by itself and 1, which rules out most of the even numbers. That makes your isprime() horribly broken if you think it finds primes. A simple test for primality is slightly more difficult than just testing divisibility by 2, but it's dreadfully slow for larger numbers:
Code:

```bool isprime(int num) {   if (num < 2) return 0;   for (int x = num - 1; x > 1; x--) {     if (num % x == 0) return false;   }   return true; }```
• 09-07-2005
Daved
>> something like this.....???

Not exactly. In your new version, if isprime returns false, then the loop will be broken. You only want the loop broken when x < 10 is not true. So remove the isprime part from the control of the while loop. Instead, it should go inside the loop in an if statement like you had it before. The only change would be to also increment x if isprime returned true.