TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
prime_numbers.cpp
Go to the documentation of this file.
1
6
#include <iostream>
7
#include <vector>
8
12
std::vector<int>
primes
(
size_t
max) {
13
std::vector<int> res;
14
std::vector<bool> is_not_prime(max + 1,
false
);
15
for
(
size_t
i = 2; i <= max; i++) {
16
if
(!is_not_prime[i]) {
17
res.emplace_back(i);
18
}
19
for
(
int
p : res) {
20
size_t
k = i * p;
21
if
(k > max) {
22
break
;
23
}
24
is_not_prime[k] =
true
;
25
if
(i % p == 0) {
26
break
;
27
}
28
}
29
}
30
return
res;
31
}
32
34
int
main
() {
35
std::cout <<
"Calculate primes up to:\n>> "
;
36
int
n = 0;
37
std::cin >> n;
38
std::vector<int> ans =
primes
(n);
39
for
(
int
p : ans) std::cout << p <<
' '
;
40
std::cout << std::endl;
41
}
primes
std::vector< int > primes(size_t max)
Definition
prime_numbers.cpp:12
main
int main()
Definition
prime_numbers.cpp:34
math
prime_numbers.cpp
Generated by
1.12.0