```
// 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;
}
```

**Are you able to help answer this sponsored question?**

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Recommended Topics