Sieve of Eratosthenes: Difference between revisions

→‎Immutable arrays, by segments: +sub_section "as list comprehension"
(→‎Immutable arrays, by segments: +sub_section "as list comprehension")
Line 9,132:
a = accumArray (\ b c -> False) True (1,q-1)
[(i,()) | (s,y) <- fs, i <- [y+s, y+s+s..q]]</syntaxhighlight>
 
====As list comprehension====
 
<syntaxhighlight lang="haskell">import Data.Array.Unboxed
import Data.List (tails, inits)
 
primes = 2 : [ n |
(r:q:_, px) <- zip (tails (2 : [p*p | p <- primes]))
(inits primes),
(n, True) <- assocs ( accumArray (\_ _ -> False) True
(r+1,q-1)
[ (m,()) | p <- px
, s <- [ div (r+p) p * p]
, m <- [s,s+p..q-1] ] :: UArray Int Bool
) ]</syntaxhighlight>
 
===Basic list-based sieve===
751

edits