Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Prime Number Finder - Finished


devilsson2010's Avatar
Member
0 0

This is a program I made to check for prime numbers starting from 3, since 1 and 2 aren't technically prime numbers. Anyway, it uses modulus (division remainder) to check if it has any divisors besides 1 and itself. So far in about 1 1/2 days of running it, it has found 1,525,631, the highest one so far being 24,144,881. Anyway here is the code:

#include <iostream>
#include <math.h>
#include <fstream>

using namespace std;

int main()
{
int lastnum=0;

fstream primenums("prime_numbers.txt" );
if (primenums.fail())
{printf("Unable to find Prime_Numbers.txt\nExiting . . .\n" );
system("PAUSE" );
return EXIT_SUCCESS;
} else {printf("Prime_Numbers.txt successfully opened\n" );}

printf("Finding last prime number to start from . . .\n" );
int iamount=0;
while (!primenums.eof())
{iamount+=1;
primenums >> lastnum;}

system("cls" );
printf("Prime_Numbers.txt successfully opened\n" );

primenums.seekg(0,ios::beg);
primenums.clear();

if (lastnum!=0)
{
cout << "Last prime number found was: " << lastnum << endl;
cout << "Number of prime numbers found is: " << iamount << endl;
} else {lastnum=1;
        printf("No last prime number found\n" );}

printf("\nHit any key to start calculating prime numbers . . .\n" );
system("PAUSE" );
system("cls" );
printf("Calculating Prime Numbers . . .\n" );

for (int j=lastnum+2;;j+=2)
{
bool Prime=true;

int checknums=(j/2)+1;

for (int i=3;i<checknums;i+=2)
    {if (j%i==0)
        {Prime=false;
         break;}
    }
if (Prime)
   {primenums << j << endl;}
}

primenums.close();

return EXIT_SUCCESS;
}```