primes/primes.cpp

62 lines
859 B
C++

// C++ version of a Prime Number Finder
// haplo@mindstab.net
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
using namespace std;
class CPrimes
{
public:
void countPrimes(unsigned int MAX);
};
void CPrimes::countPrimes(unsigned int MAX)
{
for(unsigned int cnt = 3; cnt <= MAX; cnt+=2)
{
int test = (int)sqrt((double)cnt);
char isPrime=1;
for(int i = 3; i<= test; i+=2)
{
if(! (cnt % i))
{
isPrime = 0;
break;
}
}
if(isPrime)
{
cout << cnt << endl;
}
}
}
int main(int argc, char **argv)
{
int MAX;
if(argc <= 1) {
cout << "Useage: primescpp [Max Num]" << endl;
return(1);
} else {
MAX = atoi(argv[1]);
}
if(MAX < 1) {
cout << "Invalide Max Num" << endl;
return(1);
}
CPrimes *primes = new CPrimes();
primes->countPrimes(MAX);
delete primes;
return(0);
}