-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnsieve.dylan
41 lines (32 loc) · 953 Bytes
/
nsieve.dylan
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
module: nsieve
author: Eric Kidd <eric.kidd@pobox.com>
define library nsieve
use common-dylan;
use io;
end library;
define module nsieve
use common-dylan, exclude: { format-to-string };
use format-out;
end module;
define function nsieve(limit :: <integer>) => result :: <integer>;
let flags = make(<byte-vector>, size: limit + 1, fill: 1);
let count = 0;
for (i from 2 below limit )
if ( flags[i] > 0)
for (k from (i * 2) below limit by i)
flags[k] := 0;
end for;
count := count + 1;
end if;
end for;
count;
end function nsieve;
begin
let n = string-to-integer(element(application-arguments(), 0, default: "2"));
let m = (2 ^ n) * 10000;
format-out("Primes up to %8d %8d\n", m, nsieve(m));
m := (2 ^ (n - 1)) * 10000;
format-out("Primes up to %8d %8d\n", m, nsieve(m));
m := (2 ^ (n - 2)) * 10000;
format-out("Primes up to %8d %8d\n", m, nsieve(m));
end;