Create PrimeNumbers.cpp (#607)

* Create PrimeNumbers.cpp

* Rename Math/PrimeNumbers/PrimeNumbers.cpp to math/prime_numbers.cpp

* Trailing whitespace, std::cin, std::cout, std::endl

* std::vector

* std::vector again
This commit is contained in:
5ur3 2019-12-04 12:05:08 +04:00 committed by Christian Clauss
parent d16ae9350b
commit 0ce3226f00

26
math/prime_numbers.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
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]) {
for (int j = i; j < max; j += i)
numbers[j] = true;
res.push_back(i);
}
}
return res;
}
int main() {
std::cout << "Calculate primes up to:\n>> ";
int n;
std::cin >> n;
std::vector<int> ans = primes(n);
for (int i = 0; i < ans.size(); i++)
std::cout << ans[i] << ' ';
std::cout << std::endl;
}