summaryrefslogtreecommitdiff
path: root/sys/src/cmd/primes.c
blob: ccc8ac93763e93f458391445f0fab246f1a93431 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <u.h>
#include <libc.h>
#include <bio.h>

double big = 9.007199254740992e15;

int pt[] =
{
	  2,  3,  5,  7, 11, 13, 17, 19, 23, 29,
	 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
	 73, 79, 83, 89, 97,101,103,107,109,113,
	127,131,137,139,149,151,157,163,167,173,
	179,181,191,193,197,199,211,223,227,229,
};
double wheel[] =
{
	10, 2, 4, 2, 4, 6, 2, 6, 4, 2,
	 4, 6, 6, 2, 6, 4, 2, 6, 4, 6,
	 8, 4, 2, 4, 2, 4, 8, 6, 4, 6,
	 2, 4, 6, 2, 6, 6, 4, 2, 4, 6,
	 2, 6, 4, 2, 4, 2,10, 2,
};
uchar table[1000];
uchar bittab[] =
{
	1, 2, 4, 8, 16, 32, 64, 128,
};

enum {
	ptsiz	= nelem(pt),
	whsiz	= nelem(wheel),
	tabsiz	= nelem(table),
	tsiz8	= tabsiz*8,
};

void	mark(double nn, long k);

void
usage(void)
{
	fprint(2, "usage: %s [start [finish]]\n", argv0);
	exits("limits");
}

void
ouch(void)
{
	fprint(2, "limits exceeded\n");
	exits("limits");
}

void
main(int argc, char *argv[])
{
	int i;
	double k, temp, v, limit, nn;
	char *l;
	Biobuf bin;

	ARGBEGIN{
	default:
		usage();
		break;
	}ARGEND;

	nn = 0;
	limit = big;
	switch (argc) {
	case 0:
		Binit(&bin, 0, OREAD);
		while ((l = Brdline(&bin, '\n')) != nil) {
			if (*l == '\n')
				continue;
			nn = atof(l);
			if(nn < 0)
				sysfatal("negative start");
			break;
		}
		Bterm(&bin);
		break;
	case 2:
		limit = atof(argv[1]);
		if(limit < nn)
			exits(0);
		if(limit > big)
			ouch();
		/* fallthrough */
	case 1:
		nn = atof(argv[0]);
		break;
	default:
		usage();
		break;
	}

	if(nn < 0 || nn > big)
		ouch();
	if(nn == 0)
		nn = 1;

	if(nn < 230) {
		for(i=0; i<ptsiz; i++) {
			if(pt[i] < nn)
				continue;
			if(pt[i] > limit)
				exits(0);
			print("%d\n", pt[i]);
//			if(limit >= big)
//				exits(0);
		}
		nn = 230;
	}

	modf(nn/2, &temp);
	nn = 2*temp + 1;
/*
 *	clear the sieve table.
 */
	for(;;) {
		for(i = 0; i < tabsiz; i++)
			table[i] = 0;
/*
 *	run the sieve.
 */
		v = sqrt(nn+tsiz8);
		mark(nn, 3);
		mark(nn, 5);
		mark(nn, 7);
		for(i = 0, k = 11; k <= v; k += wheel[i]) {
			mark(nn, k);
			i++;
			if(i >= whsiz)
				i = 0;
		}
/*
 *	now get the primes from the table
 *	and print them.
 */
		for(i = 0; i < tsiz8; i += 2) {
			if(table[i>>3] & bittab[i&07])
				continue;
			temp = nn + i;
			if(temp > limit)
				exits(0);
			print("%.0f\n", temp);
//			if(limit >= big)
//				exits(0);
		}
		nn += tsiz8;
	}
}

void
mark(double nn, long k)
{
	double t1;
	long j;

	modf(nn/k, &t1);
	j = k*t1 - nn;
	if(j < 0)
		j += k;
	for(; j < tsiz8; j += k)
		table[j>>3] |= bittab[j&07];
}