-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsieve.c
43 lines (40 loc) · 1.36 KB
/
sieve.c
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
/*
* sieve.c
*
* Prints out primes from 1 to N, one per line.
*
* Adaptation/sizeable clean up of the Sieve of Eratosthenes I found online at
* http://www.cs.ucr.edu/~ciardo/teaching/CS237/source/sieve.c
*
* GRE, 1/20/10
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define N 2000000 /* Limited only by memory size */
#define SQRTN ((long) sqrt(N))
int main(void) {
bool prime[N + 1]; /* n is prime <=> prime[n] */
long n; /* Index of possible primes */
long s; /* Step index */
/* Initialize the sieve */
prime[0] = false;
prime[1] = false;
memset(prime + 2, true, N - 2);
for (n = 2; n <= SQRTN; n++) { /* Search all possibilities */
if (prime[n]) { /* If n is prime,... */
for (s = 2; s <= (N / n); s++) {
/* ...sn can't be prime for any s */
prime[s * n] = false;
}
}
}
/* Output */
for (n = 2; n <= N; n++) {
if (prime[n]) {
printf("%7ld\n", n);
}
}
return 0;
}