Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created May 14, 2016 11:56
Show Gist options
  • Save JadenGeller/bde391ba142c6a1d357aba2422b2340a to your computer and use it in GitHub Desktop.
Save JadenGeller/bde391ba142c6a1d357aba2422b2340a to your computer and use it in GitHub Desktop.
Factorization in Haskell
import Data.Numbers.Primes
import Data.List
factors :: Integral n => n -> [n]
factors n = case find (\p -> n `mod` p == 0) candidates of
Just p -> p : (factors $ n `div` p)
Nothing -> [n]
where candidates = takeWhile (<= ceiling (sqrt (fromIntegral n))) primes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment