forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimesGenerator.cpp
More file actions
43 lines (35 loc) · 744 Bytes
/
PrimesGenerator.cpp
File metadata and controls
43 lines (35 loc) · 744 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
39
40
41
42
43
#include <vector>
#include <iostream>
using namespace std;
vector<int> getPrimes(int n) {
if (n <= 1)
return vector<int>();
vector<bool> prime(n + 1, true);
prime[0] = prime[1] = false;
vector<int> primes;
for (int i = 2; i * i <= n; i++)
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
primes.push_back(i);
}
return primes;
}
bool isPrime(long long n) {
if (n <= 1)
return false;
for (long long i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
int main() {
int n = 31;
vector<int> primes = getPrimes(n);
for (int i = 0; i < primes.size(); i++)
cout << primes[i] << " ";
cout << endl;
for (int i = 0; i <= n; i++)
if (isPrime(i))
cout << i << " ";
}