diff options
Diffstat (limited to 'appl/math/genprimes.b')
| -rw-r--r-- | appl/math/genprimes.b | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/appl/math/genprimes.b b/appl/math/genprimes.b new file mode 100644 index 00000000..19c32938 --- /dev/null +++ b/appl/math/genprimes.b @@ -0,0 +1,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; + } +} |
