Skip to content

Instantly share code, notes, and snippets.

@algorev
Created June 19, 2017 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save algorev/48ea01c921fdbedb7762fcc2fceab245 to your computer and use it in GitHub Desktop.
Save algorev/48ea01c921fdbedb7762fcc2fceab245 to your computer and use it in GitHub Desktop.
Prime Generator Written In Haskell
module Main where
import Data.List
main :: IO ()
main = putStrLn $ unlines $ map show $ primesTo 100
primesTo :: Int -> [Int]
primesTo upTo = primesTo' [2..upTo] []
primesTo' :: [Int] -> [Int] -> [Int]
primesTo' [] primes = primes
primesTo' (x:xs) primes = if foldl (||) False $ map (dividableBy x) primes
then primesTo' xs primes
else primesTo' xs (x `insert` primes)
dividableBy :: Int -> Int -> Bool
dividableBy lop rop = (lop `mod` rop) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment