-
Notifications
You must be signed in to change notification settings - Fork 1
/
era2e.c
86 lines (72 loc) · 2.49 KB
/
era2e.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ST unsigned long
#define SW (sizeof(ST) * 8)
// 5 7 . 11 13 . 17 19 . 23 25 . 29 31 . 35 37 . 41 43 . 47 49 . 53 55 . 59 61 . 65 67 . 71 73 . 77 79 .
//
//
void doSieve(ST max, void (*action)(ST)){
ST *sieve = calloc(((max) / (2 * SW)) + 3, sizeof(ST));
ST i;
if(action) action(3);
for(i = 0; i < ((max) ) / 3; ++i)
{
// printf(" i = %lu, p = %lu, %lu \n", i, p, i/SW);
if(!(sieve[i / SW] & ((ST)1 << (i % SW))))
{
ST p = 5 + 2*(i + i/2);
if(p > max){
break;
}
ST j = (p*p-5)/2 - (p*p-5)/6;
// printf(" l_0: %lu %lu %lu / %lu\n", p, p*p, j, p%3);
ST k;
if(p%3 == 1){
k = ((p+4)*p-5)/2 - (((p+4)*p-5)/2)/3;
}
else{
k = ((p+2)*p-5)/2 - (((p+2)*p-5)/2)/3;
}
// printf(" k = %lu max/3 = %lu max/3-k = %lu \n", k, max/3, max/3-k);
if(max/3 > j){
if(max/3 < k){
// only one half step
sieve[(j) / SW] |= ((ST)1 << ((j) % SW));
continue;
}
ST l=0;
for(l=0; l <= (max/3-k)/(2*p); l++){
// printf("p %lu unsetting %lu %lu, / %lu / %lu\n", p, j+2*l*p, k+2*l*p, max/3, k);
sieve[(j+2*l*p) / SW] |= ((ST)1 << ((j+2*l*p) % SW));
sieve[(k+2*l*p) / SW] |= ((ST)1 << ((k+2*l*p) % SW));
// printf(" k = %lu 2pl+k = %lu max/3 = %lu max/3-k = %lu \n", k, 2*p*l + k, max/3, max/3-k);
}
if(j + 2*l+p <= max/3){
sieve[(j+2*l*p) / SW] |= ((ST)1 << ((j+2*l*p) % SW));
}
}
if(action) action(p);
}
}
}
void modPrint(ST p){
static long anz = 0;
anz++;
if(anz % 1000 == 7) printf("%lu %lu\n", anz, p);
/*
static long anz = 0;
anz++;
if(anz % 1000 == 7) printf("%lu %lu\n", anz, p);
*/
}
int main(){
// unsigned long max = 10000000UL;
unsigned long max = 10000000UL;
// unsigned long max = 500UL;
// doSieve(max*log(max)*log(log(max)) , &modPrint);
doSieve((max * log(max) + max * (log(log(max)) - 0.9385)), &modPrint);
printf("max = %lu\n", (max * log(max) + max * (log(log(max)) - 0.9385)));
// check(13);
return 0;
}