Start New Discussion within our **Software Development Community** # Prime Number Generator

```
// vishesh yadav
// prime number generator
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <ctime>
int checkprime(unsigned int max, std::vector<unsigned int> &prms);
int main()
{
unsigned int MAX;
std::cout << "ENTER RANGE FROM 0 TO: ";
std::cin >> MAX;
clock_t start, finish;
std::vector<unsigned int> primes; start = clock();
checkprime(MAX, primes); finish = clock();
std::cout << "\nWTITING TO FILE \'primes.txt\'";
std::ofstream file("primes.txt");
file.clear();
file << "TIME TAKEN: " << (double)(finish - start) / CLOCKS_PER_SEC;
file << "\nPRIMES BETWEEN 0 TO " << MAX
<< "\nTOTAL PRIMES GENERATED: " << primes.size() << "\n\n";
for(std::vector<unsigned int>::size_type i=0; i<primes.size(); i++) {
file << "\n" << primes[i];
}
file.close();
}
int checkprime(unsigned int max, std::vector<unsigned int> &prms)
{
std::vector<unsigned int>::size_type sze;
prms.push_back(2);
for(unsigned int i=3; i<max; i+=2) {
unsigned int x = (unsigned int)(std::sqrt(float(i)));
sze = prms.size();
for(std::vector<unsigned int>::size_type j=0; j<sze; j++) {
if(x < prms[j]) {
prms.push_back(i);
break;
}
else if(i%prms[j] == 0) {
break;
}
}
}
return 0;
}
```

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.