29 lines
666 B
C++
29 lines
666 B
C++
|
|
// Generate primes using the sieve of eratosthenes
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
int main() {
|
|
// TODO: make it work with GMP (https://gmplib.org/)
|
|
unsigned int nextnumber { 3 };
|
|
bool notprime;
|
|
std::vector<unsigned int> primes;
|
|
primes.push_back(2); // Initial prime
|
|
std::cout << "2\n";
|
|
|
|
while (true) {
|
|
notprime = false;
|
|
for (unsigned int i=0;i<primes.size();++i) {
|
|
if (nextnumber % primes[i] == 0) {
|
|
notprime = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!notprime) {
|
|
primes.push_back(nextnumber);
|
|
std::cout << nextnumber << '\n';
|
|
}
|
|
nextnumber += 2; // Except for two, primes are odd. This skips even numbers.
|
|
}
|
|
}
|