-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sieve_1bit.c
49 lines (42 loc) · 1.28 KB
/
Sieve_1bit.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
44
45
46
47
48
49
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 1000000000
int main(void)
{
int arraySize = ((N + 63)/64 + 1) * sizeof(uint32_t);
uint32_t *primes = malloc(arraySize);
// Each bit in primes is an odd number.
// Bit 0 = 1, bit 1 = 3, bit 2 = 5, etc.
memset(primes, 0xff, arraySize);
// 1 is not a prime.
primes[0] &= ~0x1;
int sqrt_N = sqrt(N);
for(int i = 3; i <= sqrt_N; i += 2) {
int iIndex = i >> 6;
int iBit = (1 << ((i >> 1) & 31));
if ((primes[iIndex] & iBit) != 0) {
int increment = i+i;
for (int j = i * i; j < N; j += increment) {
int jIndex = j >> 6;
int jBit = (1 << ((j >> 1) & 31));
primes[jIndex] &= ~jBit;
}
}
}
// Count the number of primes in order to verify that the above worked.
// Start count at 1 to include 2 as the first prime, since we are only
// going to count odd primes.
int count = 1;
for (int i = 3; i < N; i += 2) {
int iIndex = i >> 6;
int iBit = (1 << ((i >> 1) & 31));
if (primes[iIndex] & iBit)
count++;
}
printf("%d\n", count);
free(primes);
return 0;
}