2020-05-28 04:45:33 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Get list of prime numbers
|
|
|
|
* @see primes_up_to_billion.cpp sieve_of_eratosthenes.cpp
|
|
|
|
*/
|
2019-12-04 16:05:08 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-05-28 04:45:33 +08:00
|
|
|
/** Generate an increasingly large number of primes
|
|
|
|
* and store in a list
|
|
|
|
*/
|
2019-12-04 16:05:08 +08:00
|
|
|
std::vector<int> primes(int max) {
|
|
|
|
max++;
|
|
|
|
std::vector<int> res;
|
|
|
|
std::vector<bool> numbers(max, false);
|
|
|
|
for (int i = 2; i < max; i++) {
|
|
|
|
if (!numbers[i]) {
|
2020-05-28 04:45:33 +08:00
|
|
|
for (int j = i; j < max; j += i) numbers[j] = true;
|
2019-12-04 16:05:08 +08:00
|
|
|
res.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:45:33 +08:00
|
|
|
/** main function */
|
2019-12-04 16:05:08 +08:00
|
|
|
int main() {
|
|
|
|
std::cout << "Calculate primes up to:\n>> ";
|
|
|
|
int n;
|
|
|
|
std::cin >> n;
|
|
|
|
std::vector<int> ans = primes(n);
|
2020-05-28 04:45:33 +08:00
|
|
|
for (int i = 0; i < ans.size(); i++) std::cout << ans[i] << ' ';
|
2019-12-04 16:05:08 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|