Fixed a lazySlot bug where lazy slots in different objects end up pointing to the...
[io/quag.git] / addons / Loki / samples / shootout / nsieve-bits.io
blob50fb7bd631046e956438f575ff024cddf1dbf960
1 /* The Great Computer Language Shootout
2 http://shootout.alioth.debian.org
4 contributed by Ian Osgood */
6 Sequence pad := method(w, s:=self; while (s size < w, s=" ".. s) )
7 Number clearBit := method(b, &(1 clone shiftLeft(b) bitwiseComplement) )
9 sieve := Object clone do(
10 flags := Sequence clone do(
11 isSet := method(i, at(i/8) at(i%8) != 0)
12 clear := method(i, atPut(i/8, at(i/8) clearBit(i%8) ) )
14 show := method(n,
15 flags setSize((n/8) ceil)
16 for (i, 0, flags size - 1, flags atPut(i, 255))
17 primes := 0
18 for (i, 2, n-1,
19 if (flags isSet(i),
20 primes = primes + 1
21 if (i+i < n, for (j, i+i, n-1, i, flags clear(j) ) )
24 writeln("Primes up to", n asString pad(9), primes asString pad(9))
28 n := args at(1) asNumber
29 sieve show(10000 shiftLeft(n))
30 sieve show(10000 shiftLeft(n-1))
31 sieve show(10000 shiftLeft(n-2))