Skip to content

Instantly share code, notes, and snippets.

@oropon
Last active January 3, 2016 05:58
Show Gist options
  • Save oropon/8419087 to your computer and use it in GitHub Desktop.
Save oropon/8419087 to your computer and use it in GitHub Desktop.
module Main where
main = putStrLn $ unlines $ map (show) $ primes 1000000
primes :: Int -> [Int]
primes max
| max < 2 = error "No primes"
| otherwise = erat [] [2..max]
where
erat :: [Int] -> [Int] -> [Int]
erat ps [] = ps
erat ps (x:xs)
| x ^ 2 < max
= erat (x:ps) (filter (\n -> n `mod` x /= 0) xs)
| otherwise = reverse ps ++ xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment