⍝ Sieve of Eratosthenes:

    sieve 2 to 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

    ⎕io ← 0                             ⍝ Roger's coding:

    sieve←{
      b←⍵⍴{∧⌿↑(×/⍵)⍴¨~⍵↑¨1}2 3 5
      b[⍳6⌊⍵]←(6⌊⍵)⍴0 0 1 1 0 1
      49≥⍵:b
      p←3↓⍸∇⌈⍵*0.5
      m←1+⌊(⍵-1+p×p)÷2×p
      b⊣p{b[⍺×⍺+2×⍳⍵]←0}¨m
    }

    ⍸ sieve 40                          ⍝ primes less than 40
2 3 5 7 11 13 17 19 23 29 31 37

⍝∇ sieve to

Back to: code

Back to: Workspaces