-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_prime.cpp
62 lines (57 loc) · 1.32 KB
/
gen_prime.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int N;
int * primes;
bool * is_prime;
int tot;
void gen() {
memset(is_prime, false, sizeof(is_prime));
for(int i = 3; i < N; i+=2) {
is_prime[i] = true;
}
for(int i = 3; i < N; i+=2) {
if(is_prime[i]) {
for(int j = i << 1; j < N; j += i) {
is_prime[j] = false;
}
}
}
tot = 0;
primes[tot++] = 2;
for(int i = 3; i < N; i+=2) {
if(is_prime[i]) {
primes[tot++] = i;
}
}
}
int main(int argc, char ** argv) {
if( argc != 2) {
cout << "please input <param>!" << '\n';
return 1;
}
else {
sscanf(argv[1], "%d", &N);
N = N+1;
primes = new int[N];
is_prime = new bool[N];
gen();
char file_name[100];
sprintf(file_name, "primes_%d.out", N-1);
printf("%s generated!\n", file_name);
FILE * fp = fopen(file_name, "w");
for(int i = 0; i < tot; i++) {
fprintf(fp, "%d ", primes[i]);
}
fclose(fp);
delete [] primes;
delete [] is_prime;
/*for(int i = 0; i < tot; i++) {
cout << primes[i] << (i == tot - 1?'\n':' ');
}*/
}
}