Skip to content

Instantly share code, notes, and snippets.

@VlachJosef
Created November 22, 2017 22:50
Show Gist options
  • Save VlachJosef/10c58fbea0772b90e75c00a274728cb7 to your computer and use it in GitHub Desktop.
Save VlachJosef/10c58fbea0772b90e75c00a274728cb7 to your computer and use it in GitHub Desktop.
factorisation
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