module Exercise05 where -- May or may not be useful: computes the logarithm base 2 (rounded down) of the given number. -- You don't have to move this into the WETT tags if you want to use it. log2 :: (Integral a, Num b) => a -> b log2 = let go acc n = if n <= 1 then acc else go (acc + 1) (n `div` 2) in go 0 {-WETT-} decompose :: [Integer] -> [Integer] decompose ds = if 0 `elem` ds then [] else help 0 (log2 $minimum ds) ds help :: Integer -> Integer -> [Integer] -> [Integer] help x e ds = if x == e + 1 then [] else loopy (product $map (`div` a) ds) (map (`div` a) $filter (\x -> x `mod` i /= 0) ds) : help (x + 1) e (map (\x -> if x `mod` i == 0 then x else x - a) ds) where i = 2 ^ (x + 1) a = 2 ^ x notneg j = if j < 0 then 0 else j loopy p ys = if null ys then 0 else loopy ((p `div` head ys) * notneg (head ys -1)) (tail ys) + p `div` head ys {-TTEW-}