-- sum of primes
-- https://p...content-available-to-author-only...s.com/
-- 2012/09/programmingwithprimenumbers.pdf
import Control
.Monad (forM
_, when
) import Data.Array.ST
import Data.Array.Unboxed
sieve n = runSTUArray $ do
bits <- newArray (0, m-1) True
forM
_ [0 .. r `
div`
2 - 1] $ \i
-> do isPrime <- readArray bits i
when isPrime $ do
let a = 2*i*i + 6*i + 3
b = 2*i*i + 8*i + 6
forM_ [a, b .. (m-1)] $ \j -> do
writeArray bits j False
primes n = 2 : [2*i+3 | (i, True) <- assocs $ sieve n]
main = do
LS0gc3VtIG9mIHByaW1lcwotLSBodHRwczovL3AuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLnMuY29tLwotLSAgICAgMjAxMi8wOS9wcm9ncmFtbWluZ3dpdGhwcmltZW51bWJlcnMucGRmCgppbXBvcnQgQ29udHJvbC5Nb25hZCAoZm9yTV8sIHdoZW4pCmltcG9ydCBDb250cm9sLk1vbmFkLlNUCmltcG9ydCBEYXRhLkFycmF5LlNUCmltcG9ydCBEYXRhLkFycmF5LlVuYm94ZWQKCnNpZXZlIDo6IEludCAtPiBVQXJyYXkgSW50IEJvb2wKc2lldmUgbiA9IHJ1blNUVUFycmF5ICQgZG8KICAgIGxldCBtID0gKG4tMSkgYGRpdmAgMgogICAgICAgIHIgPSBmbG9vciAuIHNxcnQgJCBmcm9tSW50ZWdyYWwgbgogICAgYml0cyA8LSBuZXdBcnJheSAoMCwgbS0xKSBUcnVlCiAgICBmb3JNXyBbMCAuLiByIGBkaXZgIDIgLSAxXSAkIFxpIC0+IGRvCiAgICAgICAgaXNQcmltZSA8LSByZWFkQXJyYXkgYml0cyBpCiAgICAgICAgd2hlbiBpc1ByaW1lICQgZG8KICAgICAgICAgICAgbGV0IGEgPSAyKmkqaSArIDYqaSArIDMKICAgICAgICAgICAgICAgIGIgPSAyKmkqaSArIDgqaSArIDYKICAgICAgICAgICAgZm9yTV8gW2EsIGIgLi4gKG0tMSldICQgXGogLT4gZG8KICAgICAgICAgICAgICAgIHdyaXRlQXJyYXkgYml0cyBqIEZhbHNlCiAgICByZXR1cm4gYml0cwoKcHJpbWVzIDo6IEludCAtPiBbSW50XQpwcmltZXMgbiA9IDIgOiBbMippKzMgfCAoaSwgVHJ1ZSkgPC0gYXNzb2NzICQgc2lldmUgbl0KCm1haW4gPSBkbwogICAgcHJpbnQgJCBzdW0gJCBwcmltZXMgMTAwMDAwMA==