-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
38 lines (29 loc) · 715 Bytes
/
main.cpp
File metadata and controls
38 lines (29 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer \n=>";
cin >> n;
if (n < 2) {
cout << "There doesn't exist any prime numbers less than 2.\n";
return 0;
}
vector<bool> isPrime(n + 1, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; ++i) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
cout << "Prime numbers up to " << n << ":\n";
for (int i = 2; i <= n; ++i) {
if (isPrime[i]) {
cout << i << " \n" << endl;
}
}
return 0;
}