haskell: lazy sieve of Eratosthenes (+mod/div/rem/quot, +2 tests, 390/390)

This commit is contained in:
2026-04-25 17:59:39 +00:00
parent 4ed7ffe9dd
commit 9be65d7d60
3 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
-- sieve.hs — lazy sieve of Eratosthenes.
--
-- Each recursive call to `sieve` consumes one prime `p` off the front
-- of the input stream and produces an infinite stream of composites
-- filtered out via `filter`. Because cons is lazy, only as much of
-- the stream is forced as demanded by `take`.
sieve (p:xs) = p : sieve (filter (\x -> x `mod` p /= 0) xs)
sieve [] = []
primes = sieve [2..]
result = take 10 primes