Created
November 22, 2017 22:50
-
-
Save VlachJosef/10c58fbea0772b90e75c00a274728cb7 to your computer and use it in GitHub Desktop.
factorisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Factorisation where | |
import Control.MonadZero (guard) | |
import Data.Array (concatMap, cons, nub, sort, (..)) | |
import Data.Functor (map) | |
import Data.Tuple (Tuple(..)) | |
import Prelude (bind, pure, ($), (==), discard, (*)) | |
factors :: Int -> Array (Tuple Int Int) | |
factors n = do | |
i <- 2 .. n | |
j <- i .. n | |
guard $ i * j == n | |
pure $ Tuple i j | |
factorisation :: Int -> Array (Array Int) | |
factorisation n = nub $ map sort $ concatMap (\(Tuple h t) -> cons [h,t] $ map (cons h) (factorisation t)) (factors n) | |
-- > factorisation 12 | |
-- [[2,6],[2,2,3],[3,4]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment