Skip to content

Instantly share code, notes, and snippets.

@maxdeliso
Created September 5, 2019 20:30
Show Gist options
  • Save maxdeliso/98ab958cb7a4018dfb8382f7f2490354 to your computer and use it in GitHub Desktop.
Save maxdeliso/98ab958cb7a4018dfb8382f7f2490354 to your computer and use it in GitHub Desktop.
simple sieve of eratosthenes in haskell
divides n d = n /= d && mod n d == 0
eratosthenes n =
let
filter_k_rec d acc =
if d * d < n
then filter_k_rec (d + 1) $ filter (\n -> not $ divides n d) acc
else acc
in filter_k_rec 2 [1..n]
main = do
putStrLn "enter n: "
n <- readLn :: IO Int
print $ eratosthenes n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment