Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active August 29, 2015 14:12
Show Gist options
  • Save fbiville/7471b9b0da7467184188 to your computer and use it in GitHub Desktop.
Save fbiville/7471b9b0da7467184188 to your computer and use it in GitHub Desktop.
import Data.Maybe
import Data.List
primeFactors :: Int -> [Int]
primeFactors = (filter prime) . divisors
prime :: Int -> Bool
prime a =
let upper = floor (sqrt (fromIntegral a)) + 1 in
(a `mod` 2 /= 0) && isNothing (find (isMultiple a) [3,5..upper])
divisors :: Int -> [Int]
divisors a = let upper = floor(fromIntegral(a)/2) in filter (isMultiple a) [2..upper]
isMultiple :: Int -> Int -> Bool
isMultiple a b = a `mod` b == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment