Implementing a prime number counter

266 views Asked by At

For some reason, my last prime(int prime) isn't showing up at the end. Any clue ? fyi: primeEval stands for a flag, if the loop ends && primeEval==2, the number is actually a prime number. qty stands for quantity of primes counted.

int main(){


long primeEval=0,prime=0,qtyprime=0;

time_t timerr=(time(NULL)+10);



   for (int i = 2; time(NULL)!=timerr; i++) {

        for (int j = 1; j <= i; j++) {

            if((i%j)==0 && primeEval<2){

                primeEval++;

                if (i==j && primeEval==2) {
                    qtyprime++;
                    prime=i;
                    primeEval=0; // Resets for the next number 'i'

                }
            }

        }
    }

cout << "last prime found: " << prime << endl << "Ttal primes found: " << qtyprime;
}
2

There are 2 answers

4
NathanOliver On BEST ANSWER

New Answer:

With the change in your code you will now loop through all number. The problem with it now is that once you find a non prime number you will never reset primeEval and because of that you will never capture another prime number If you change your code to the following it will work

int main()
{
    long primeEval = 0, prime = 0, qtyprime = 0;
    time_t timerr = (time(NULL) + 10);

    for (int i = 2; time(NULL) != timerr; i++) {

        for (int j = 1; j <= i; j++) {

            if ((i%j) == 0){
                primeEval++;  // incmrent factor
            }
            // if we are at the end and have 2 factors then we are prime
            if (i == j && primeEval == 2) {
                qtyprime++;
                prime = i;
                primeEval = 0; // Resets for the next number 'i'
            }
            // if we reach the end with more than 2 factors reset and go to the next number
            if (i == j && primeEval > 2) {
                primeEval = 0; // Resets for the next number 'i'
            }
        }
    }
    cout << "last prime found: " << prime << endl << "Ttal primes found: " << qtyprime;
    cin.get();
    return 0;
}

I would also suggest you look at Which is the fastest algorithm to find prime numbers? to find some more efficient ways to get prime numbers.

Old Answer:

In your code you have:

for (int i = 2; time(NULL)!=timerr; i=+2)

So when you start checking from primes you start with 2 which is prime. Then you increment i by 2 so the next number you check is 4 which is an even number. All even numbers are not prime except for 2. Since you are always adding 2 you will always have an even number so the only prime number you will find is 2.

0
Fabio says Reinstate Monica On

You have different issues:

for (int i = 2; time(NULL)!=timerr; i=+2) {

Here the syntax is just wrong: it must be i+=2, not i=+2, otherwise you will keep setting i to +2 and testing whether 2 is prime.

Then, as others have pointed out, why are you increasing i by 2? If you want to optimize the search, you should increase j by 2, not i! And j should in any case start from 2 (or, given your approach, from 1), and then you should try j = 3 and then you can increase j by 2 without the risk of skipping some important divisors.

Then, you reset primeEval to 0 only if you find a prime. If you test a number i that is not prime, primeEval stays at 2 and you'll never get into the block again.

So the final code could be:

#include <iostream>
using namespace std;

int main(){

    long primeEval=0,prime=0,qtyprime=0;

    time_t timerr=(time(NULL)+10);

    for (int i = 2; time(NULL)!=timerr; i++) {
        primeEval=0;
        for (int j = 1; j <= i; j++) {
            if((i%j)==0 && primeEval<2){
                primeEval++;
                if (i==j && primeEval==2) {
                    qtyprime++;
                    prime=i;
                    primeEval=0; // Resets for the next number 'i'
                }
            }
        }
    }

    cout << "last prime found: " << prime << endl << "Ttal primes found: " << qtyprime;
}