|
sieve←{
b←⍵⍴{∧⌿↑(×/⍵)⍴¨~⍵↑¨1}q←2 3 5
b[⍳6⌊⍵]←(6⌊⍵)⍴0 0 1 1 0 1
49≥⍵:b
p←(≢q)↓{⍵/⍳⍴⍵}∇⌈⍵*0.5
m←1+⌊(⍵-1+p×p)÷2×p
b ⊣ p {b[⍺×⍺+2×⍳⍵]←0}¨ m
}
a. |
Why is 1 not a prime?
(Some older math texts say 1 is a prime.)
|
b. |
Write a version of sieve that
checks its argument and result.
assert←{⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕signal 8 ⋄ shy←0}
sieve_chk←{
b←sieve ⍵
assert(,⍵)≡⍴b:
assert∧/b∊0 1:
...
b
}
(1⊣sieve_chk)¨ ⍳10
(1⊣sieve_chk)¨ ¯1 0 1∘.+2 3 5 7 11 13 17 19*2
|
c. |
The function pco from the dfns workspace performs various prime computations.
In particular, b←10 pco r,s
is a boolean vector such that r+b/⍳≢b
are the primes in the half-open interval [r,s) .
Check that pco is consistent
with sieve
(and that pco is self-consistent).
|
|