blob: 19c3293817a8ba2973e31c62a8f2973c253f0024 (
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
|
implement Primes;
include "draw.m";
Primes: module
{
init: fn(nil: ref Draw->Context, argl: list of string);
};
include "sys.m";
sys: Sys;
include "arg.m";
arg: Arg;
LIM: con 1729;
MAX: con 1000000;
BUFSZ: con 256;
init(nil: ref Draw->Context, argl: list of string)
{
sys = load Sys Sys->PATH;
arg = load Arg Arg->PATH;
arg->init(argl);
quiet := 0;
lim := LIM;
while((ch := arg->opt()) != 0){
case (ch){
'q' =>
quiet = 1;
* =>
;
}
}
argv := arg->argv();
if(argv != nil)
lim = int hd argv;
if(lim < 2)
lim = 2;
if(lim > MAX)
lim = MAX;
c := chan[BUFSZ] of int;
spawn prime(c, !quiet);
for(n := 2; n <= lim; n++)
c <-= n;
c <-= 1;
}
prime(c: chan of int, pr: int)
{
p := <-c;
if(p == 1)
exit;
if(pr)
sys->print("%d\n", p);
nc := chan[BUFSZ] of int;
spawn prime(nc, pr);
for(;;){
n := <-c;
if(n%p)
nc <-= n;
if(n == 1)
exit;
}
}
|