Skip to content

Instantly share code, notes, and snippets.

@plaster
Created December 19, 2015 15:43
Show Gist options
  • Save plaster/bf2fc29bf66aa58ebdb0 to your computer and use it in GitHub Desktop.
Save plaster/bf2fc29bf66aa58ebdb0 to your computer and use it in GitHub Desktop.
素直な素因数分解
factorize :: Integer -> [ Integer ]
factorize n = factorize' n $ takeWhile (\x -> x * x <= n) [ 2 .. n ] where
factorize' n [] = case n of
1 -> []
_ -> [n]
factorize' n ds@(d:ds') = case n `mod` d of
0 -> d : factorize' (n `div` d) ds
_ -> factorize' n ds'
main = interact $ show . factorize . read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment