David
David

Reputation: 3

Prime number programs stops at 20,031'st prime, 225,149 for no apparent reason. No error message

Why does my program for finding primes work, but exit at the 20,031'st prime, which is 225,149? There is no error message and x-code claims that the program "ended with exit code" as it would normally do.

My question is not about the program per se, which appears to be working (finding primes), but what limit or calculation error I'm coming up against that's causing it to exit.

This loop is intentionally infinite, but it prints out to 225,149 as expected then stops and exits with no error. It just exits, as if it reached the end of a loop. I've tried putting a limit on iterations and bumped it up bit by bit to 10,000,0000,000 but it oddly stops a the same number, 225,149.

Is there a computation time bound that is exceeded or something else?

The last bit of output: the program outputs the a count of the primes from zero, then the prime.

20027) 225109 20028) 225119 20029) 225133 20030) 225143 20031) 225149 Program ended with exit code: 0

#include <iostream>
#include <cmath>
using namespace std;

//performs modulus and find remainder return remainder r to void primal
double fmodulus (double n, double d)
{
    double r;
    r= fmod(n, d);
    return r;
 }
//finds prime number using modulus double type modulus function fmod()
void primal()
{
    int count=1;
    double n=3.0, d=2.0;
    for (int i= 0; i>=0; ++i)
    {
        double r;
        r= fmodulus(n, d);

        //if n==d then is prime nymber   
        if (n==d)
        {
            cout<<count<<") "<<n<<endl;
            n++;
            d=2.0;
            count++;
        }

        //if remainder == 0 then not prime number    
        else if (r==0)
        {
            n++;
            d=2.0;
        } 

        //not prime so updates d of modulus increment by 1
        else
        {
            d++;
        }
    }
}


int main(int argc, const char * argv[])
{
    cout<<endl;
    primal();
}

Upvotes: 0

Views: 101

Answers (2)

lephe
lephe

Reputation: 327

The problement si that i is incremented each time you make a test. In this case, you can easily reach 2 billion tests. And when i overflows, its value becomes negative, so the loop ends.

You can do 3 different things to solve this problem :

  • The first is to change the type of i to make it unsigned int or unsigned long long. Those types are always positive, and your loop will never end. The difference is only that an unsigned long long is written on 64 bits instead of 32.

  • The second way is to change the condition in your loop ; if you want it to be infinite, you can simply use 1. This is the general true condition.

  • The last way is to change your program, inserting a second loop inside the first to ensure that a different number is tested at each iteration.

Upvotes: 2

David Grayson
David Grayson

Reputation: 87446

Eventually the int i will overflow. Signed integer overflow is undefined behavior so the compiler can do whatever it wants.

Usually it will just overflow to INT_MIN, which is negative so your loop ends.

Try making i be a long and print its value to monitor it.

Upvotes: 2

Related Questions