Skip to content

Instantly share code, notes, and snippets.

@KatagiriSo
Created June 12, 2017 09:39
Show Gist options
  • Save KatagiriSo/43f729dd4b87f211a13f6528a51b86e0 to your computer and use it in GitHub Desktop.
Save KatagiriSo/43f729dd4b87f211a13f6528a51b86e0 to your computer and use it in GitHub Desktop.
main = do
print $ factorize(1000)
prime :: Int -> Bool
prime n = length([x | x <- [1..n], n `mod` x == 0 ]) == 2
primes :: Int -> [Int]
primes n = [x | x <- [1..n], prime x]
factor :: Int -> Int -> Bool
factor x y = x `mod` y == 0
factorize :: Int -> [Int]
factorize 1 = []
factorize n =
let
x = head [x | x <- primes n, factor n x]
in
x:(factorize (n `div` x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment