Created
June 19, 2017 19:49
-
-
Save algorev/48ea01c921fdbedb7762fcc2fceab245 to your computer and use it in GitHub Desktop.
Prime Generator Written In Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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