Skip to content

Instantly share code, notes, and snippets.

@eccstartup
Created June 2, 2016 02:06
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 eccstartup/cb69911c163649e5dd0991c27ae102b5 to your computer and use it in GitHub Desktop.
Save eccstartup/cb69911c163649e5dd0991c27ae102b5 to your computer and use it in GitHub Desktop.
module PrimeFactors where
lessFacs1 n = [k | k <- [2..(truncate (sqrt (fromInteger n)))], mod n k == 0]
lessPrimeFacs [] n = []
lessPrimeFacs (p:ps) n
| mod n p == 0 = p:(lessPrimeFacs (p:ps) (div n p))
| otherwise = lessPrimeFacs ps n
lessFacs n = lessPrimeFacs (lessFacs1 n) n
factor 1 = []
factor n
| lf == [] = [n]
| length lf == 1 = lf ++ [div n (head lf)]
| otherwise = lf
where lf = lessFacs n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment